PacificNW_Lover
PacificNW_Lover

Reputation: 5384

Issues with importing and using Clap.with_name() function

Have been enjoying learning Rust by reading and going through the examples in Rust in Action

However, one program isn’t working as listed in the book (and can’t find it on the book’s Github repo).

So, the author is showing the reader how to build a command line utility, using a crate called clap, which will serve as a mini grep-like tool in Rust.

Rust version on macOS:

rustc --version
rustc 1.81.0 (eeb90cda1 2024-09-04)

Cargo.toml:

[package]
name = "grep-lite"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = "4.5.17"
regex = "1"

main.rs:

use regex::Regex;
use clap::{App,Arg};

fn main() {

    let args = App::new("grep-lite")
                  .version("0.1")
                  .about("searches for patterns")
                  .arg(Arg::with_name("pattern")
                          .help("The pattern to search for")
                          .takes_value(true)
                          .required(true)      
                  ).get_matches();

    let pattern = args.value_of("pattern").unwrap();              

    let re = Regex::new(pattern).unwrap();

    let quote = "Every face, every shop, bedroom window, public-house, and
  dark square is a picture feverishly turned--in search of what?
  It is the same with books. What do we seek through millions of pages?";
   
    for line in quote.lines() {
      let contains_substring = re.find(line); 
      match contains_substring {
            Some(_) => println!("{}", line),
            None => (),
      }
    }
  }

After invoking cargo run:

cargo run
   Compiling grep-lite v0.1.0 (/Users/pnwlover/RustInAction/ch2/grep-lite)
error[E0432]: unresolved import `clap::App`
 --> src/main.rs:2:12
  |
2 | use clap::{App,Arg};
  |            ^^^ no `App` in the root

error[E0599]: no function or associated item named `with_name` found for struct `Arg` in the current scope
   --> src/main.rs:9:29
    |
9   |                   .arg(Arg::with_name("pattern")
    |                             ^^^^^^^^^ function or associated item not found in `Arg`
    |
note: if you're trying to build a new `Arg`, consider using `Arg::new` which returns `Arg`
   --> /Users/pnwlover/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clap_builder-4.5.17/src/builder/arg.rs:114:5
    |
114 |     pub fn new(id: impl Into<Id>) -> Self {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0432, E0599.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `grep-lite` (bin "grep-lite") due to 2 previous errors

Rust rocks but do wish to understand what I am doing wrong?

Upvotes: 0

Views: 83

Answers (0)

Related Questions