TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.7k stars 149 forks source link

Requiring non-Option fields leads to panics. #498

Closed paholg closed 2 years ago

paholg commented 2 years ago

Given the following code:

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(long, short)]
    foo: bool,

    #[structopt(long, short, required_unless = "foo")]
    bar: String,
}

fn main() {
    let opt = Opt::from_args();

    println!("Opt: {:?}", opt);
}

Running cargo run -- --foo results in

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:9:10

Ideally, this would be a compiler error instead.

TeXitoi commented 2 years ago

Structopt can't detect this sorry. It doesn't know what required_unless means.