Creating autocomplete script with sub commands

前端 未结 1 1631
难免孤独
难免孤独 2021-02-04 07:35

I\'m trying to create an autocomplete script for use with fish; i\'m porting over a bash completion script for the same program.

The program has three top level commands,

1条回答
  •  一整个雨季
    2021-02-04 07:53

    I guess you are aware that return 0 means true and that return 1 means false?

    From your output it looks like your needs_command function is not working properly, thus showing bar even when it has subcommands.

    I just tried the following code and it works as expected:

    function __fish_prog_needs_command
      set cmd (commandline -opc)
      if [ (count $cmd) -eq 1 -a $cmd[1] = 'prog' ]
        return 0
      end
      return 1
    end
    
    function __fish_prog_using_command
      set cmd (commandline -opc)
      if [ (count $cmd) -gt 1 ]
        if [ $argv[1] = $cmd[2] ]
          return 0
        end
      end
      return 1
    end
    
    complete -f -c prog -n '__fish_prog_needs_command' -a bar
    
    complete -f -c prog -n '__fish_prog_needs_command' -a foo
    complete -f -c prog -n '__fish_prog_using_command foo' -a a
    complete -f -c prog -n '__fish_prog_using_command foo' -a b
    complete -f -c prog -n '__fish_prog_using_command foo' -a c
    
    complete -f -c prog -n '__fish_prog_needs_command' -a baz
    

    Output from completion:

    ➤ prog 
    bar  baz  foo
    ➤ prog foo 
    a  b  c
    ➤ prog foo
    

    Is this what you want?

    0 讨论(0)
提交回复
热议问题