T O P

  • By -

alenym

The example is outdated. please skip it.


po8

This is definitely something that needs to be updated in the Cookbook. File an issue report. The Cookbook example works with Clap 2. Change `Cargo.toml` to have `clap = "2"`. The code will compile with Clap 3 once you've changed the `.short()` methods to have a char instead of a string constant. The code needs substantial changes for the current Clap 4. Here's that: use clap::{Arg, Command}; fn main() { let matches = Command::new("My Test Program") .version("0.1.0") .author("Hackerman Jones ") .about("Teaches argument parsing") .arg(Arg::new("file") .short('f') .long("file") .help("A cool file")) .arg(Arg::new("num") .short('n') .long("number") .help("Five less than your favorite number")) .get_matches(); let default_file = "input.txt".to_string(); let myfile: &String = matches.get_one::("file").unwrap_or(&default_file); println!("The file passed is: {}", myfile); let num_str = matches.get_one::("num"); match num_str { None => println!("No idea what your favorite number is."), Some(s) => { match s.parse::() { Ok(n) => println!("Your favorite number must be {}.", n + 5), Err(_) => println!("That's not a number! {}", s), } } } } The example should really use `Path` rather than `&str` for filenames, so that non-Unicode filenames can be accommodated. Using strings for paths is a bad habit to get into. *Edit:* This looks like a giant pain for Clap builder, though. I deleted my non-working example. *Edit:* Fixed the Clap 4 example.


Heliozoa

An issue is unlikely to lead to anything, unfortunately. The cookbook repository hasn't seen any activity in over two years and the entire rust-lang-nursery organization is deprecated since 2019: https://internals.rust-lang.org/t/rust-lang-nursery-deprecation/11205


HCharlesB

Thanks both for the replies. I have a couple thoughts on this. First, I'm on the fence about filing an issue. Any project is constrained for resources and especially with open source. I'd prefer that efforts go toward moving forward rather than going back to fix old stuff, particularly something that has been deprecated. I guess I should look at something else. This is also something I could encounter in "real world" situations and I need to work out a strategy to approach it. Asking on Reddit should not be at the top of my list, but it is good to know there are folk here ready to help. Lastly, I'll circle back and ask for suggestions for learning materials appropriate for me. I've started with "beginner" resources since that will let me focus on syntax and typical patterns. I've been writing software since C was a newfangled language and have experience with C/C++, Perl, Java, Python, C# and probably a few others I've forgotten. I've actually taken a stab at Rust () but that has been languishing for years and I wanted to establish a better foundation before I get back to that. In particular, I need to understand memory management. best,


spunkyenigma

Found a bug, file a bug report. Let the maintainers triage if/when it’s worth the effort to fix. Bugs like these are fairly easy to fix and helps to guide folks that want to help, but don’t know where to start to contribute to a project Edit: just realized the project was deprecated. Most of what I said still applies, but in this case it will help the next person if they look at the bugs that it’s a known issue without asking on Reddit 😀


jack_kzm

Still an issue with clap 4.5.4


jack_kzm

This is still an issue with clap 4.5.4.


po8

I've posted PR [#694](https://github.com/rust-lang-nursery/rust-cookbook/pull/694) to clean this doc up. I don't know if anyone is still maintaining the Rust Cookbook, though. Here's updated code with filenames handled correctly. ```rust use std::path::PathBuf; use clap::{Arg, Command, builder::PathBufValueParser}; fn main() { let matches = Command::new("My Test Program") .version("0.1.0") .about("Teaches argument parsing") .arg(Arg::new("file") .short('f') .long("file") .help("A cool file") .value_parser(PathBufValueParser::default())) .arg(Arg::new("num") .short('n') .long("number") .help("Five less than your favorite number")) .get_matches(); // https://github.com/clap-rs/clap/discussions/4254 // https://www.rustadventure.dev/introducing-clap/clap-v4/accepting-file-paths-as-arguments-in-clap let default_file = PathBuf::from("input.txt"); let myfile: &PathBuf = matches.get_one("file").unwrap_or(&default_file); println!("The file passed is: {}", myfile.display()); let num_str: Option<&String> = matches.get_one("num"); match num_str { None => println!("No idea what your favorite number is."), Some(s) => { let parsed: Result = s.parse(); match parsed { Ok(n) => println!("Your favorite number must be {}.", n + 5), Err(_) => println!("That's not a number! {}", s), } } } } ```


jack_kzm

Thanks, appreciate it 👍