Suppress filename/directory completion in fish shell

238 views Asked by At

I am writing a completion script for Hub.

This being my first completion script, I have run into some problems. Taking inspiration from git.fish completion script, here is what I have till now:

# statement starting with 'hub'
function __fish_hub_needs_command
    set cmd (commandline -opc)
    if [ (count $cmd) -eq 1 -a $cmd[1] = 'hub' ]
        return 0
    end
    return 1
end

# statement starting with 'hub COMMAND'
function __fish_hub_using_command
    set cmd (commandline -opc)
    if [ (count $cmd) -gt 1 ]
        if [ $argv[1] = $cmd[2] ]
            return 0
        end
    end
    return 1
end

# help
# show '--help' for every command
complete -f -c hub -n '__fish_hub_needs_command' -a help -d 'Display enhanced git-help(1)'
complete -f -c hub -n 'not __fish_hub_needs_command' -l help -d 'Display enhanced git-help(1)'

# alias
complete -f -c hub -n '__fish_hub_needs_command' -a alias -d 'Shows shell instructions for wrapping git.'
complete -f -c hub -n '__fish_hub_using_command alias' -s s -d 'Output shell script suitable for eval.'

Now, when type hub <tab> it shows me help and alias correctly as possible completions. But after typing hub alias <tab> it shows me a list of directories even when I have provided the -f option there.

What is wrong here? Why is that happening? Is there a way to suppress file/directory completion in such a scenario?

Help would be appreciated. Thank you.

1

There are 1 answers

0
ridiculous_fish On BEST ANSWER

AFAIK it's not possible to totally suppress tab completions. If no command-specific completion is available, you will always get files, under the theory that something is better than nothing.

What the -f flag does is prevent files from being shown alongside your options:

> complete -c test_cmd -a 'alpha beta'
> test_cmd <tab>

you will see files intermixed with alpha and beta. Now:

> complete -f -c test_cmd -a 'gamma'
> test_cmd <tab>

the files are gone because of the -f flag.

In the future this stuff will be subsumed by a docopt-based completion syntax (#478) which should make it more straightforward.