How to bash complete a GNU long option with given

2020-03-26 06:18发布

问题:

GNU advises to use --name=value syntax for passing argument for long option. It enables a long option to accept an argument that is itself optional.

Suppose you have a complete set of possible arguments. How do you write a bash completion code for such an option? I want the completion to add space when it completes an unambiguous argument, but not before.

回答1:

Here is the template code I wrote for completing GNU options given in the code for imaginary command gnu-options.

Options that do not take arguments are defined in array opts. Options that do possibly take argument are defined in associative array args. Note that as with-args0 appears in both it is an option with optional argument.

The script supports even case where $COMP_WORDBREAKS does not include '=', but the shown completions are longer then.

# Hack for given strings $2,$3,... possibly being in $1 and $COMP_WORDBREAKS
# Only the part after each match is listed as a completion.
# Run 'shopt -s extdebug; declare -F __ltrim_colon_completions; shopt -u extdebug'
# to see location for the respective function for colon only.
__ltrim_completions ()
{
    local cur=$1; shift
    while [[ ${1+x} ]]; do
        if [[ "$cur" == *$1* && "$COMP_WORDBREAKS" == *$1* ]]; then
            local x_word=${cur%$1*}$1
            local i
            for i in ${!COMPREPLY[*]}; do
                COMPREPLY[$i]=${COMPREPLY[$i]#"$x_word"}
            done
        fi
        shift
    done
} 


_gnu_options()
{
    local IFS=$'\n' # needed for handling trailing space of some options and all arguments
    local cur prev words cword split # needed by _init_completion()
    local opts i prefix= wordlist
    local -A args=()
    # Do not treat = as word breaks even if they are in $COMP_WORDBREAKS:
    # Split option=value into option in $prev and value in $cur
    _init_completion -s || return

    # DEFINE OPTIONS THAT DO NOT TAKE AN ARGUMENT HERE:
    opts=(with-args0 option0 option1 par param)
    # DEFINE THE OPTIONS WITH ARGUMENTS HERE:
    args=([with-args0]= [with-args1]=$'arg10\narg11')
    args[with-args2]=\
'arg=20
arg=21
var=22
argx'
    args[with-args3]=

    for i in ${!args[*]}; do
        if [[ $prev = --$i ]]; then
            local j dobreak=
            [[ $split == false ]] && {
                # equal sign not used; check, if argument is optional.
                for j in ${opts[*]}; do [[ $i == $j ]] && { dobreak=t; break; } done
            }
            [[ $dobreak ]] && break
            [[ "$COMP_WORDBREAKS" != *=* && $split == true ]] && prefix="--$i="
            if [[ ${args[$i]} ]]; then
                COMPREPLY=( $( compgen -P "$prefix" -W "${args[$i]}" -- "$cur" ) )
                __ltrim_completions "$cur" =
            else
                case $i in
                    with-args0)
                        # expand file/directory name.
                        COMPREPLY=( $( compgen -P "$prefix" -A file -- "$cur" ) )
                        compopt -o filenames
                        ;;
                    *)
                        COMPREPLY=()
                        ;;
                esac
            fi
            return 0
        fi
    done

    wordlist=()
    for i in ${opts[*]}; do wordlist+=("--$i "); done
    for i in ${!args[*]}; do wordlist+=("--$i="); done
    COMPREPLY=( $( compgen -W "${wordlist[*]}" -- "$cur" ) )
    compopt -o nospace
} && complete -F _gnu_options gnu-options