Last active
April 29, 2021 19:14
-
-
Save dekarrin/d0ce500bec820d340458a0bf32b15a49 to your computer and use it in GitHub Desktop.
Bash argument parser as specified by https://medium.com/@Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
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
# parse args | |
PARAMS="" | |
while (( "$#" )); do | |
case "$1" in | |
-t|--telegraf-conf-dir) | |
telegraf_conf_install_dir="$2" | |
shift 2 | |
;; | |
-h|--help) | |
echo "usage: $0 [<flags>]" | |
echo "Installs app onto a centos system" | |
echo "" | |
echo "Flags:" | |
echo ' -h, --help Show this help and exit' | |
echo ' -t, --telegraf-conf-dir "path" Set install directory of telegraf config. Default is /etc/telegraf/telegraf.d' | |
echo ' -- Flag end marker; no flags after this will be parsed' | |
exit 0 | |
;; | |
--) # end argument parsing | |
shift | |
break | |
;; | |
-*|--*=) # unsupported flags | |
echo "Error: Unsupported flag $1" >&2 | |
echo "Invoke with -h or --help for help" >&2 | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS="$PARAMS $1" | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
eval set -- "$PARAMS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment