tshepang
tshepang

Reputation: 12459

proc-macro panic with structopt using required_unless and conflicts_with

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

Upvotes: 2

Views: 600

Answers (1)

kennytm
kennytm

Reputation: 523574

#[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>,

Upvotes: 4

Related Questions