Clap オプションと派生マクロが Rust Clap 3.0.0-beta.5 でコンパイルに失敗する 質問する

Clap オプションと派生マクロが Rust Clap 3.0.0-beta.5 でコンパイルに失敗する 質問する

「派生マクロの使用」の例を試していますインデックスページ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. <[email protected]>")]
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,
}
...

残念ながらコンパイルに失敗します:

$ 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. <[email protected]>")]
  |   ^^^^
  |
  = note: `clap` is in scope, but it is a crate, not an attribute
...

tarファイル内の完全なサンプルファイル「examples/01d_quick_example.rs」を見つけようとしました。このGitHubタグですが、そこには存在しないようです。

これはベータ版であることは承知していますが、この機能は動作するはずですか、それとも私が何か間違っているのでしょうか?

ありがとう!

ベストアンサー1

拍手では、派生する機能を有効にするためにfeatures = [ "derive" ]in を使用します :)Cargo.toml

アップデート

以下の @stein は、回答を拡張する上で良い点を指摘しています。

「使用する」とは、[dependencies] セクションで、次のような行で clap を指定することを意味します: clap = { version = "3.1.0", features = ["derive"]} – Stein 2 月 19 日 13:00

ぜひコメントに +1 を付けてください :-)。

つまり、あなたのCargo.toml

[dependencies]
# ...
clap = { version = "3", features = ["derive"]}

おすすめ記事