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

Subcommand aliases not included in completion scripts #4265

Closed
2 tasks done
martinvonz opened this issue Sep 26, 2022 · 6 comments · Fixed by #5476
Closed
2 tasks done

Subcommand aliases not included in completion scripts #4265

martinvonz opened this issue Sep 26, 2022 · 6 comments · Fixed by #5476
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.

Comments

@martinvonz
Copy link
Contributor

Please complete the following tasks

Rust Version

1.63.0

Clap Version

3.2.22

Minimal reproducible code

use std::io::{stdout, Write};
use clap;
use clap::CommandFactory;

#[derive(clap::Parser, Clone, Debug)]
enum App {
    #[clap(alias = "bar")]
    Foo(FooArgs),
}

#[derive(clap::Args, Clone, Debug)]
struct FooArgs {
    my_flag: bool,
}

fn main() {
    let mut app = App::command().disable_help_flag(true).disable_help_subcommand(true);
    let mut buf = vec![];
    clap_complete::generate(clap_complete::Shell::Bash, &mut app, "test", &mut buf);
    stdout().write_all(&mut buf).unwrap();
}

Steps to reproduce the bug with the above code

Run the program to print out a completion script for Bash

Actual Behaviour

Sourcing the completion script and then running test <TAB> will not suggest bar and test bar <TAB> will not suggest --my-flag.

Expected Behaviour

test bar <TAB> should suggest --my-flag. I'm not sure if I think test <TAB> should suggest both foo and bar; perhaps the current behavior of not suggesting aliases is best?

The same bug exists in the Fish completion script. I haven't tried others (or checked the code).

I actually noticed the bug because my tool has jj log command and a jj operation/op log command, and I ran jj op log <TAB>, which suggested arguments that are for jj log. That's because the op (an alias for operation) is ignored and completion is run for jj log instead.

Additional Context

No response

Debug Output

No response

@martinvonz martinvonz added the C-bug Category: Updating dependencies label Sep 26, 2022
@epage
Copy link
Member

epage commented Sep 26, 2022

alias is hidden in --help and you need visible_alias to show up in --help.

We've had some back and forth on whether things that are hidden from help should show up in completions

When we resolve #3166, my thought is to handle this according to #3951

@martinvonz
Copy link
Contributor Author

alias is hidden in --help and you need visible_alias to show up in --help.

That makes sense, but it doesn't seem to affect the completion script.

Are you open to a PR to add visible aliases to the completion scripts if I can figure out how?

@epage epage added A-completion Area: completion generator E-help-wanted Call for participation: Help is requested to fix this issue. E-easy Call for participation: Experience needed to fix: Easy / not much labels Sep 28, 2022
@martinvonz
Copy link
Contributor Author

I've noticed a few other bugs while working on this. Do you prefer that I file separate bugs for those? Looking at past release notes, it doesn't look like you require a bug for each fix, but I don't mind creating them if you prefer.

@epage
Copy link
Member

epage commented Sep 28, 2022

Separate issues are good in case

  • There is discussion around the requirements (I prefer PRs to be focused on the implementation)
  • In case not all of them get resolved together

I would at least ask for separate commits for each one. If a change is likely to have more discussion, having that in a separate PR helps.

martinvonz added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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
martinvonz added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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 added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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
martinvonz added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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 added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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
martinvonz added a commit to martinvonz/clap that referenced this issue Sep 29, 2022
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.
@T0mstone
Copy link

Supports visible aliases:

Although the zsh completion supports listing the aliases for test <TAB>, there is currently still the bug with test bar <TAB> not suggesting --my-flag (or anything else, for that matter).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
3 participants