NBash
73 строки · 2.3 Кб
1#!/usr/bin/env bash
2
3PATH="..:$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.
19argsparse_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.
23argsparse_use_option option2 "An hidden option." hidden
24
25# Options can exclude each others, through the 'exclude' property.
26argsparse_use_option option3 "A 3rd option."
27argsparse_use_option option4 "A 4th option." exclude:option3
28# The 'exclude' property value is actually a space-separated option list.
29argsparse_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.
36argsparse_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.
41argsparse_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.
45argsparse_use_option option8 "A 8th option" require:"option2 option3"
46
47#
48printf -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.
60argsparse_parse_options "$@"
61
62printf "Options reporting:\n"
63# Simple reporting function.
64argsparse_report
65printf "End of argsparse report.\n\n"
66
67if argsparse_is_option_set option2
68then
69printf "The hidden option %s has been set. %d time(s)\n" \
70option2 "${program_options[option2]}"
71else
72printf "The hidden option %s remains hidden and unset.\n" option2
73fi
74