Don't panic on wrong argument, instead display error message

This commit is contained in:
southerntofu 2022-01-06 17:46:25 +01:00
parent 8fa9d7dca0
commit 628b6372aa
1 changed files with 20 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use std::env::{set_current_dir as cd, set_var};
use structopt::StructOpt;
use structopt::clap::ErrorKind as ClapError;
// For UNIX extended metadata
mod cli;
@ -11,11 +12,27 @@ mod task;
use log::Context;
fn main() -> Result<(), std::io::Error> {
let mut context = Context::new();
// TODO: use StructOpt::from_iter_safe so we can hook on Cli error
// to display additional unknown_arg error message.
let cmd = cli::Cli::from_args();
let mut context = Context::new();
//let cmd = cli::Cli::from_args();
let res = cli::Cli::from_iter_safe(std::env::args());
if res.is_err() {
let e = res.unwrap_err();
match &e.kind {
ClapError::UnknownArgument => {
context.insert("$i18n_arg".to_string(), e.info.unwrap().first().unwrap().to_string());
log::error("unknown_arg", &context);
std::process::exit(1);
},
/*ClapError::HelpDisplayed {
e.exit()
}*/
_ => e.exit()
}
}
let cmd = res.unwrap();
let basedir = cmd.basedir();
context.insert("$i18n_basedir".to_string(), basedir.to_str().unwrap().to_string());