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 74
|
#!/usr/bin/env bash
PATH="..:$PATH"
# Load argsparse library.
. /usr/share/bash-argsparse/argsparse.sh
# Miscellaneous other functions. See below.
argsparse_use_option option1 "An option" value exclude:option2
argsparse_use_option option2 "Another option"
#
printf -v argsparse_usage_description "%s\n" \
"A tutorial script to show other misc argsparse features." \
"Try command lines such as:" \
" $0" \
" $0 -h" \
" $0 --option1 123a" \
" $0 --option2"
# You can define your own usage function
usage() {
printf "Beginning of usage\n"
# Unless you want it to be completely removed, you can still call
# argsparse_usage function to also have auto-generated argsparse
# usage message.
argsparse_usage
printf "End of usage\n"
# And perform your own exit.
exit 16
}
# Allow empty command line
argsparse_allow_no_argument yes
# Command line parsing is done here.
argsparse_parse_options "$@"
if [[ $# -eq 0 ]]
then
cat <<'information'
Though this script has been called without any parameter, `usage' has
not been triggered, due to the "argsparse_allow_no_argument yes"
executed a few lines above in the script.
To obtain the usage message, explicitly add --help on the command line.
information
fi
# The argsparse_has_option_property function returns with 0 if a named
# option has a named property. If the property has a value, it is
# printed to standard input. So you can do things like that:
if exclusion=$(argsparse_has_option_property option1 exclude)
then
printf "The option %s excludes these other options: %s\n" \
option1 "$exclusion"
fi
for option in option1 option2
do
if argsparse_has_option_property "$option" value
then
printf "%s has the value property.\n" "$option"
else
printf "%s does not have the value property.\n" "$option"
fi
done
printf "\nOptions reporting:\n"
# Simple reporting function.
argsparse_report
printf "End of argsparse report.\n\n"
|