如何使用给定的参数集完成 GNU 长选项?

jar*_*rno 5 gnu command-line-arguments bash-completion

GNU建议使用 --name=value语法为 long 选项传递参数。它使长选项能够接受本身是可选的参数。

假设您有一组完整的可能参数。您如何为这样的选项编写 bash 完成代码?我希望完成在完成一个明确的参数时添加空间,但不是之前。

jar*_*rno 1

这是我为完成虚构命令代码中给出的 GNU 选项而编写的模板代码gnu-options

不带参数的选项在 array 中定义opts。可能接受参数的选项在关联数组中定义args。请注意,正如with-args0两者中出现的那样,它是一个带有可选参数的选项。

该脚本甚至支持不包含“=”的情况$COMP_WORDBREAKS,但显示的完成时间更长。

# 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
Run Code Online (Sandbox Code Playgroud)