Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 121c58be authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Daniel Borkmann
Browse files

tools: bpftool: deal with options upfront



Remove options (in getopt() sense, i.e. starting with a dash like
-n or --NAME) while parsing arguments for bash completions.  This
allows us to refer to position-dependent parameters better, and
complete options at any point.

Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent ef347a34
Loading
Loading
Loading
Loading
+21 −11
Original line number Original line Diff line number Diff line
@@ -153,6 +153,13 @@ _bpftool()
    local cur prev words objword
    local cur prev words objword
    _init_completion || return
    _init_completion || return


    # Deal with options
    if [[ ${words[cword]} == -* ]]; then
        local c='--version --json --pretty --bpffs'
        COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
        return 0
    fi

    # Deal with simplest keywords
    # Deal with simplest keywords
    case $prev in
    case $prev in
        help|hex|opcodes|visual)
        help|hex|opcodes|visual)
@@ -172,20 +179,23 @@ _bpftool()
            ;;
            ;;
    esac
    esac


    # Search for object and command
    # Remove all options so completions don't have to deal with them.
    local object command cmdword
    local i
    for (( cmdword=1; cmdword < ${#words[@]}-1; cmdword++ )); do
    for (( i=1; i < ${#words[@]}; )); do
        [[ -n $object ]] && command=${words[cmdword]} && break
        if [[ ${words[i]::1} == - ]]; then
        [[ ${words[cmdword]} != -* ]] && object=${words[cmdword]}
            words=( "${words[@]:0:i}" "${words[@]:i+1}" )
            [[ $i -le $cword ]] && cword=$(( cword - 1 ))
        else
            i=$(( ++i ))
        fi
    done
    done
    cur=${words[cword]}
    prev=${words[cword - 1]}

    local object=${words[1]} command=${words[2]}


    if [[ -z $object ]]; then
    if [[ -z $object || $cword -eq 1 ]]; then
        case $cur in
        case $cur in
            -*)
                local c='--version --json --pretty --bpffs'
                COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
                return 0
                ;;
            *)
            *)
                COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
                COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
                    command sed \
                    command sed \