NBash
57 строк · 1.8 Кб
1#!/usr/bin/env bash
2
3PATH="..:$PATH"
4
5# Load argsparse library.
6. argsparse.sh
7
8# It is possible to have cumulative options with argsparse. Cumulative
9# options can be repeated and all values are kept in an array.
10# To have a cumulative option just declare an option with the
11# 'cumulative' property.
12argsparse_use_option =option1 "A cumulative option" cumulative
13# The user-given values will be stored in the array named
14# cumulated_values_<option name>
15
16# The cumulativeset property acts exactly like cumulative, except that
17# there can not be any repetition of a value, like in a python 'set'.
18
19# e.g: In this example, using "--option2 foo --option2 foo" wont add a
20# second 'foo' to option2 list of user-given values.
21argsparse_use_option option2 "A uniqly-cumulative option" \
22cumulativeset short:O
23
24#
25printf -v argsparse_usage_description "%s\n" \
26"A tutorial script for cumulative options." \
27"Try command lines such as:" \
28" $0" \
29" $0 -h" \
30" $0 --option1 123" \
31" $0 --option1 123 -o 456 -o 'foo bar'" \
32" Also note the difference of behaviour between those 2 lines:" \
33" $0 --option1 123 -o 123 -o 456 -o 'foo bar' -o 456" \
34" $0 --option2 123 -O 123 -O 456 -O 'foo bar' -O 456"
35
36# Command line parsing is done here.
37argsparse_parse_options "$@"
38
39printf "Options reporting:\n"
40# Simple reporting function.
41argsparse_report
42printf "End of argsparse report.\n\n"
43
44#
45if argsparse_is_option_set option1
46then
47printf 'option1 has been set %d time(s):\nUser-given values are:\n' \
48"${#cumulated_values_option1[@]}"
49printf -- '- %s\n' "${cumulated_values_option1[@]}"
50fi
51
52if argsparse_is_option_set option2
53then
54printf 'option2 has been set %d time(s):\nUser-given values are:\n' \
55"${#cumulated_values_option2[@]}"
56printf -- '- %s\n' "${cumulated_values_option2[@]}"
57fi
58