首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有派生宏的Clap选项在rust clap 3.0.0-beta.5中无法编译

带有派生宏的Clap选项在rust clap 3.0.0-beta.5中无法编译
EN

Stack Overflow用户
提问于 2021-11-06 10:31:46
回答 1查看 1.1K关注 0票数 6

我在the index page上尝试了最新测试版的clap的“使用派生宏”示例:

代码语言:javascript
复制
// (Full example with detailed comments in examples/01d_quick_example.rs)
//
// This example demonstrates clap's full 'custom derive' style of creating arguments which is the
// simplest method of use, but sacrifices some flexibility.
use clap::{AppSettings, Parser};

/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Parser)]
#[clap(version = "1.0", author = "Kevin K. <kbknapp@gmail.com>")]
struct Opts {
    /// Sets a custom config file. Could have been an Option<T> with no default too
    #[clap(short, long, default_value = "default.conf")]
    config: String,
    /// Some input. Because this isn't an Option<T> it's required to be used
    input: String,
    /// A level of verbosity, and can be used multiple times
    #[clap(short, long, parse(from_occurrences))]
    verbose: i32,
    #[clap(subcommand)]
    subcmd: SubCommand,
}
...

不幸的是,它无法编译:

代码语言:javascript
复制
$ cargo build
   Compiling ex v1.0.0-SNAPSHOT (/home/hwalters/git/home/ex)
error: cannot find derive macro `Parser` in this scope
 --> src/main.rs:5:10
  |
5 | #[derive(Parser)]
  |          ^^^^^^
  |
note: `Parser` is imported here, but it is only a trait, without a derive macro
 --> src/main.rs:1:25
  |
1 | use clap::{AppSettings, Parser};
  |                         ^^^^^^

error: cannot find attribute `clap` in this scope
 --> src/main.rs:6:3
  |
6 | #[clap(version = "1.0", author = "Kevin K. <kbknapp@gmail.com>")]
  |   ^^^^
  |
  = note: `clap` is in scope, but it is a crate, not an attribute
...

我试图在this GitHub tag的tar文件中找到完整的示例文件"examples/01d_quick_example.rs“,但它似乎不存在。

我很感谢这是一个测试版,但是这个功能是否可以正常工作,或者我做错了什么?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-11-09 13:07:31

在clap中,使用Cargo.toml中的features = [ "derive" ]来启用派生功能:)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69863208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档