From 0bc2902b94370447adab0ad68892c1dc543756f4 Mon Sep 17 00:00:00 2001 From: Kevin Ryan <614956+ktr@users.noreply.github.com> Date: Sat, 24 Jul 2021 10:16:58 -0400 Subject: [PATCH] Adding -h flag for help and better error messages --- src/lib.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 12 +++++++++++- src/wb.rs | 1 - 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 899d319..d52ab59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,8 @@ pub struct Config { tab: SheetNameOrNum, /// How many rows should we print? nrows: Option, + /// Should we show usage information? + want_help: bool, } pub enum ConfigError<'a> { @@ -62,7 +64,7 @@ pub enum ConfigError<'a> { impl<'a> fmt::Display for ConfigError<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - ConfigError::NeedPathAndTab(exe) => write!(f, "Usage: {} [-n num-rows]", exe), + ConfigError::NeedPathAndTab(exe) => write!(f, "need to provide path and tab when running '{}'. See usage below.", exe), ConfigError::NeedTab => write!(f, "must also provide which tab you want to view in workbook"), ConfigError::RowsMustBeInt => write!(f, "number of rows must be an integer value"), ConfigError::NeedNumRows => write!(f, "must provide number of rows when using -n"), @@ -76,14 +78,22 @@ impl Config { if args.len() < 2 { return Err(ConfigError::NeedPathAndTab(&args[0])) } else if args.len() < 3 { - return Err(ConfigError::NeedTab) + return match args[1].as_ref() { + "-h" | "--help" => Ok(Config { + workbook_path: "".to_owned(), + tab: SheetNameOrNum::Num(0), + nrows: None, + want_help: true, + }), + _ => Err(ConfigError::NeedTab) + } } let workbook_path = args[1].clone(); let tab = match args[2].parse::() { Ok(num) => SheetNameOrNum::Num(num), Err(_) => SheetNameOrNum::Name(args[2].clone()) }; - let mut config = Config { workbook_path, tab, nrows: None }; + let mut config = Config { workbook_path, tab, nrows: None, want_help: false }; let mut iter = args[3..].iter(); while let Some(flag) = iter.next() { let flag = &flag[..]; @@ -107,6 +117,10 @@ impl Config { } pub fn run(config: Config) -> Result<(), String> { + if config.want_help { + usage(); + std::process::exit(0); + } match crate::Workbook::new(&config.workbook_path) { Ok(mut wb) => { let sheets = wb.sheets(); @@ -131,3 +145,28 @@ pub fn run(config: Config) -> Result<(), String> { Err(e) => Err(e) } } + +pub fn usage() { + println!(concat!( + "\n", + "xlcat 0.1.2\n", + "Kevin Ryan \n", + "\n", + "xlcat is like cat, but for Excel files (xlsx files to be precise). You simply\n", + "give it the path of the xlsx and the tab you want to view, and it prints the\n", + "data in that tab to your screen in a comma-delimited format.\n", + "\n", + "You can read about the project at https://xlpro.tips/posts/xlcat. The project\n", + "page is hosted at https://github.com/xlprotips/xl.\n", + "\n", + "USAGE:\n", + " xlcat PATH TAB [-n NUM] [-h | --help]\n", + "\n", + "ARGS:\n", + " PATH Where the xlsx file is located on your filesystem.\n", + " TAB Which tab in the xlsx you want to print to screen.\n", + "\n", + "OPTIONS:\n", + " -n Limit the number of rows we print to .\n", + )); +} diff --git a/src/main.rs b/src/main.rs index c3b83b9..eb700c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,10 @@ fn main() { let args: Vec = env::args().collect(); let config = xl::Config::new(&args).unwrap_or_else(|err| { match err { + xl::ConfigError::NeedPathAndTab(_) => { + eprintln!("Error: {}", err); + xl::usage(); + }, xl::ConfigError::NeedTab => { eprintln!("Error: {}", err); if let Ok(mut wb) = xl::Workbook::open(&args[1]) { @@ -12,9 +16,15 @@ fn main() { for sheet_name in wb.sheets().by_name() { eprintln!(" {}", sheet_name); } + } else { + eprintln!("(that workbook also does not seem to exist or is not a valid xlsx file)"); } + eprintln!("\nSee help by using -h flag."); + }, + _ => { + eprintln!("Error: {}", err); + eprintln!("\nSee help by using -h flag."); }, - _ => eprintln!("Error: {}", err), } process::exit(1); }); diff --git a/src/wb.rs b/src/wb.rs index 625c49b..d69cc2f 100644 --- a/src/wb.rs +++ b/src/wb.rs @@ -80,7 +80,6 @@ impl SheetMap { /// because I expect people who will use this library will be very used to that "style" and may /// expect the same thing in this library. If it becomes an issue, we can change it later). pub fn by_name(&self) -> Vec<&str> { - println!("{:?}", self.sheets_by_num); self.sheets_by_num .iter() .filter(|&s| s.is_some())