From ced77f9aa2b9020ff0e7ad24b3d6e756bfb2d46b Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 18 Aug 2022 11:54:23 +0300 Subject: [PATCH] cli/show: add possibility to print path and link --- src/cli/show.rs | 51 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/cli/show.rs b/src/cli/show.rs index 17c2046..2e367af 100644 --- a/src/cli/show.rs +++ b/src/cli/show.rs @@ -19,8 +19,25 @@ use crate::repo::Repository; #[derive(clap::Args)] pub struct Args { - #[clap(long)] - print_path: bool, + part: Option, +} + +#[derive(clap::ValueEnum, Clone)] +enum PrintPart { + Path, + Link, +} + +impl std::str::FromStr for PrintPart { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "path" => Ok(PrintPart::Path), + "link" => Ok(PrintPart::Link), + _ => Err(r#"You can display only "path" or "link""#), + } + } } pub fn execute(repo: impl Repository, args: Args) { @@ -34,15 +51,25 @@ pub fn execute(repo: impl Repository, args: Args) { } }; - if args.print_path { - println!( - "{}", - task.path - .unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir")) - .to_string_lossy() - ) - } else { - println!("Information about your current task:"); - print_task_detail(&task); + match args.part { + None => { + println!("Information about your current task:"); + print_task_detail(&task); + } + Some(PrintPart::Path) => { + println!( + "{}", + task.path + .unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir")) + .to_string_lossy() + ) + } + Some(PrintPart::Link) => { + if let Some(link) = task.link { + println!("{}", link) + } else { + eprintln!("The current task doesn't contain link") + } + } } }