Skip to content

Instantly share code, notes, and snippets.

@ravecat
Last active December 14, 2020 22:46
Show Gist options
  • Save ravecat/d5b70b66316a4cded2f7182ec5a77aa8 to your computer and use it in GitHub Desktop.
Save ravecat/d5b70b66316a4cded2f7182ec5a77aa8 to your computer and use it in GitHub Desktop.
bash
function get_help () {
cat << EOF
Help
EOF
}
function handle_kwarg () {
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
echo $2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
get_help
exit 0 ;;
# parse flags, bool parameters
-f|--flag)
MY_FLAG=0
shift ;;
# parse parameters with values
-o|--output)
TEST=$(handle_kwarg $1 $2)
shift 2 ;;
-*|--*)
echo "Error: Unsupported flag $1" >&2
exit 1 ;;
*)
POSITIONAL+=("$1")
shift ;;
esac
done
# restore positional parameters
set -- "${POSITIONAL[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment