How do you use load_yaml! macro in clap?

Here's my Cargo.toml

[dependencies]
clap = "*"

I copied the code from here: clap::load_yaml - Rust

fn main() {
    let yaml = load_yaml!("app.yaml");
    let app = App::from(yaml);
}

Using: rustc 1.67.1 (d5a82bbd2 2023-02-07)

This is what I get:

cargo build 
   Compiling test-clap-yaml v0.1.0 (/tmp/test-clap-yaml)
error[E0432]: unresolved import `clap::App`
 --> src/main.rs:1:5
  |
1 | use clap::App;
  |     ^^^^^^^^^ no `App` in the root

error: cannot find macro `load_yaml` in this scope
 --> src/main.rs:3:16
  |
3 |     let yaml = load_yaml!("app.yaml");
  |                ^^^^^^^^^

Is the documentation is wrong?

The documentation you linked is for a beta version of clap 3.0.0. Looks like yaml support was deprecated and then removed from clap proper. Someone made this crate as a result (which I'm not familiar with).

Incidentally, you can run cargo tree to see what version you're using. Or perhaps use cargo doc --open --package clap.

This is very much not recommended because clap will update to a new major version and your code is going to potentially break. Just choose a version number or at least only let the minor and patch version be chosen for you.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.