-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Auto generate bash completions #376
Comments
Copying comments over:
|
|
@kamalmarhubi I am quite confused on the "completion mode" that you speak of. AFAIK, shells (bash, zsh...) cannot get runtime information of programs that are not yet run. They do completion by predefined completion functions. For example, here is bash completion for git. @kbknapp Since generating completion file is one time process, it'd be more appropriate if we could do it on compile time. Nobody wants an overhead of generating completion file every time a program is run ;) |
I have no idea how they work or if they're appropriate, but it sounds like a possible use for features in the
|
@sru: note that the completion file calls |
Equivalents from a few other languages' ecosystems:
|
I just noticed that clap is already using features for a couple of extras: http://kbknapp.github.io/clap-rs/clap/index.html#optional-dependencies--features |
@sru exactly my point :) Which is kind of my thoughts on either using some sort of syntax extension or the like to generate this at compile time, or potentially allowing this completion to be generated at runtime only in via features like @kamalmarhubi mentions. This would allow similar to how we run clippy, just a one time build/run to generate the completions functions, then you build without the feature enabled for actual use. |
Just FYI:
fish is theorically able to get the documentation from the manual ( |
Hey. So.. I am managing a fairly decent sized clap based app that actually has a custom bash completions script. It wasn't actually to bad to write a completion script for a clap app (though still not something I'd recommend) because clap it has a quite rigid structure in how arguments and subcommands are parsed. I think my current script could have easily been generated by clap provided one or two extra instructions were added to clap. Sorry for the wall of text. Suggested completion algorithmThe bash side, the part that will be generated and passed to
And you would iterate 4 and 5 until you are at the bottom of the tree for this. Think this kind of recursion would work. I have a proof of concept up to, but not including this point (recursion), and it feels pretty great. All the lists mentioned would be compiled into the bash script as variables, or as scripts to be called using extensions: Suggested ExtensionsSome choices to be made here to make this work. We could easily allow: Static CompletionsSpecial suggestions could be populated from a list that a user provides to clap. let args = App::new("foo")
.subcommand(SubCommand::with_name("fetch")
.arg(Arg::with_name("components")
.completions(vec!["yajl", "zlib"]))) which would be nice, easy to implement (just dump the list straight into the completion script as a variable), but it's not very flexible. Dynamic CompletionsA dynamic option would be more useful if if the list is dependent on some outside state that needs IO to verify. E.g., if a command like I can see two options from clap's side that could provide this: 1. Inline bashEasy to implement (just dump the function straight into the completion script to be eval'd), but it's not a particularly elegant solution. let args = App::new("foo")
.subcommand(SubCommand::with_name("fetch")
.arg(Arg::with_name("components")
.completer("find ~/.foo/cache/ -maxdepth 1 -mindepth 1 -type d -printf \"%f \"") 2. Allow custom functionsAllow completer functions that do some computation or IO: fn fetch_completer() -> Result<(), Vec<String>> {
// provide a list from some IO operation
}
let args = App::new("foo")
.subcommand(SubCommand::with_name("fetch")
.arg(Arg::with_name("components")
.completer(fetch_completer) clap could then expose that function secretly as a hidden subcommand, say ----In my current app, I am kind of using a combination of the two already:
----So.. Does this approach seem sensible? Anyone see better approaches? |
Excellent! Thanks for taking the time to put all this together! I'm still on vacation for the next two days, but let me do some looking into this and I'll post back here with what I find. I'd be excited if we can get this into |
Cool. Enjoy your vacation :) I have a rough prototype of something working here that maybe you could have a look at when you get back. A very hacky thing, but it gets some basics done. |
I've finally began working on this issue! The method I'm going with uses a build.rs script and outputs a completion file which from initial testing works great even with subcommands and aliases. It's not perfect as it's "dumb" about what positional args are left but as an initial approach I'm happy. Once I do a little more testing and tweaking I'll put in the PR. Edit, to be clear this also works by only completing on the valid options for each individual subcommand ;) |
Awesome. I had no idea how to actually generate the completion script without hooking into the arg chain at the very end when it was fully populated. Glad you have found a sensible way. I'd be happy to test it out and look for some edge cases when you get it in a sensible state 😄 |
I went ahead and put you 2.7.0 since I had limited time to work on this over the past few days. This issue is next in the hopper. |
I just finished implementing this. Putting in the PR now. As a sample, I built |
Very nice. Just tested it on my app and got 400 line bash.sh that works pretty well 👍 Is there a way to provide dynamic completion values to the script here? E.g. like completor fn above. |
I like how it suggests positional arguments as Found one issue: |
I plan on adding aliases and possible values next prior to updating on crates.io. There isn't a way to use a completer fn yet, but it should be addable once I have this base system complete. |
Aliases and possible values are now completable. Once I get home tonight I'll update the version on crates.io |
Are there plans for zsh completion? |
Thanks to @kamalmarhubi for reminding me about this and making some suggestions.
The text was updated successfully, but these errors were encountered: