2

I want to have two options that conflict with each other, but also one of them must be required:

#[macro_use]
extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt)]
struct Opt {
    #[structopt(
        long = "foo",
        required_unless = "bar",
        conflicts_with = "bar",
    )]
    foo: Option<String>,
    #[structopt(
        long = "bar",
        required_unless = "foo"),
    ]
    bar: Option<String>,
}

fn main() {
    let args = Opt::from_args();
    println!("{:?}", args.foo);
    println!("{:?}", args.bar);
}

Here is where how the compiler (v1.28.0) complains:

error: proc-macro derive panicked
 --> src/main.rs:6:10
  |
6 | #[derive(StructOpt)]
  |          ^^^^^^^^^
  |
  = help: message: invalid structopt syntax: attr
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

4

#[stuff(...),] with that extra , at the end is not a valid attribute syntax. Your code works fine if you fix this typo.

#[structopt(
    long = "bar",
    required_unless = "foo",    // no `)` on this line.
)]                              // put `)` on this line, no `,` after it
bar: Option<String>,
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005