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

ArgMathces::args_present behaviour with ArgAction::SetTrue #5860

Open
2 tasks done
Vaider7 opened this issue Dec 25, 2024 · 2 comments
Open
2 tasks done

ArgMathces::args_present behaviour with ArgAction::SetTrue #5860

Vaider7 opened this issue Dec 25, 2024 · 2 comments
Labels
C-bug Category: Updating dependencies

Comments

@Vaider7
Copy link

Vaider7 commented Dec 25, 2024

Please complete the following tasks

Rust Version

rustc 1.85.0-nightly

Clap Version

4.5.23

Minimal reproducible code

fn main() {
    let cli = Command::new("myapp").arg(
        Arg::new("show")
            .short('s')
            .long("show")
            .action(ArgAction::SetTrue),
    );
    let matches = cli.get_matches_from(["myapp"]);
    assert!(!matches.args_present());
}

This code panic with message

assertion failed: !matches.args_present()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

No args were provided, but args_present() returns true, which I think counterintuitive

Steps to reproduce the bug with the above code

cargo run

Actual Behaviour

matches.args_present() returns true

Expected Behaviour

matches.args_present() returns false if no arguments were provided despite Arg::new("show").action(ArgAction::SetTrue)

Additional Context

No response

Debug Output

No response

@Vaider7 Vaider7 added the C-bug Category: Updating dependencies label Dec 25, 2024
@epage
Copy link
Member

epage commented Dec 25, 2024

The documentation says "present on command line" but the behavior is if any ids are present, like contains_id
https://github.com/clap-rs/clap/blob/master/clap_builder%2Fsrc%2Fparser%2Fmatches%2Farg_matches.rs#L578

With SetTrue, false is a default which makes it so it contains the id.

The value_source is the defínitive way to know.

The question is whether we should change the docs or the behavior.

@Vaider7
Copy link
Author

Vaider7 commented Dec 25, 2024

Well, personally, I think it's very important to be able to check whether some args were provided via command line or not. If we change the docs, the behavior of args_present remains the same. And if so, args_present is not useful for any CLI setup, which uses action(ArgAction::SetTrue), because it will always return true regardless of whether args were provided or not. So my personal suggestion is that args_present should return presence of arguments despite any Command setup.

I'm not very familiar with clap yet, so I'm sorry if I'm saying nonsense things

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

No branches or pull requests

2 participants