From 39527db5c91cf7c66dba569c974a73eedc4d7ac8 Mon Sep 17 00:00:00 2001 From: Britton W Date: Tue, 25 Jul 2023 10:22:46 -0400 Subject: [PATCH] flags now collect properly, and list no longer fails without names --- Cargo.toml | 2 +- src/main.rs | 40 ++++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index beb1a8c..b738957 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "config" -version = "0.4.1" +version = "0.4.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index 376b616..bb411bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,8 +21,14 @@ fn main() -> Result<(), Box>{ // get args let mut args = Arguments::from_env(); + // collect args + let flag_help = args.contains(["-h", "--help"]); + let flag_list = args.contains(["-l", "--list"]); + let flag_dir = args.contains(["-d", "--dir"]); + let flag_path = args.contains(["-p", "--path"]); + // handle help flag - if args.contains(["-h", "--help"]) { + if flag_help { help_text(); return Ok(()); } @@ -50,32 +56,34 @@ fn main() -> Result<(), Box>{ let mut section: &Map = config; let mut target = args.subcommand().unwrap(); let mut fullname = String::new(); - loop { - let entry = target.as_ref().unwrap(); - if !fullname.is_empty() { fullname += "."; } - fullname += entry; - let i_section = section.get(entry); - if i_section.is_none() { error::no_section(&entry); } - section = i_section.unwrap().as_table().unwrap(); + if target.is_some() { + loop { + let entry = target.as_ref().unwrap(); + if !fullname.is_empty() { fullname += "."; } + fullname += entry; + let i_section = section.get(entry); + if i_section.is_none() { error::no_section(&entry); } + section = i_section.unwrap().as_table().unwrap(); - let i_target = args.subcommand().unwrap(); - if i_target.is_none() { break; } - target = i_target; + let i_target = args.subcommand().unwrap(); + if i_target.is_none() { break; } + target = i_target; + } } - if target.is_none() { error::no_args(); } - // handle list flag - if args.contains(["-l", "--list"]) { + if flag_help { for (key, _) in section.iter() { println!("{key}"); } return Ok(()); } + if target.is_none() { error::no_args(); } + let prop_path = section.get("path"); // handle dir flag - if args.contains(["-d", "--dir"]) { + if flag_dir { if prop_path.is_none() { error::no_property(&fullname, "path"); } @@ -92,7 +100,7 @@ fn main() -> Result<(), Box>{ return Ok(()); } // handle path flag - if args.contains(["-p", "--path"]) { + if flag_path { if prop_path.is_none() { error::no_property(&fullname, "path"); } let i_file_path = prop_path.unwrap().as_str().unwrap(); let file_path = if i_file_path.starts_with("~") { i_file_path.replace("~", &home_dir) } else { i_file_path.to_string() };