title: Creating autocomplete script with sub commands
body: <p>I'm trying to create an autocomplete script for use with fish; i'm porting over a bash completion script for the same program.</p>
  <p>The program has three top level commands, say <code>foo</code>, <code>bar</code>, and <code>baz</code> and each has some subcommands, just say <code>a</code> <code>b</code> and <code>c</code> for each.</p>
  <p>What I'm seeing is that the top level commands auto complete ok, so if I type <code>f</code> I'm getting <code>foo</code> to autocomplete, but then if I hit tab again to see what it's sub commands are, i see <code>foo</code>, <code>bar</code>, <code>baz</code>, <code>a</code>, <code>b</code>, <code>c</code> and it should just be <code>a</code>, <code>b</code>, <code>c</code></p>
  <p>I am using as a reference the <a href="https://github.com/weavejester/fish-git/blob/master/completions/git.fish" rel="nofollow noreferrer">git completion script</a> since it seems to work right.  I am also using the <a href="https://github.com/bobthecow/git-flow-completion/blob/master/git.fish#L44" rel="nofollow noreferrer">git flow script</a> as a reference as well.</p>
  <p>I <em>think</em> this is handled in the git completion script by:</p>
  <pre><code>function __fish_git_needs_command
    set cmd (commandline -opc)
    if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
      return 0
    end
    return 1
  end
  </code></pre>
  <p>Which makes sense, you can only use the completion if there is a single arg to the command, the script itself; if you use that as the condition (<code>-n</code>) for the call to complete on the top level commands, I think the right thing would happen.</p>
  <p>However, what I'm seeing is not the case.  I copied that function over to my script, changed &quot;git&quot; appropriately, and did not have any luck.</p>
  <p>The trimmed down script is as follows:</p>
  <pre><code>function __fish_prog_using_command
    set cmd (commandline -opc)
    set subcommands $argv
    if [ (count $cmd) = (math (count $subcommands) + 1) ]
      for i in (seq (count $subcommands))
        if not test $subcommands[$i] = $cmd[(math $i + 1)]
          return 1
        end
      end
      return 0
    end
    return 1
  end
  
  function __fish_git_needs_command
    set cmd (commandline -opc)
    set startsWith (echo &quot;$cmd[1]&quot; | grep  -E 'prog$')
    # there's got to be a better way to do this regex, fish newb alert
    if [ (count $cmd) = 1 ]
      # Is this always false? Is this the problem?
      if [ $cmd[1] -eq $cmd[1] ]
        return 1
      end
    end
  
    return 0
  end
  
  complete --no-files -c prog -a bar -n &quot;__fish_git_needs_command&quot;
  
  complete --no-files -c prog -a foo -n &quot;__fish_git_needs_command&quot;
  
  complete --no-files -c prog -a a -n &quot;__fish_prog_using_command foo&quot; 
  complete --no-files -c prog -a b -n &quot;__fish_prog_using_command foo&quot; 
  complete --no-files -c prog -a c -n &quot;__fish_prog_using_command foo&quot; 
  
  complete --no-files -c prog -a baz -n &quot;__fish_git_needs_command&quot; 
  </code></pre>
  <p>Any suggestions on how to make this work is much appreciated.</p>