clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust

Home Page:docs.rs/clap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't show flags in native completion that are already present

HKalbasi opened this issue · comments

Please complete the following tasks

Rust Version

1.76

Clap Version

4.5.2

Minimal reproducible code

use clap::{CommandFactory, Parser, ValueEnum};

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
struct Cli {
    /// What mode to run the program in
    #[arg(short, long, value_enum)]
    mode: Mode,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Mode {
    /// Run swiftly
    Fast,
    /// Crawl slowly but steadily
    ///
    /// This paragraph is ignored because there is no long help text for possible values.
    Slow,
}
fn main() {
    let mut cmd = Cli::command();
    let args = std::env::args_os().collect::<Vec<_>>();
    let arg_index = args.len() - 1;
    let r = clap_complete::dynamic::complete(&mut cmd, args, arg_index, None).unwrap();
    dbg!(r);
}}

Steps to reproduce the bug with the above code

cargo run -- --mode fast --mo

Actual Behaviour

[src/main.rs:25:5] r = [
    (
        "--mode",
        Some(
            StyledStr(
                "What mode to run the program in",
            ),
        ),
    ),
]

Expected Behaviour

[src/main.rs:25:5] r = []

Additional Context

Using --mode is invalid, so we should not complete it. Obviously flags that accept multiple values should keep the existing behavior.

Debug Output

No response

There is a little more to it. We should only do this if the ArgAction prevenhs it and if we don't override ourself.