mirror of https://github.com/spf13/cobra.git
Add flaghash to bash_completion to keep flag value
This commit is contained in:
parent
2bd8a730ae
commit
60b8213eba
|
@ -126,8 +126,10 @@ __handle_flag()
|
|||
|
||||
# if a command required a flag, and we found it, unset must_have_one_flag()
|
||||
local flagname=${words[c]}
|
||||
local flagvalue
|
||||
# if the word contained an =
|
||||
if [[ ${words[c]} == *"="* ]]; then
|
||||
flagvalue=${flagname#*=} # take in as flagvalue after the =
|
||||
flagname=${flagname%%=*} # strip everything after the =
|
||||
flagname="${flagname}=" # but put the = back
|
||||
fi
|
||||
|
@ -136,6 +138,15 @@ __handle_flag()
|
|||
must_have_one_flag=()
|
||||
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
|
||||
if __contains_word "${words[c]}" "${two_word_flags[@]}"; then
|
||||
c=$((c+1))
|
||||
|
@ -145,7 +156,6 @@ __handle_flag()
|
|||
fi
|
||||
fi
|
||||
|
||||
# skip the flag itself
|
||||
c=$((c+1))
|
||||
|
||||
}
|
||||
|
@ -202,6 +212,7 @@ func postscript(out *bytes.Buffer, name string) {
|
|||
fmt.Fprintf(out, "__start_%s()\n", name)
|
||||
fmt.Fprintf(out, `{
|
||||
local cur prev words cword
|
||||
declare -A flaghash
|
||||
if declare -F _init_completion >/dev/null 2>&1; then
|
||||
_init_completion -s || return
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue