Created
October 4, 2022 07:24
-
-
Save juliyvchirkov/dddbc9faea794bd5f85ed6c9f4c6e761 to your computer and use it in GitHub Desktop.
bash: long options parser
This file contains hidden or 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
## | |
# long option format: --option=value | |
# | |
# usage: value="$(getOptionValue option "${@}")" | |
# script.sh --pidfile=/run/pidfile.pid ➙ pidfile="$(getOptionValue pidfile "${@}")" | |
## | |
getOptionValue() { | |
local option | |
[[ $# -gt 1 ]] || return | |
option="--${1}=" | |
shift | |
while [[ $# -gt 0 ]]; do | |
[[ "${1:0:${#option}}" = "${option}" ]] && { | |
printf "%s" "${1:${#option}}" | |
return | |
} | |
shift | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment