Search code examples
zshcompletion

Fix zsh _arguments options (--whatever) completion after non-options (noDashes) input


I would like to allow completion for --flags after other input in my gradle completion script, but _arguments seems to require that

specs that describe option flags must precede specs that describe non-option ("positional" or "normal") arguments of the analyzed line (from the zsh completion docs)

In other words: command foo --o[TAB] does nothing, but command --o[TAB] works fine. Is there any way to configure _arguments or do I need to use other control functions?

NOTE: Separate completion functions do not seem like an option in my case as the inputs are not in a fixed list (gradle tasks are arbitrary and multiple can be specified, gradle myfoo mybar --o[TAB] must work).


Solution

  • I was able to solve this with this commit at least to allow options after 1 task is specified.

    The trick is to set the state within _arguments with :->statename, reset the context to the next word, and provide a wildcard matcher that matches non-command words and use _arguments again.

    There's almost certainly a way to allow options specified after an arbitrary number of words and avoid some duplication, but this is a workable start.