clap icon indicating copy to clipboard operation
clap copied to clipboard

Subcommand aliases not included in completion scripts

Open martinvonz opened this issue 2 years ago • 6 comments

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 avatar Sep 26 '22 21:09 martinvonz

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

  • https://github.com/clap-rs/clap/issues/2541
  • https://github.com/clap-rs/clap/issues/1335

When we resolve https://github.com/clap-rs/clap/issues/3166, my thought is to handle this according to https://github.com/clap-rs/clap/issues/3951

epage avatar Sep 26 '22 22:09 epage

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?

martinvonz avatar Sep 26 '22 23:09 martinvonz

Yes

Supports visible aliases:

  • zsh: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/zsh.rs#L170

Doesn't

  • bash: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/bash.rs#L104
  • elvish: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/elvish.rs#L122
  • fish: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/fish.rs#L135
  • powershell: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/powershell.rs#L151

epage avatar Sep 27 '22 01:09 epage

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.

martinvonz avatar Sep 28 '22 16:09 martinvonz

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.

epage avatar Sep 28 '22 16:09 epage

Supports visible aliases:

  • zsh: https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/zsh.rs#L170

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).

T0mstone avatar Oct 31 '23 16:10 T0mstone