From 7cfaedd1cb06aec9ac02cf6401bc70b071b2cc6b Mon Sep 17 00:00:00 2001 From: MT <59728838+mt-empty@users.noreply.github.com> Date: Fri, 14 Apr 2023 17:50:41 +1000 Subject: [PATCH] fixed issue with argument optionality --- Cargo.toml | 2 +- src/main.rs | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a23fe47..d12200a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shortcut" -version = "1.0.0" +version = "1.0.1" 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 91fa51d..87624d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ const ANSI_BOLD_OFF: &str = "\x1b[22m"; struct Cli { /// The program name, e.g. `firefox` #[arg()] - program_name: String, + program_name: Option, /// List all available shortcut pages in the cache #[arg(short, long, action = clap::ArgAction::SetTrue)] @@ -49,8 +49,6 @@ struct Cli { fn main() { let cli = Cli::parse(); - //TODO, validate program_name - let program_name = cli.program_name.trim().to_owned(); let is_colour_on = !cli.no_colour.unwrap(); if cli.update.unwrap() { match update() { @@ -62,17 +60,28 @@ fn main() { } } else if cli.list.unwrap() { list_shortcuts(); - } else if program_name != "" { - get_shortcut_page(&program_name, is_colour_on); } else { - eprintln!("No valid argument specified"); - exit(1) + match cli.program_name.map(|s| s.trim().to_owned()) { + Some(program_name) if !program_name.is_empty() => { + // let program_name = program_name.trim().to_owned(); + get_shortcut_page(&program_name, is_colour_on); + } + _ => { + eprintln!("No valid argument specified"); + exit(1) + } + } } } fn has_write_permission(path: PathBuf) -> Result { - let metadata = fs::metadata(path)?; - Ok(!metadata.permissions().readonly()) + match fs::create_dir_all(path.to_owned()) { + Ok(()) => { + let metadata = fs::metadata(path)?; + Ok(!metadata.permissions().readonly()) + } + Err(error) => Err(error), + } } fn update() -> Result<()> {