added the help flag and an error if too many arguments are passed

This commit is contained in:
Valerie Wolfe 2021-06-17 23:31:33 -04:00
parent bbb73290ef
commit 65ca86a60c
2 changed files with 33 additions and 1 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "open" name = "open"
version = "0.2.0" version = "0.3.0"
authors = ["Valerie Wolfe <sleeplessval@gmail.com>"] authors = ["Valerie Wolfe <sleeplessval@gmail.com>"]
edition = "2018" edition = "2018"

View file

@ -13,8 +13,40 @@ fn main() {
let dir = i_dir.as_path(); let dir = i_dir.as_path();
let config = Config::new(); let config = Config::new();
// Parse arguments and handle them.
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
let mut error: Option<String> = Some("open: missing file operand.".to_string());
for arg in &args[1..] {
match arg.as_str() {
"-h" |
"--help" => {
println!("open
Valerie Wolfe <sleeplessval@gmail.com>
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 default = ".".to_string();
let arg_target = args.get(1); let arg_target = args.get(1);
let i_target = arg_target.unwrap_or(&default); let i_target = arg_target.unwrap_or(&default);