Search code examples
zshzsh-completion

Completing options of the form --<option>:<file> and --<option>:<possible values>


I'm trying to write a completion method for fsharpi (F# interactive), which has options like the following:

--use:<file>              Use the given file on startup as initial input
--debug:{full|pdbonly}    Specify debugging type: full, pdbonly. ('full' is the default and enables attaching a debugger to a running program).
--warn:<n>                Set a warning level (0-5)

I'm guessing this has to be handled with $state similarly to sub-commands, but the documentation is monolithic and the language isn't very descriptive, so I've gotten nowhere with experimentation and by stitching together different examples.

A solution to this would also work for aspell, which uses an equals-sign instead of the colon e.g.

--conf=<str>              main configuration file

Solution

  • This is one of the most common forms of completion, and it can be easily handled by _arguments. Note that literal colons in options can be quoted with a backslash. Here's the code example:

    #compdef command
    
    arguments=(
        '--use\:-:initial input file:_files'
        '--debug\:-:debugging type:(full pbonly)'
        '--warn\:-:warning level:(0 1 2 3 4 5)'
    )
    
    _arguments -S $arguments[@]
    

    Reference: _arguments in official documentation.