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
Generate subcommand alias #4284
Conversation
018b5b1
to
e71d082
Compare
The failed "CI / Docs" check is not my fault, is it? Possibly related, I've sent this for the |
Per our support policy, changes should go into |
The example binaries were renamed in 89c2b3b, but the commands in them were not, making the generated completion scripts not work (because we use the command name as binary name in the examples).
The derive-based example has a `///` comment on one argument, which ends up as a description for the argument in the generated completion scripts. Let's switch to `//` so the two scripts produce the same output (except for the binary name), so they're easy to compare.
It's useful when testing to have a subcommand in the examples.
I want to add support for completion of arguments for aliased subcommands so it's nice to have an example to test on.
Only zsh includes completion for visible aliases of subcommands. Let's show that in tests.
There seems to be little reason to return early with an empty list when there are no subcommands, instead of going through the loop 0 times and then returning the empty list.
The `text` variable here is clearly never empty, so don't check if it is.
Early in the Bash-completion script, we build up a string that identifies the command or subcommand. When we see the top-level command's name (e.g. `git`) we set the command so far to that value. We do that regardless of where in the argument list it appears. For example, if the argument list is `git diff git`, we set the current command to `git` when run into it the second time. We therefore suggest arguments to the top-level command afterwards, which is not correct. This patch fixes that by also considering the string that identifies the command so far, so we only set the overall command to `git` if the command so far is the empty string. This is actually just a step on the way to getting completion to work for aliases of subcommands. Closes clap-rs#4273
This continues the work started with the fix for clap-rs#4273. There was another bug caused by using the subcommand names without considering their position in the argument list. If the user enters `git diff log <TAB>`, we build up a string that identifies the subcommand. We ended up making the string `git__diff__log` in this case because we appended `__log` without considering the current state. Since `git__diff__log` does not correspond to an actual command, we wouldn't provide any suggestions. This commit restructures the code so we walk subcommands and subsubcommands in `bash.rs`. While walking those, we build up a list containing triples of the parent `$cmd` name (e.g. `git__diff`), the current command's name (e.g. `log`), and the `$cmd` for the current command. We then build the shell script's case arms based on that information. We could instead have fixed clap-rs#4280 by using the second element in the pair returned from `utils::all_subcommands()` (a stringified list of the subcommand path) instead of the first one. However, that would not have helped us solve clap-rs#4265. Closes clap-rs#4280
With the previous fixes for clap-rs#4273 and clap-rs#4280 in place, it's now easy to add support for subcommand aliases, which this commit does. This addresses clap-rs#4265 for Bash.
e71d082
to
33fa7c4
Compare
This is now rebase and ready for |
I think github got confused with the re-targeting; its not kicking off the actions and I'm not finding a way to force it. Will we need to close this PR and create a new one? |
Sure, I don't mind doing that.
By the way, I didn't realize that clap 4 was now released. Congrats! :) I'm working on upgrading my project now. |
If you haven't seen the announcement post, you should check it out as it goes into a lot more detail than the changelog |
This should fix #4273, #4280, and the Bash part of #4265. I haven't started looking at the Fish side or other shells yet.