luarocks / argparse

Feature-rich command line parser for Lua
https://github.com/luarocks/argparse
Other
58 stars 8 forks source link

Fish completions: sub-subcommands are completed for main command #7

Closed p-ouellette closed 5 years ago

p-ouellette commented 5 years ago
~/s/a/spec > ./comptest <TAB>
add                   (Add a rock to a server)  i                    (Install a rock)
admin   (Rock server administration interface)  install              (Install a rock)

(add is a subcommand of the admin command)

p-ouellette commented 5 years ago

An edited version that works: It also fixes ./comptest --completion bash <TAB> not completing subcommands (__fish_use_subcommand returns false if it sees anything not starting with -).

function __fish_comptest_print_command
    set -l cmdline (commandline -poc)
    set -e cmdline[1]
    set -l cmd
    for arg in $cmdline
        set -e cmdline[1]
        switch $arg
            case help
                set cmd $cmd help
                break
            case completion
                set cmd $cmd completion
                break
            case install i
                set cmd $cmd install
                break
            case admin
                set cmd $cmd admin
                for arg in $cmdline
                    set -e cmdline[1]
                    switch $arg
                        case help
                            set cmd $cmd help
                            break
                        case add
                            set cmd $cmd add
                            break
                        case remove
                            set cmd $cmd remove
                            break
                    end
                end
                break
        end
    end
    echo "$cmd"
end

function __fish_comptest_using_command
    test (__fish_comptest_print_command) = "$argv"
    and return 0
    or return 1
end

function __fish_comptest_seen_command
    string match -q "$argv*" (__fish_comptest_print_command)
    and return 0
    or return 1
end

complete -c comptest -n '__fish_comptest_using_command' -xa 'help' -d 'Show help for commands'
complete -c comptest -n '__fish_comptest_using_command' -xa 'completion' -d 'Output a shell completion script'
complete -c comptest -n '__fish_comptest_using_command' -xa 'install' -d 'Install a rock'
complete -c comptest -n '__fish_comptest_using_command' -xa 'i' -d 'Install a rock'
complete -c comptest -n '__fish_comptest_using_command' -xa 'admin' -d 'Rock server administration interface'
complete -c comptest -s h -l help -d 'Show this help message and exit'
complete -c comptest -l completion -xa 'bash zsh fish' -d 'Output a shell completion script for the specified shell'
complete -c comptest -s v -l verbose -d 'Set the verbosity level'
complete -c comptest -s f -l files -r -d 'A description with illegal "\' characters'

complete -c comptest -n '__fish_comptest_using_command help' -xa 'help completion install i admin'
complete -c comptest -n '__fish_comptest_seen_command help' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command completion' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command install' -s h -l help -d 'Show this help message and exit'
complete -c comptest -n '__fish_comptest_seen_command install' -l deps-mode -xa 'all one order none'
complete -c comptest -n '__fish_comptest_seen_command install' -l no-doc -d 'Install without documentation'

complete -c comptest -n '__fish_comptest_using_command admin' -xa 'help' -d 'Show help for commands'
complete -c comptest -n '__fish_comptest_using_command admin' -xa 'add' -d 'Add a rock to a server'
complete -c comptest -n '__fish_comptest_using_command admin' -xa 'remove' -d 'Remove a rock from  a server'
complete -c comptest -n '__fish_comptest_seen_command admin' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_using_command admin help' -xa 'help add remove'
complete -c comptest -n '__fish_comptest_seen_command admin help' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_using_command admin add' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_using_command admin remove' -s h -l help -d 'Show this help message and exit'