NBash

Форк
0
73 строки · 2.3 Кб
1
#!/usr/bin/env bash
2

3
PATH="..:$PATH"
4

5
# Load argsparse library.
6
. argsparse.sh
7

8
# In previous tutorials, we have already seen some properties: 
9
# * short
10
# * value
11
# * default
12
# * cumulative
13
# * type
14

15
# There exists other properties.
16

17
# options with 'mandatory' property are not an option (sic). All
18
# 'mandatory' must be set on the command line.
19
argsparse_use_option option1 "A mandatory option." mandatory
20

21
# The 'hidden' property hides an option to the user by not showing the
22
# option in the usage message nor in the argsparse_report output.
23
argsparse_use_option option2 "An hidden option." hidden
24

25
# Options can exclude each others, through the 'exclude' property.
26
argsparse_use_option option3 "A 3rd option."
27
argsparse_use_option option4 "A 4th option." exclude:option3
28
# The 'exclude' property value is actually a space-separated option list.
29
argsparse_use_option option5 "A 5th option." exclude:'option3 option4'
30

31
# You can set option aliases using the 'alias' property. Only options
32
# without the 'value' property are accepted both as the alias and as
33
# 'alias' property value.
34
# As for the 'exclude' property, the 'alias' property value is a
35
# space-separated option list.
36
argsparse_use_option option6 "A 6th option." alias:'option1 option2 option3'
37
# Aliases are mentioned in argsparse usage.
38

39
# You can make an alias of an alias. But be careful, though, as there
40
# is currently no loop detection.
41
argsparse_use_option option7 "A 7th option" alias:option6
42

43
# You can also create dependencies between options. In this case, if
44
# you specify --option8, you cannot omit --option2 nor --option3.
45
argsparse_use_option option8 "A 8th option" require:"option2 option3"
46

47
# 
48
printf -v argsparse_usage_description "%s\n" \
49
	"A tutorial script teaching other argsparse option properties." \
50
	"Try command lines such as:" \
51
	" $0" \
52
	" $0 -h" \
53
	" $0 --option1" \
54
	" $0 --option2" \
55
	" $0 --option3 --option4" \
56
	" $0 --option5 --option4" \
57
	" $0 --option1 --option6"
58

59
# Command line parsing is done here.
60
argsparse_parse_options "$@"
61

62
printf "Options reporting:\n"
63
# Simple reporting function.
64
argsparse_report
65
printf "End of argsparse report.\n\n"
66

67
if argsparse_is_option_set option2
68
then
69
	printf "The hidden option %s has been set. %d time(s)\n" \
70
		option2 "${program_options[option2]}"
71
else
72
	printf "The hidden option %s remains hidden and unset.\n" option2
73
fi
74

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.