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