NBash

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

3
PATH="..:$PATH"
4

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

8
# You can also change the way options are set by defining a function
9
# named set_option_<optionname>.
10
argsparse_use_option option1 "An option." value type:hexa
11

12
set_option_option1() {
13
	local option=$1
14
	local value=$2
15
	# You can do own stuff here. Whatever you want, including,
16
	# transforming the value, calling other functions, performing some
17
	# actions..
18
	# E.g: you could enforce the 0x in front of a user-given
19
	# hexadecimal value.
20
	if [[ "$value" != 0x* ]]
21
	then
22
		value="0x$value"
23
	fi
24
	# This is the original argsparse setting action.
25
	argsparse_set_option_with_value "$option" "$value"
26
}
27

28
# For options without value.
29
argsparse_use_option option2 "Another option."
30

31
set_option_option2() {
32
	local option=$1
33
	# Again, you can do whatever you want here.
34
	: some command with some params.
35
	# You dont have to do it, but it makes sense to call in the end
36
	# the original argsparse action.
37
	argsparse_set_option_without_value "$option"
38
}
39

40
# This is a way to re-implement a cumulative option if you're
41
# satisfied with the 'cumulative' property.
42
argsparse_use_option cumul "A cumulative option." value
43

44
all_cumul_values=()
45
set_option_cumul() {
46
	local option=$1
47
	local value=$2
48
	# Append the value to the array.
49
	all_cumul_values+=( "$value" )
50
	# Doing this will prevent the argsparse_is_option_set test from
51
	# returning false.
52
	argsparse_set_option_with_value "$option" 1
53
}
54

55
# 
56
printf -v argsparse_usage_description "%s\n" \
57
	"A tutorial script teaching how to change the way options are set." \
58
	"Try command lines such as:" \
59
	" $0" \
60
	" $0 -h" \
61
	" $0 --option1 123a" \
62
	" $0 --option2" \
63
	" $0 --cumul first --cumul second --cumul other"
64

65
# Command line parsing is done here.
66
argsparse_parse_options "$@"
67

68
printf "Options reporting:\n"
69
# Simple reporting function.
70
argsparse_report
71
printf "End of argsparse report.\n\n"
72

73
printf "These are all the 'cumul' option values:\n"
74
i=0
75
for v in ${all_cumul_values[@]}
76
do
77
	printf "Value #%d: %s\n" $((++i)) "$v"
78
done
79

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

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

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

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