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

exclusive fails to take priority over required_unless_present flags #5507

Closed
2 tasks done
obskyr opened this issue May 26, 2024 · 0 comments · Fixed by #5520
Closed
2 tasks done

exclusive fails to take priority over required_unless_present flags #5507

obskyr opened this issue May 26, 2024 · 0 comments · Fixed by #5520
Labels
A-validators Area: ArgMatches validation logi C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@obskyr
Copy link

obskyr commented May 26, 2024

Please complete the following tasks

Rust Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)

Clap Version

4.5.4

Minimal reproducible code

The flags I have found to eschew exclusive are:

  • required_unless_present

    #[derive(clap::Parser)]
    struct Args {
        #[arg(required_unless_present("alternative_arg"))]
        required_arg: Option<String>,
        alternative_arg: Option<String>,
    
        #[arg(exclusive = true, long, default_value_t = false)]
        exclusive_arg: bool
    }
    
    fn main() {
        let _args: Args = clap::Parser::parse();
    }
  • required_unless_present_any

    #[derive(clap::Parser)]
    struct Args {
        #[arg(required_unless_present_any(["alternative_arg"]))]
        required_arg: Option<String>,
        alternative_arg: Option<String>,
    
        #[arg(exclusive = true, long, default_value_t = false)]
        exclusive_arg: bool
    }
    
    fn main() {
        let _args: Args = clap::Parser::parse();
    }
  • required_unless_present_all

    #[derive(clap::Parser)]
    struct Args {
        #[arg(required_unless_present_all(["alternative_arg"]))]
        required_arg: Option<String>,
        alternative_arg: Option<String>,
    
        #[arg(exclusive = true, long, default_value_t = false)]
        exclusive_arg: bool
    }
    
    fn main() {
        let _args: Args = clap::Parser::parse();
    }

Steps to reproduce the bug with the above code

cargo run -- --exclusive-arg

Actual Behaviour

Clap fails to recognize that an exclusive argument has been supplied, and still requires the required arguments with an error message akin to:

error: the following required arguments were not provided:
  <REQUIRED_ARG>

Usage: clap-test.exe --exclusive-arg <REQUIRED_ARG> [ALTERNATIVE_ARG]

For more information, try '--help'.

Expected Behaviour

The exclusive argument should take priority, and allow not supplying the required arguments. (If it doesn't, it makes exclusive useless in these cases – since it's impossible to use the exclusive argument without adding the required arguments, and if you add the required arguments, you get an error due to supplying arguments along with the exclusive one.)

Additional Context

Using the exclusive flag for an argument allows it to override required arguments, as long as those arguments can be initialized without. This code that sets required = true on an argument still runs without error if --exclusive-arg is specified:

#[derive(clap::Parser)]
struct Args {
    #[arg(required = true)]
    required_arg: Option<String>,

    #[arg(exclusive = true, long, default_value_t = false)]
    exclusive_arg: bool
}

fn main() {
    let _args: Args = clap::Parser::parse();
}

To match this, the same should be true for the other, more specified ways to require an argument!

Note that the required_unless_present and required_unless_present_any (though crucially, not required_unless_present_all) cases can be worked around by adding a special case – for example, switching out required_unless_present("blah") for required_unless_present_any(["blah", "exclusive"]).

Debug Output

No response

@obskyr obskyr added the C-bug Category: Updating dependencies label May 26, 2024
@epage epage added A-validators Area: ArgMatches validation logi E-easy Call for participation: Experience needed to fix: Easy / not much labels May 27, 2024
epage added a commit to epage/clap that referenced this issue Jun 6, 2024
There are other cases for `required` that aren't being handled
- Groups
- Conflicts

I'm concerned there might be weird corner cases and didn't want the
analysis for that to block fixing this.

Fixes clap-rs#5507
epage added a commit to epage/clap that referenced this issue Jun 6, 2024
There are other cases for `required` that aren't being handled
- Groups
- Conflicts

I'm concerned there might be weird corner cases and didn't want the
analysis for that to block fixing this.

Fixes clap-rs#5507
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants