NBash

Форк
0
56 строк · 1.7 Кб
1
#!/usr/bin/env bash
2

3
PATH="..:$PATH"
4

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

8
# Beside the 'type', you can control options user-given values by
9
# using other methods.
10

11
argsparse_use_option option1 "An enumerated option." value
12

13
# *Instead* of declaring a type for the option, you can declare an
14
# array named option_<optionname>_values, which will contain the
15
# acceptable values for said option.
16
option_option1_values=(only accepting those values)
17
# Acceptable values are mentioned in argsparse usage.
18

19
# And, after either type checking of enumerated values checking, you
20
# have the possibility to simply define a function named
21
# check_value_of_<optionname>. If the function return with value 0, it
22
# means the value is correct. Any other value returned by the function
23
# would make the value incorrect.
24
argsparse_use_option option2 "An always bad option." value
25
check_value_of_option2() {
26
	# So, this would make all values return an error.
27
	false
28
}
29

30
# If an option name contains '-' chars...
31
argsparse_use_option long-option "An option with a long name." value
32
# ... then the array name...
33
option_long_option_values=(long option acceptable values)
34
# ... and the function name must have '_' in place of '-'.
35
check_value_of_long_option() {
36
	local value=$1
37
	[[ "$value" = long ]]
38
}
39

40
# 
41
printf -v argsparse_usage_description "%s\n" \
42
	"A tutorial script teaching advanced value checking." \
43
	"Try command lines such as:" \
44
	" $0" \
45
	" $0 -h" \
46
	" $0 --option1 only" \
47
	" $0 --option2 something" \
48
	" $0 --long-option only"
49

50
# Command line parsing is done here.
51
argsparse_parse_options "$@"
52

53
printf "Options reporting:\n"
54
# Simple reporting function.
55
argsparse_report
56
printf "End of argsparse report.\n\n"
57

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

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

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

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