【问题标题】:proc-macro panic with structopt using required_unless and conflicts_with使用 required_unless 和 conflict_with 的 structopt 的 proc-macro 恐慌
【发布时间】:2018-08-08 15:56:28
【问题描述】:

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

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

【问题讨论】:

    标签: rust structopt


    【解决方案1】:

    #[stuff(...),] 最后带有额外的, 不是有效的属性语法。如果你修正了这个错字,你的代码就可以正常工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多