Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

requires_ifs does not appear to work #246

Open
2 tasks done
epage opened this issue Dec 6, 2021 · 5 comments
Open
2 tasks done

requires_ifs does not appear to work #246

epage opened this issue Dec 6, 2021 · 5 comments
Labels

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by FrankC01
Saturday Dec 04, 2021 at 11:16 GMT
Originally opened as clap-rs/clap#3059


Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Rust Version

rustc 1.55.0 (c8dfcfe04 2021-09-06)

Clap Version

clap = "2.33.3"

Minimal reproducible code

    #[test]
    fn test_output() {
        let res = App::new("prog")
            .arg(
                Arg::with_name("output")
                    .long("output")
                    .short("o")
                    .takes_value(true)
                    .possible_values(&["csv", "excel", "stdout"])
                    .default_value("stdout")
                    .help("Direct output to file"),
            )
            .arg(
                Arg::with_name("filename")
                    .long("filename")
                    .short("f")
                    .takes_value(true)
                    // .requires_if("excel", "output") 
                    // .requires_ifs(&[("output", "excel"), ("output", "csv")])
                    .requires_ifs(&[("excel", "output"), ("csv", "output")])
                    .help("Filename for '-o excel' or '-o csv' output"),
            )
            .get_matches_from_safe(vec!["prog", "-o", "excel"]);
        println!("{:?}", res);
        // assert!(res.is_err()); // We  used -o excel so -f <filename> is required
        // assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
    }

Steps to reproduce the bug with the above code

cargo test

Actual Behaviour

No error occurred

Expected Behaviour

Expected error as the -f <val> is required if -o excel or -o csv is present

Additional Context

No response

Debug Output

No response

@epage epage added the T: bug label Dec 6, 2021
@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by FrankC01
Saturday Dec 04, 2021 at 11:45 GMT


This works if I change

.requires_ifs(&[("output", "excel"), ("output", "csv")])

to

.required_ifs(&[("output", "excel"), ("output", "csv")])

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by epage
Saturday Dec 04, 2021 at 17:53 GMT


Yes

  • a "requires" relationship says filename cannot be present unless X conditions are met (prog -f file will fail without -o excel)
  • a "required" relationship says filename must be present if X conditions are met (prog -o excel will fail without -f file)

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by epage
Saturday Dec 04, 2021 at 17:54 GMT


And I do feel like we could do a better job clarifying that distinction

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by FrankC01
Sunday Dec 05, 2021 at 09:09 GMT


However, while required_ifs fails if -f <arg> is not supplied with -o <arg>, it does not fail if I do prog -f somefile

Basically I'm looking for a way to:

  1. If -f <arg> appears without -o <arg then fail
  2. if -o <arg> appears without -f <arg> then fail

Am I missing some magic combo?

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by epage
Monday Dec 06, 2021 at 13:00 GMT


Could you provide code with those cases so we know which APIs you are using in which ways to try to get the behavior you are wanting that isn't working as expected?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant