From 60b8213eba37c8e71507eb58a37e46034997a066 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Sun, 6 Dec 2015 02:57:45 +0900 Subject: [PATCH] Add flaghash to bash_completion to keep flag value --- bash_completions.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bash_completions.go b/bash_completions.go index 35e8711..848da9c 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -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