25

I'm trying out the "Using Derive Macros" example on the index page for the latest beta version of 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,
}
...

Unfortunately it fails to compile:

$ 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
...

I tried to find the full example file "examples/01d_quick_example.rs" in the tar file for this GitHub tag, but it does not seem exist there.

I appreciate this is a beta version, but is this functionality expected to work, or am I doing something wrong?

Thanks!

2
  • 6
    Did you enable the derive feature for clap in Cargo.toml? :) I have made that mistake so many times Commented Nov 6, 2021 at 15:21
  • 2
    Thanks, that was the problem. If you would like to post an answer, I'll upvote it. Commented Nov 6, 2021 at 15:36

1 Answer 1

47

In clap, use features = [ "derive" ] in Cargo.toml to enable the ability to derive :)

update

@stein below makes a good point of expanding the answer:

To "use" means: in the [dependencies] section, specify clap with a line similar to: clap = { version = "3.1.0", features = ["derive"]} –  Stein Feb 19 at 13:00

Please +1 their comment :-).

This means, in your Cargo.toml

[dependencies]
# ...
clap = { version = "3", features = ["derive"]}
2
  • 4
    if you're like me, check if you forgot the #[derive(Parser)] before any clap() Commented Jan 6, 2022 at 2:17
  • 11
    To "use" means: in the [dependencies] section, specify clap with a line similar to: clap = { version = "3.1.0", features = ["derive"]}
    – Stein
    Commented Feb 19, 2022 at 13:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.