Last active
May 20, 2021 19:04
-
-
Save perfecto25/075277ea9cca5fd9e008b811f73e44aa to your computer and use it in GitHub Desktop.
Bash arg parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_params(){ | |
PARAMS="" | |
while (( "$#" )); do | |
case "$1" in | |
## RESET - boolean | |
-r|--reset) | |
RESET=true | |
shift | |
;; | |
## AGE | |
-a|--age) | |
if [ -n "${2}" ] && [ ${2:0:1} != "-" ]; then | |
AGE=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
## NAME | |
-n|--name) | |
if [ -n "${2}" ] && [ ${2:0:1} != "-" ]; then | |
NAME=${2} | |
shift 2 | |
else | |
echo -e "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-*|--*=) # unsupported flags | |
echo -e "Error: Unsupported flag $1" >&2 | |
print_usage | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS="$PARAMS $1" | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
eval set -- "${PARAMS}" | |
# sanity checks | |
[[ -z "${AGE}" && -z "${NAME}" ]] && { echo "You must provide either a Name or Age '--name joe or --age 25'. exiting.."; exit 1; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment