completion: add __git_get_option_value helper

This function allows to search the commmand line and config files for an option, long and short, with mandatory value. The function would return e.g. for the command line "git status -uno --untracked-files=all" the result "all" regardless of the config option. Signed-off-by: Thomas Braun <thomas.braun@virtuell-zuhause.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Braun committed Jun 10, 2016 at 12:12 UTC 7c599e92aaba4a2ef07784858def25ced8512276
1 file changed +44
contrib/completion/git-completion.bash
+44
@@ -803,6 +803,50 @@ __git_find_on_cmdline ()
803 done
804 }
805
806 +# Echo the value of an option set on the command line or config
807 +#
808 +# $1: short option name
809 +# $2: long option name including =
810 +# $3: list of possible values
811 +# $4: config string (optional)
812 +#
813 +# example:
814 +# result="$(__git_get_option_value "-d" "--do-something=" \
815 +# "yes no" "core.doSomething")"
816 +#
817 +# result is then either empty (no option set) or "yes" or "no"
818 +#
819 +# __git_get_option_value requires 3 arguments
820 +__git_get_option_value ()
821 +{
822 + local c short_opt long_opt val
823 + local result= values config_key word
824 +
825 + short_opt="$1"
826 + long_opt="$2"
827 + values="$3"
828 + config_key="$4"
829 +
830 + ((c = $cword - 1))
831 + while [ $c -ge 0 ]; do
832 + word="${words[c]}"
833 + for val in $values; do
834 + if [ "$short_opt$val" = "$word" ] ||
835 + [ "$long_opt$val" = "$word" ]; then
836 + result="$val"
837 + break 2
838 + fi
839 + done
840 + ((c--))
841 + done
842 +
843 + if [ -n "$config_key" ] && [ -z "$result" ]; then
844 + result="$(git --git-dir="$(__gitdir)" config "$config_key")"
845 + fi
846 +
847 + echo "$result"
848 +}
849 +
850 __git_has_doubledash ()
851 {
852 local c=1