Skip to content
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 #4289

Merged
merged 10 commits into from Sep 29, 2022
Merged

Conversation

martinvonz
Copy link
Contributor

This should fix #4273, #4280, and the Bash part of #4265. I haven't started looking at the Fish side or other shells yet.

This is a new attempt after checks for #4284 got into a weird state.

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.
@epage epage merged commit cdaa8a1 into clap-rs:master Sep 29, 2022
@epage
Copy link
Member

epage commented Sep 29, 2022

Thanks!

clap_complete 4.0.2 is now out with this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bash completion for git diff git suggests only top-level completions
2 participants