StructOpt and conditional required argument

I've got something like the following struct:

#[derive(Debug, StructOpt)]
#[structopt(about = "Foo")]
struct Options {
    #[structopt(short, long)]
    pub output: PathBuf,

    #[structopt(short, long))]
    pub foo: Option<PathBuf>,

    #[structopt(long, required_unless("foo"))]
    pub barOption<PathBuf>,

    #[structopt(short, long)]
    pub debug: bool,
}

I want to have argument bar as required if argument foo is present. Unfortunately the behavior above is exactly the opposite. I've seen in the documentation that there is a required_if. But this parameter needs a specific value, but the value for foo is arbitrary. So is there something like required_if("foo") possible?

1 Like

This isn't an answer to your question, but if you were willing to give up the short arguments, you could use auto-args:

#[derive(Debug, AutoArgs)]
struct Fooness {
  /// Documentation for foo
  foo: PathBuf,
  barOption: PathBuf,
}

#[derive(Debug, AutoArgs)]
struct Options {
    pub output: PathBuf,
    pub _foo: Option<Fooness>,
    pub debug: bool,
}

auto-args isn't for you, though, if you want short options, or in general need to fine tune your interface.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.