Bash_Scripts/Templates/Functions/Arg-Parsing.bash

72 lines
1.5 KiB
Bash
Raw Normal View History

2021-07-28 09:32:08 -05:00
#!/bin/bash --posix
2021-08-04 01:21:11 -05:00
# Copyright 2021, Price Hiller - All Rights Reserved
#
# Copying, distribution, usage, or modification of this file or the following source code, via any
# method is strictly prohibited without the explicit written consent of Price Hiller (philler3138@gmail.com)
# The following file contains proprietary and confidential content
#
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)
echo "${2}"
;;
-t | --test)
echo "${2}"
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
;;
*) # 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 "$@"