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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/usr/bin/env bash
PATH="..:$PATH"
# Load argsparse library.
. /usr/share/bash-argsparse/argsparse.sh
# In previous tutorials, we have already seen some properties:
# * short
# * value
# * default
# * cumulative
# * type
# There exists other properties.
# options with 'mandatory' property are not an option (sic). All
# 'mandatory' must be set on the command line.
argsparse_use_option option1 "A mandatory option." mandatory
# The 'hidden' property hides an option to the user by not showing the
# option in the usage message nor in the argsparse_report output.
argsparse_use_option option2 "An hidden option." hidden
# Options can exclude each others, through the 'exclude' property.
argsparse_use_option option3 "A 3rd option."
argsparse_use_option option4 "A 4th option." exclude:option3
# The 'exclude' property value is actually a space-separated option list.
argsparse_use_option option5 "A 5th option." exclude:'option3 option4'
# You can set option aliases using the 'alias' property. Only options
# without the 'value' property are accepted both as the alias and as
# 'alias' property value.
# As for the 'exclude' property, the 'alias' property value is a
# space-separated option list.
argsparse_use_option option6 "A 6th option." alias:'option1 option2 option3'
# Aliases are mentioned in argsparse usage.
# You can make an alias of an alias. But be careful, though, as there
# is currently no loop detection.
argsparse_use_option option7 "A 7th option" alias:option6
# You can also create dependencies between options. In this case, if
# you specify --option8, you cannot omit --option2 nor --option3.
argsparse_use_option option8 "A 8th option" require:"option2 option3"
#
printf -v argsparse_usage_description "%s\n" \
"A tutorial script teaching other argsparse option properties." \
"Try command lines such as:" \
" $0" \
" $0 -h" \
" $0 --option1" \
" $0 --option2" \
" $0 --option3 --option4" \
" $0 --option5 --option4" \
" $0 --option1 --option6"
# 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 option2
then
printf "The hidden option %s has been set. %d time(s)\n" \
option2 "${program_options[option2]}"
else
printf "The hidden option %s remains hidden and unset.\n" option2
fi
|