Skip to content

Instantly share code, notes, and snippets.

@hilotech
Last active August 29, 2015 14:18
Show Gist options
  • Save hilotech/f9664a0e77644b479e33 to your computer and use it in GitHub Desktop.
Save hilotech/f9664a0e77644b479e33 to your computer and use it in GitHub Desktop.
bash(>3) : getopts long options
# Usage: local -A __opts=`_getOptsArray --hoge=fuga --age=uge`
# to get the value: ${__opts["hoge"]}
function _getOptsArray() {
local __name=''
local __value=''
local __declaration=''
local -A __opts
for OPT in "$@"
do
__name=${OPT%%=*}
__name=${__name#--}
[ -z "$__name" ] && continue
__value=${OPT#*=}
__declaration="$__declaration[\"$__name\"]=\"$__value\" "
done
__declaration="( $__declaration)"
echo $__declaration
}
function main() {
local -A __opts=`_getOptsArray --hoge=fuga --age=uge`
echo ${!__opts[@]}
echo ${__opts[@]}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment