gpt4 book ai didi

rust - 使用 required_unless 和 conflicts_with 的 structopt 的 proc-macro panic

转载 作者:行者123 更新时间:2023-11-29 08:12:12 24 4
gpt4 key购买 nike

我想有两个相互冲突的选项,但其中一个必须是必需的:

#[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);
}

这是编译器 (v1.28.0) 提示的地方:

error: proc-macro derive panicked
--> src/main.rs:6:10
|
6 | #[derive(StructOpt)]
| ^^^^^^^^^
|
= help: message: invalid structopt syntax: attr

最佳答案

#[stuff(...),] 末尾带有额外的 , 不是有效的属性语法。如果您修复此拼写错误,您的代码可以正常工作。

#[structopt(
long = "bar",
required_unless = "foo", // no `)` on this line.
)] // put `)` on this line, no `,` after it
bar: Option<String>,

关于rust - 使用 required_unless 和 conflicts_with 的 structopt 的 proc-macro panic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51750975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com