Basic CLI

    0

    1

    Giovanny Gongora

    Codiga's Rust Recipes

    cli with arguments definition

    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),
          }
        }
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.