Last active
December 19, 2021 15:27
-
-
Save jstine35/0d49e3b3b9343c3bb1f490946479393d to your computer and use it in GitHub Desktop.
Bash Positional Option Parsing with rvalue assignment
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
#!/bin/bash | |
POSITIONAL=( ) | |
for item; do | |
rvalue="${item#*=}" | |
[[ "$item" == --help ]] && SHOWHELP=1 && continue | |
[[ "$item" == --force ]] && FORCE=1 && continue | |
[[ "$item" == --force=* ]] && FORCE=$rvalue && continue | |
[[ "$item" == --* ]] && { | |
>&2 echo "unrecognized option: $item" | |
CLI_ERROR=1 | |
continue | |
} | |
POSITIONAL+=( "$item" ) | |
done | |
set -- "${POSITIONAL[@]}" | |
if (( SHOWHELP )); then | |
echo "" | |
exit 0 | |
fi | |
(( CLI_ERROR )) && exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment