From ff5afb24ba4430501a246634db2f8006073df494 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 27 Feb 2020 13:16:47 -0300 Subject: [PATCH] Mapped all the commands --- .gitignore | 1 + src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..a175e72 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +*.sw? diff --git a/src/main.rs b/src/main.rs index 6ad09b0..50e9792 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ -use clap::{Arg, App, SubCommand}; +use clap::{Arg, ArgMatches, App, SubCommand}; fn main() { let generate = SubCommand::with_name("generate") .about("Generate a new name") .arg(Arg::with_name("description") + .required(true) .help("Short description of your application")); let adjectives = SubCommand::with_name("adjectives") @@ -25,11 +26,11 @@ fn main() { .about("List current metal names")) .subcommand(SubCommand::with_name("add") .about("Add a new metal name") - .arg(Arg::with_name("adjective") + .arg(Arg::with_name("name") .help("Metal name to be added"))) .subcommand(SubCommand::with_name("rm") .about("Remove a metal name") - .arg(Arg::with_name("adjective") + .arg(Arg::with_name("name") .help("Metal name to be removed"))); let main = App::new("Name Rust Programs") @@ -41,5 +42,57 @@ fn main() { .subcommand(metals); let matches = main.get_matches(); - dbg!(matches); + match matches.subcommand() { + ("metals", Some(metal_command)) => metals_commands(&metal_command), + ("adjectives", Some(adj_command)) => adjective_commands(&adj_command), + ("generate", Some(gen_command)) => generate_command(&gen_command), + (_, _) => panic!("I don't know how to handle that!"), + }; +} + +fn metals_commands(command: &ArgMatches) { + match command.subcommand() { + ("list", Some(_)) => metals_list(), + ("add", Some(add_cmd)) => metals_add(add_cmd.value_of("name")), + ("rm", Some(rm_cmd)) => metals_rm(rm_cmd.value_of("name")), + (command, _) => println!("Invalid command: {}", command), + } +} + +fn adjective_commands(command: &ArgMatches) { + match command.subcommand() { + ("list" , Some(_)) => adjective_list(), + ("add" , Some(add_cmd)) => adjective_add(add_cmd.value_of("name")), + ("rm" , Some(rm_cmd)) => adjective_rm(rm_cmd.value_of("name")), + (command, _) => println!("Invalid command: {}", command), + } +} + +fn generate_command(command: &ArgMatches) { + let app_description = command.value_of("description"); + dbg!("generate description", app_description); +} + +fn metals_list() { + dbg!("list metals"); +} + +fn metals_add(name: Option<&str>) { + dbg!("Add metal", name); +} + +fn metals_rm(name: Option<&str>) { + dbg!("Rmove metal", name); +} + +fn adjective_list() { + dbg!("list adjectives"); +} + +fn adjective_add(name: Option<&str>) { + dbg!("Add adjective", name); +} + +fn adjective_rm(name: Option<&str>) { + dbg!("Remove adjective", name); }