Bash_Scripts/Templates/Functions/Arg-Parsing.bash

69 lines
1.2 KiB
Bash
Raw Normal View History

2021-07-28 09:32:08 -05:00
#!/bin/bash --posix
2021-08-04 21:17:20 -05:00
2021-07-28 09:32:08 -05:00
usage() {
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
2021-08-03 23:45:33 -05:00
printf "Usage: %s\n" \
"$(basename ${0}) -i \"this is some input\" -t \"this is some more example input\"
2021-07-28 09:32:08 -05:00
--input <string> | -i <string>
Example:
--input \"this is an example input\"
--test <string> | -t <string>
Example:
--test \"this is more example input\""
}
parse_args() {
# Parse input arguments
#
# Arguments:
# Consult the `usage` function
#
# Usage:
# parse_args "$@"
# - All arguments should be ingested by parse_args first for variable setting
#
# POSIX Compliant:
# Yes
#
while :; do
case ${1} in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
;;
--) # End of all options.
break
;;
-i | --input)
2021-08-17 20:21:33 -05:00
echo "${1}"
2021-07-28 09:32:08 -05:00
;;
-t | --test)
2021-08-17 20:21:33 -05:00
echo "${1}"
2021-07-28 09:32:08 -05:00
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
exit 1
2021-07-28 09:32:08 -05:00
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
2021-08-03 23:45:33 -05:00
shift
2021-07-28 09:32:08 -05:00
done
}
parse_args "$@"