2

My goal is to generate zsh completions for a program I am writing. One option would be to write a lengthy completion file that lists all arguments and options, but since I often add arguments and options I would love zsh to handle the generation of the completions by itself. My program has a --help argument that provides output similar to this:

$ myProgram --help
Usage: myProgram
Lorem ipsum

Possible Arguments:
    --argOps                 Description of argOps that takes options. Possible Values: [foo, bar, baz]
    --argNoOps               Description of argNoOps.
    --argFiles               Description of argFiles that says it takes a file path. 

myProgram documentation is available via: ...

From this zsh is able to generate something in the general direction of what I want with the following in my .zshrc:

autoload -U compinit && compinit
compdef _gnu_generic myProgram    # parses arguments from 'myProgram --help'

However, I want more: If I for example write myProgram --argOps and hit tab zsh should present me with the options for argOps namely foo bar and baz and also allow completion on those. In theory, this should be possible by parsing the output of myProgram --help since all the information is available there. I tried writing a custom completion script following the documentation. There, I noticed the subsection "Deriving spec forms from the help output" which talks about the form _arguments -- that allows pattern matching on the output of --help. However, it seems that one can only match the arguments themselves and not the description texts?

If I could access the descriptions I would not mind writing weird regex parsers, but I am a bit lost in how to access the descriptions.

Note: Since this is my own program I can completely change the output of --help if necessary.

Seriously
  • 884
  • 1
  • 11
  • 25
  • Rather than generating the completions from the help output, have your program itself output the completions using `myProgram --zsh-completions`. Then your program is responsible for generating two different outputs from the same underlying data structure that contains the necessary information. – chepner May 23 '20 at 20:26
  • I like the idea and am tempted to use it as a temporary solution, but I still would like to know how to achieve the above. – Seriously May 24 '20 at 10:23

0 Answers0