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

clap_complete: completions for args following subcommand are missing #5139

Closed
2 tasks done
scootermon opened this issue Sep 25, 2023 · 1 comment · Fixed by #5140
Closed
2 tasks done

clap_complete: completions for args following subcommand are missing #5139

scootermon opened this issue Sep 25, 2023 · 1 comment · Fixed by #5140
Labels
C-bug Category: Updating dependencies

Comments

@scootermon
Copy link

Please complete the following tasks

Rust Version

rustc 1.72.1 (d5c2e9c34 2023-09-13)

Clap Version

clap: 4.4.4, clap_complete: 4.4.1

Minimal reproducible code

fn main() {
    #[derive(Clone, clap::ValueEnum)]
    enum Enum {
        A,
        B,
        C,
    }

    // create a dummy command with a subcommand, which in turn takes an enum arg
    let mut app = clap::Command::new("mre").subcommand(
        clap::Command::new("subcmd").arg(
            clap::Arg::new("arg")
                .required(true)
                .value_parser(clap::builder::EnumValueParser::<Enum>::new()),
        ),
    );

    // ask clap_complete to generate completions for the argument following the subcommand
    let completions = clap_complete::dynamic::complete(
        &mut app,
        vec!["mre".into(), "subcmd".into(), "".into()],
        2,
        None,
    )
    .unwrap();
    // this will panic because 'A', 'B', 'C' are missing (only '-h' and '--help' are listed)
    assert!(completions.len() >= 3);
}

Steps to reproduce the bug with the above code

cargo run is enough as long as the "unstable-dynamic" is active for clap_complete.

Actual Behaviour

clap_complete doesn't suggest any values for arguments following a subcommand.

Expected Behaviour

It should suggest possible values for arguments following a subcommand.

Additional Context

The reason for this seems fairly straight-forward:

The code responsible for adding the arg value completions uses the pos_index passed into the function to find the correct clap::Arg:

if let Some(positional) = cmd
.get_positionals()
.find(|p| p.get_index() == Some(pos_index))
{
completions.extend(complete_arg_value(arg.to_value(), positional, current_dir));
}

This fails because the pos_index in our example is set to 0. The pos_index is always set to 0 when clap_complete encounters a subcommand:

if let Some(next_cmd) = current_cmd.find_subcommand(value) {
current_cmd = next_cmd;
pos_index = 0;
continue;
}

I fail to see why this is done. If the value is set to 1 instead, the example starts working.

Debug Output

No response

@scootermon scootermon added the C-bug Category: Updating dependencies label Sep 25, 2023
epage added a commit to epage/clap that referenced this issue Sep 25, 2023
@epage
Copy link
Member

epage commented Sep 25, 2023

Good to know people are trying out the Rust-implemeted completions. clap_complete-v4.4.2 is now out with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants