How to bash complete a GNU long option with given set of arguments?
Asked Answered
B

1

5

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.

Beaufort answered 29/9, 2019 at 15:31 Comment(6)
could you elaborate a bit more what's your question?Manyplies
@Manyplies There is similar question hereBeaufort
--name= and --name='' and --name '' are equivalent, so in that sense --name=value syntax does not offer much advantage.Beaufort
Oh, in contrary to previous comment, for GNU sort --check and --check=diagnose-first are equivalent; you can not give argument for the option like --check diagnose-first. I guess that is what they mean about optional arguments in the GNU manual page.Beaufort
@Manyplies I you were wondering about optional arguments, comments above may enlighten. I have also updated my answer accordingly.Beaufort
A minimal answer hereEphialtes
B
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
Beaufort answered 17/10, 2019 at 21:42 Comment(2)
Note that there are some issues in completing, if you use quoting for arguments or escaping for pre-defined arguments.Beaufort
Still, if you use getopt to parse the options, --with-args0 and --with-args0= give same output i.e. null argument for the option. So I think options with optional arguments are mostly informative, and also help to make backwards compatible code, if you later want to add argument to an option.Beaufort

© 2022 - 2024 — McMap. All rights reserved.