use clap::{Arg, App};
      
      fn main() {
        let matches = App::new("my rust cli")
        .version("1.0.0")
        .author("John Doe <john@doe.com>")
        .about("custom cli")
        .arg(Arg::with_name("file")
          .short("f")
          .long("file")
          .takes_value(true)
          .help("A cool file"))
        .arg(Arg::with_name("num")
          .short("n")
          .long("number")
          .takes_value(true)
          .help("Five less than your favorite number"))
        .get_matches();
        let myfile = matches.value_of("file").unwrap_or("input.txt");
        println!("The file passed is: {}", myfile);
        let num_str = matches.value_of("num");
        match num_str {
          None => println!("No idea what your favorite number is."),
          Some(s) => {
            match s.parse::<i32>() {
              Ok(n) => println!("Your favorite number must be {}.", n + 5),
              Err(_) => println!("That's not a number! {}", s),
            }
          }
        }
      }
      
      Rust language logo

      Basic CLI

      Codiga's Rust Recipes

      cli with arguments definition

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.