mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Add flaghash to bash_completion to keep flag value
This commit is contained in:
parent
2bd8a730ae
commit
60b8213eba
1 changed files with 12 additions and 1 deletions
|
@ -126,8 +126,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
|
||||||
|
@ -136,6 +138,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))
|
||||||
|
@ -145,7 +156,6 @@ __handle_flag()
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# skip the flag itself
|
|
||||||
c=$((c+1))
|
c=$((c+1))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -202,6 +212,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
|
||||||
|
|
Loading…
Reference in a new issue