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 #4284

Closed
wants to merge 10 commits into from

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.

@martinvonz
Copy link
Contributor Author

The failed "CI / Docs" check is not my fault, is it? Possibly related, I've sent this for the v3-master branch because that's what I personally care about (and probably most other users?). I hope that's okay.

@epage
Copy link
Member

epage commented Sep 29, 2022

clap-rs:v3-master

Per our support policy, changes should go into master and then be cherry-picked back into v3-master

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.
@martinvonz martinvonz changed the base branch from v3-master to master September 29, 2022 15:59
@martinvonz
Copy link
Contributor Author

This is now rebase and ready for master

@epage
Copy link
Member

epage commented Sep 29, 2022

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?

@martinvonz
Copy link
Contributor Author

Sure, I don't mind doing that.

Per our support policy, changes should go into master and then be cherry-picked back into v3-master

By the way, I didn't realize that clap 4 was now released. Congrats! :) I'm working on upgrading my project now.

@epage
Copy link
Member

epage commented Sep 29, 2022

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

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