0

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
Community
  • 1
  • 1
Ludvig
  • 3
  • 3
  • This is actually the simplest form of completion. What you need is `_arguments`. I suggest the tutorial from zsh-users: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org, which is at least a little bit more understandable. Examples also help a lot (abound in the linked repo). – 4ae1e1 Oct 28 '15 at 18:53
  • @4ae1e1 I'm not so sure that this is true. What I'm trying to have it complete is firstly `fsharpi TAB` to `fsharpi --use:` and then `fsharpi --use:TAB` to `fsharpi --use:script.fsx`. – Ludvig Oct 28 '15 at 19:04
  • "I'm not so sure that this is true." What is not true? I suggested `_arguments` and a tutorial. I didn't make any falsifiable claims, except that this is easy (which is relative and not falsifiable). – 4ae1e1 Oct 28 '15 at 19:11
  • What I tried to say was that I didn't think `_arguments` could complete something with a colon between the options and the actions. Maybe `_arguments` is more complex than I thought, but I'll need an exact example otherwise I'm still not getting it. – Ludvig Oct 28 '15 at 19:22
  • Just quote colons with backslash. Anyway, I'll post an exact answer. – 4ae1e1 Oct 28 '15 at 19:29

1 Answers1

0

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.

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
  • Works like a charm :) thanks! I knew you could escape the colons with backslash, but I didn't know that ` – Ludvig Oct 28 '15 at 19:42
  • I've linked to the official documentation. Look at the various forms of `optspec`. It's very flexible. – 4ae1e1 Oct 28 '15 at 19:44
  • Oh i get it now! The '-' at the end of the name of the option (as in `--use\:-`) makes the first argument come directly after the option (no space). That's just what I was missing. – Ludvig Oct 28 '15 at 19:55