Bash_Scripts/Templates/Functions/Arg-Parsing.bash

69 lines
1.2 KiB
Bash
Executable File

#!/bin/bash --posix
usage() {
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
printf "Usage: %s\n" \
"$(basename ${0}) -i \"this is some input\" -t \"this is some more example input\"
--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)
echo "${1}"
;;
-t | --test)
echo "${1}"
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
exit 1
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
}
parse_args "$@"