I have program that accepts 2 boolean flags -d
for decode and -e
for encode. However, if -e
is specified, additional message string needs to be provided (message to encode). This string should not be present if -d
is specified. How can I accomplish this using structops?
Here is what I have tried:
#[derive(Debug, StructOpt)]
#[structopt(name="encode_test", about="encodes messages")]
struct Opt {
#[structopt(short="e", long="encode")]
encode: bool,
#[structopt(requires("encode"))]
message: String,
#[structopt(short="d", long="decode")]
decode: bool,
}
running the program using cargo run -- -e "test message"
works fine, but cargo run -- -d
gives me the error that <message>
and --encode
arguments were not provided.
How can I require <message>
only if -e
is present?
I have also tried the following:
#[derive(Debug, StructOpt)]
#[structopt(name="encode_test", about="encodes messages")]
struct Opt {
#[structopt(short="d", long="decode", required_unless("encode"))]
decode: bool,
// I've tried using conflicts_with("decode") here as well, same panic
#[structopt(short="e", long="encode", required_unless("decode"))]
encode: String, //does not panic if I change type here from string to bool
}
Which again, works for -e message
but panics when specifying only -d
parameter