From 65ca86a60c3311401e8a85e37249c0f5e1ca4008 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 17 Jun 2021 23:31:33 -0400 Subject: [PATCH] added the help flag and an error if too many arguments are passed --- Cargo.toml | 2 +- src/main.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2dd3ef6..2baa15b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "0.2.0" +version = "0.3.0" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index b93ffc8..0eac478 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,40 @@ fn main() { let dir = i_dir.as_path(); let config = Config::new(); + // Parse arguments and handle them. let args: Vec = args().collect(); + let mut error: Option = Some("open: missing file operand.".to_string()); + for arg in &args[1..] { + match arg.as_str() { + "-h" | + "--help" => { + println!("open +Valerie Wolfe +A Linux implementation of the \"open\" command on Mac OS written in Rust and easily configurable. + +USAGE: + open [FLAGS] [FILE] + +FLAGS: + -h, --help Prints this help text +"); + return; + }, + _ => { + if error.is_none() { + error = Some("open: too many arguments.".to_string()); + } else { + error = None; + } + } + } + } + if error.is_some() { + println!("{}", error.unwrap()); + return; + } + let default = ".".to_string(); let arg_target = args.get(1); let i_target = arg_target.unwrap_or(&default);