These are chat archives for kbknapp/clap-rs

Feb 2019
Feb 19 17:12

Ok, first day with clap/rust and I'm fighting the borrow checker. I want to get_matches() and then print the help afterwards (ie if no command was given):

extern crate clap;
use clap::App;
fn main() {
  let mut app = App::new("nuvemfs");
  let _matches = app.get_matches();
  app.print_long_help().expect("failed to print help");

gives me

error[E0382]: borrow of moved value: `app`
 --> src/
5 |   let _matches = app.get_matches();
  |                  --- value moved here
6 |   app.print_long_help().expect("failed to print help");
  |   ^^^ value borrowed here after move
  = note: move occurs because `app` has type `clap::App<'_, '_>`, which does not implement the `Copy` trait

error: aborting due to previous error

since get_matches(self) and print_long_help(&mut self). But now I'm stuck... if get_matches consumes the app, is there any way to print the help afterwards at all?

Denis Lisov
Feb 19 20:08
@samuela How about the built-in options ArgRequiredElseHelp or SubcommandRequiredElseHelp?
Feb 19 20:09
@tanriol ah, that does what i want! thank you!
broadly speaking is there any way to do this with the borrow checker? i'm just trying to build a better intuition for how it works and what makes it happy
Denis Lisov
Feb 19 20:22
Some options are: parse with get_matches_from_safe_borrow instead; format the help message before parsing with write_long_help; clone the App before parsing; call your function that generates the App once more to get a second App instance :-)