我正在尝试最新 beta 版 clap索引页上的“使用派生宏”示例:
// (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,
}
...
Run Code Online (Sandbox Code Playgroud)
不幸的是它无法编译:
$ 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
...
Run Code Online (Sandbox Code Playgroud)
我试图在这个 GitHub 标签的tar 文件中找到完整的示例文件“examples/01d_quick_example.rs” ,但它似乎不存在。
我很高兴这是测试版,但此功能是否可以正常工作,或者我做错了什么?
谢谢!
Ahm*_*sud 42
在拍手中,features = [ "derive" ]使用Cargo.toml来启用派生能力:)
下面的@stein很好地扩展了答案:
\n\n\n“使用”的意思是:在 [依赖项] 部分中,使用类似于以下内容的行指定 clap: clap = { version = "3.1.0", features = ["derive"]} \xe2\x80\x93\xc2\ xa0\n斯坦因 2 月 19 日 13:00
\n
请+1 他们的评论:-)。
\n这意味着,在你的Cargo.toml
[dependencies]\n# ...\nclap = { version = "3", features = ["derive"]}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
9253 次 |
| 最近记录: |