Skip to content

Commit

Permalink
Adding -h flag for help and better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr committed Jul 24, 2021
1 parent 3b0048d commit 0bc2902
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
45 changes: 42 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct Config {
tab: SheetNameOrNum,
/// How many rows should we print?
nrows: Option<u32>,
/// Should we show usage information?
want_help: bool,
}

pub enum ConfigError<'a> {
Expand All @@ -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: {} <path-to-xlsx> <tab> [-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"),
Expand All @@ -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::<usize>() {
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[..];
Expand All @@ -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();
Expand All @@ -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 <[email protected]>\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 <NUM> Limit the number of rows we print to <NUM>.\n",
));
}
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ fn main() {
let args: Vec<String> = 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]) {
eprintln!("The following sheets are available in '{}':", &args[1]);
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);
});
Expand Down
1 change: 0 additions & 1 deletion src/wb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 0bc2902

Please sign in to comment.