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
|
#!/usr/bin/env bash
PATH="..:$PATH"
# Load argsparse library.
. /usr/share/bash-argsparse/argsparse.sh
# Beside the 'type', you can control options user-given values by
# using other methods.
argsparse_use_option option1 "An enumerated option." value
# *Instead* of declaring a type for the option, you can declare an
# array named option_<optionname>_values, which will contain the
# acceptable values for said option.
option_option1_values=(only accepting those values)
# Acceptable values are mentioned in argsparse usage.
# And, after either type checking of enumerated values checking, you
# have the possibility to simply define a function named
# check_value_of_<optionname>. If the function return with value 0, it
# means the value is correct. Any other value returned by the function
# would make the value incorrect.
argsparse_use_option option2 "An always bad option." value
check_value_of_option2() {
# So, this would make all values return an error.
false
}
# If an option name contains '-' chars...
argsparse_use_option long-option "An option with a long name." value
# ... then the array name...
option_long_option_values=(long option acceptable values)
# ... and the function name must have '_' in place of '-'.
check_value_of_long_option() {
local value=$1
[[ "$value" = long ]]
}
#
printf -v argsparse_usage_description "%s\n" \
"A tutorial script teaching advanced value checking." \
"Try command lines such as:" \
" $0" \
" $0 -h" \
" $0 --option1 only" \
" $0 --option2 something" \
" $0 --long-option only"
# 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"
|