如何解决structops-如何根据布尔标志
我有一个程序,它接受两个布尔标志-d
进行解码,并接受-e
进行编码。但是,如果指定了-e
,则需要提供其他消息字符串(要编码的消息)。如果指定了-d
,则此字符串不应存在。如何使用structops完成此操作?
这是我尝试过的:
#[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,}
使用cargo run -- -e "test message"
运行程序可以很好地工作,但是cargo run -- -d
给我的错误是没有提供<message>
和--encode
参数。
仅在存在<message>
的情况下如何要求-e
?
我也尝试了以下方法:
#[derive(Debug,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
}
同样,它适用于-e message
,但仅指定-d
参数时会感到恐慌
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。