-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for auto-completion #1
Comments
I made in the past some experiment for automatic generation of completion function for zsh. That can give ideas or be a base of code: https://gist.github.com/bobot/9989037 |
For restarting the conversation:
Since one should take into account the --help command and not do any initialization, a specific hidden command line argument is possible
I think two mechanisms must be provided for keeping the simple case simple and yet provide way for complicated case:
Is it really needed? The completion code must be put in a different directory for each shell. So they can be different. |
Thanks for these comments. They are not being ignored. It's just that I have too much other things in my head and todo at the moment. |
A couple of quick thoughts triggered by another discussion. I agree that hidden (though of course documented!) command line switch seems the best bet, but how about making it one that would be mostly invalid for normal command line syntax? So |
No, whatever you'll find it could be a legitimate positional argument if appropriately quoted and end up confusing and limiting the end-user. If you use a regular command line option you never limit the end-user's input capabilities, you only constrain the design space of the developer who can't use it in its program. |
I would already be happy with any |
0install does completion with multiple shells (bash, fish, zsh). There are some scripts that get installed in the shell's config: https://github.com/0install/0install/tree/master/share These invoke There are some Options names and subcommands get completed automatically, but there's lots of special-case code for option and argument values. This was about the first OCaml code I ever wrote, so probably a bit messy ;-) But it might flag up some nasty edge-cases, e.g. https://github.com/0install/0install/blob/master/ocaml/completion.ml#L197 |
Coming late to this conversation, but I thought I'd add that cmdliner's support for multiple subcommands is really useful, and if shell-completion is added, it would be good if it interacted well with multiple subcommands. Just 2c. |
This issue annoys me very much because after all these years I still don't really know what to do about it. The only step that was taken is to reserve the Today while washing the dishes I thought maybe the best way to try to move on this is that when cmdliner is invoked with Maybe then people can try to go on to experiment developing generic tools taking that data and further annotation to instruct how to complete given arguments (e.g. on This means to side step the problem completely and not try to give any support for it in |
In terms of the output formats, it might be useful to have something which is "trivially" parsed in a shell-script - just thinking for example of src/state/complete.sh in opam, which at present greps the manpages. |
My two cents, would be to version this output format. I hesitate if the version should be selected in the code or on the command line. The first version
Should cmdliner provide a command for generating those files for the common cases and shells? |
Given the reactions I think my proposal was misunderstood. The idea is not to help you write these terrible shell runes directly by allowing you to invoke My proposal is to dump all the static information (including doc strings) If something good stabilizes I'm not against integrating these tools in the |
Ah I see. If things are static, why do you prefer a structured format instead of an OCaml API? I though the |
I don't know, that could be an idea. But I have the impression that an API
But maybe indeed simply providing introspection on
I think Before being able to |
Your comment actually made me think of something else. To simplify let's just start with option/command names. No option values and no positional argument values. What about using the
I think this would be pretty easy to do and it wouldn't require anything at all from current cmdliner users. Would that be useful for writing these completion script ? Would that enable to write them in a generic manner ? We might actually need two options, one for denoting the insertion point right after a printable char and one for after whitespace for later trying to complete option arguments and positional argument:
(Of course |
It could be simpler if the empty string is used when completing after a whitespace.
It would be clearly helpful, it is similar but simpler than 0install method pointed by @talex5 .
It seems that in 0install they have to handle in OCaml some differences between the shell https://github.com/0install/0install/blob/master/src/cli/completion.ml#L185 . But it could be only in more complicated cases. |
Thanks for your input @bobot.
If that works for the completion scripts, seems even better. (I certainly need to force myself to read and understand at least one of these shell completion systems at some point). So basically the
Yes, I don't expect shells to behave in a consistent way otherwise this issue would likely be resolved. When I say in a generic manner, I mean can we write one completion script per shell for all cmdliner tools (leaving aside for now tool specific completions like e.g. package names in opam). |
So for the past two hours I tried to understand how I understand I can simply register my own function
So I thought as a first step I wanted to try to provide a But mind you I couldn't find out which arguments are actually given to the function and how you are supposed to return your result – except by delegating to other obscure utility functions with insane syntaxes. These shell people really have a problem with the notion of function. Another thing I found out is that, the OCaml
(This relies on parsing the output of This leaves me a bit wondering whether the more complex mecanism planned above is worth doing w.r.t. to just dump the options of a command and their doc string on an invocation |
So after a few more hours in this insanity I think this was the document I was looking for. Trying to understand this document leaves you wondering whether this complexity is actually needed or whether that is just bad design. In any case the rough idea is that the generic completion script will look like this: function _cmdliner_generic {
local comps
words[$CURRENT]="--cmdliner-complete=${words[$CURRENT]}"
comps=("${(@f)$(eval ${words})}")
compadd -a comps
} Basically this gets the current cli inserts I'll try that as a first step but it is likely too primitive. We want more meaningful exit codes and/or structured output. In particular for:
Basically 2+3 are for "dynamic" completions (e.g opam package names). Here again moving slowly I think it's preferable if we try to solve that in completion scripts themselves. An idea is that in these cases we output the position argument or option value For example trying to complete:
would lead the generic script to invoke
which would output (as per current
which could be replaced in an extension of Maybe more context is actually needed, e.g. which sub(sub)command, so it would rather be |
Seems like the Command library from Jane street support this auto-completion feature: |
Merge upstream 1.1.1
Any update on this issue @dbuenzli ? |
Can we get at least a v0 doing some basic autocompletion. |
@aryx We have a WIP completion PR for Dune: ocaml/dune#6377 Cmdliner is vendored in vendor/ and there are some modifications to allow for completion that could be useful. |
It seems a lot of people want cmdliner to help with auto-completion which seems a rather natural request. There are already quite a few things that could help inside (e.g. optional arguments can be specified by a prefix).
'a converter
or do we add (OCaml) optional arguments to command line argument combinators ? The problem is, is there already sufficient context at that point for the thing to be usefull ?The text was updated successfully, but these errors were encountered: