NBash

Форк
0
1
#!/usr/bin/env bash
2
#
3

4
PATH="..:$PATH"
5

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

9
# We declare an option accepting a value by giving it the "value"
10
# property. By default, if an option with the "value" property is
11
# repeated on the command line, only the last value is kept in
12
# program_options. See other tutorials to learn how to have such
13
# cumulative options.
14
argsparse_use_option option1 "An option." value
15

16
# Declaring an option accepting a value and not having a
17
# single-char equivalent. (Alternative syntax)
18
# Ending the option name by ':' is equivalent
19
argsparse_use_option option2: "Another option."
20

21
# Of course, options can accept multiple properties.
22
argsparse_use_option option3 "A 3rd option." short:o value
23

24
# Alternative syntaxes for the same thing.
25
argsparse_use_option option4: "A 4th option." short:p
26
argsparse_use_option op=tion5: "A 5th option."
27

28
# Another option accepting a value, with a single-char equivalent.
29
# Setting the "default" property to an option make it have a default
30
# value. An option with a default value is considered as always set.
31
argsparse_use_option option6 "A 6th option." short:i value default:some_value
32
# Note that 'default' does not imply 'value'.
33

34
# Declaring the option... 
35
argsparse_use_option option7 "A 7th option."
36
# ...and setting properties after.
37
argsparse_set_option_property value option7
38
argsparse_set_option_property short:n option7
39
argsparse_set_option_property default:default_option7_value option7
40

41
# 
42
printf -v argsparse_usage_description "%s\n" \
43
	"A example script with options accepting values." \
44
	"Try command lines such as:" \
45
	" $0 --help" \
46
	" $0 --option1" \
47
	" $0 --option1 my_value" \
48
	" $0 -o some_other_value --option2 asdf" \
49
	" $0 --option7 not_default_anymore"
50

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

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

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

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

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

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