Add flaghash to bash_completion to keep flag value

This commit is contained in:
Kenjiro Nakayama 2015-12-06 02:57:45 +09:00
parent 1c44ec8d3f
commit 97cc84b0e4

View file

@ -124,8 +124,10 @@ __handle_flag()
# if a command required a flag, and we found it, unset must_have_one_flag() # if a command required a flag, and we found it, unset must_have_one_flag()
local flagname=${words[c]} local flagname=${words[c]}
local flagvalue
# if the word contained an = # if the word contained an =
if [[ ${words[c]} == *"="* ]]; then if [[ ${words[c]} == *"="* ]]; then
flagvalue=${flagname#*=} # take in as flagvalue after the =
flagname=${flagname%%=*} # strip everything after the = flagname=${flagname%%=*} # strip everything after the =
flagname="${flagname}=" # but put the = back flagname="${flagname}=" # but put the = back
fi fi
@ -134,6 +136,15 @@ __handle_flag()
must_have_one_flag=() must_have_one_flag=()
fi fi
# keep flag value with flagname as flaghash
if [ ${flagvalue} ] ; then
flaghash[${flagname}]=${flagvalue}
elif [ ${words[ $((c+1)) ]} ] ; then
flaghash[${flagname}]=${words[ $((c+1)) ]}
else
flaghash[${flagname}]="true" # pad "true" for bool flag
fi
# skip the argument to a two word flag # skip the argument to a two word flag
if __contains_word "${words[c]}" "${two_word_flags[@]}"; then if __contains_word "${words[c]}" "${two_word_flags[@]}"; then
c=$((c+1)) c=$((c+1))
@ -143,7 +154,6 @@ __handle_flag()
fi fi
fi fi
# skip the flag itself
c=$((c+1)) c=$((c+1))
} }
@ -199,6 +209,7 @@ func postscript(out *bytes.Buffer, name string) {
fmt.Fprintf(out, "__start_%s()\n", name) fmt.Fprintf(out, "__start_%s()\n", name)
fmt.Fprintf(out, `{ fmt.Fprintf(out, `{
local cur prev words cword local cur prev words cword
declare -A flaghash
if declare -F _init_completion >/dev/null 2>&1; then if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -s || return _init_completion -s || return
else else