Dont panic, exit nicely
This commit is contained in:
parent
0e2e596071
commit
a060780681
4 changed files with 39 additions and 8 deletions
|
@ -37,7 +37,7 @@ pub fn get_matches_for(args: Vec<&str>) -> Result<ArgMatches<'static>> {
|
||||||
return build().get_matches_from_safe(args);
|
return build().get_matches_from_safe(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_verbose(m: ArgMatches) -> u64 {
|
pub fn get_verbose(m: &ArgMatches) -> u64 {
|
||||||
let sub = m.subcommand_matches(&m.subcommand_name().unwrap()).unwrap();
|
let sub = m.subcommand_matches(&m.subcommand_name().unwrap()).unwrap();
|
||||||
m.occurrences_of("verbose") + sub.occurrences_of("verbose")
|
m.occurrences_of("verbose") + sub.occurrences_of("verbose")
|
||||||
}
|
}
|
||||||
|
|
38
src/main.rs
38
src/main.rs
|
@ -5,6 +5,9 @@ extern crate serde_derive;
|
||||||
|
|
||||||
extern crate serde_yaml;
|
extern crate serde_yaml;
|
||||||
|
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
mod config;
|
mod config;
|
||||||
mod process;
|
mod process;
|
||||||
|
@ -14,11 +17,38 @@ mod utils;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
use clap::ArgMatches;
|
||||||
|
use config::Config;
|
||||||
|
|
||||||
|
fn ok_or_exit<T>(res: Result<T, String>) -> T {
|
||||||
|
return match res {
|
||||||
|
Ok(k) => k,
|
||||||
|
Err(err) => {
|
||||||
|
writeln!(io::stderr(), "Error: {:?}", err).unwrap();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_config(args: &ArgMatches) -> Config {
|
||||||
|
let mut config = ok_or_exit(config::get_config());
|
||||||
|
config.verbosity = args::get_verbose(args);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = args::get_matches();
|
let args = args::get_matches();
|
||||||
if args.subcommand_name().unwrap() == "build" {
|
let subcommand = args.subcommand_name().expect("subcommand error");
|
||||||
let mut config = config::get_config().expect("Config error");
|
|
||||||
config.verbosity = args::get_verbose(args);
|
match subcommand {
|
||||||
process::build(config);
|
"build" => {
|
||||||
|
let config = get_config(&args);
|
||||||
|
ok_or_exit(process::build(config));
|
||||||
|
}
|
||||||
|
cmd => {
|
||||||
|
writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,8 @@ use input::read_input_files;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
|
|
||||||
pub fn build(config: Config) {
|
pub fn build(config: Config) -> Result<(), String> {
|
||||||
let input = read_input_files(config.input).unwrap();
|
let input = try!(read_input_files(config.input));
|
||||||
println!("{}", input);
|
println!("{}", input);
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn incorrect_subcommand() {
|
||||||
#[test]
|
#[test]
|
||||||
fn verbose_number() {
|
fn verbose_number() {
|
||||||
fn get_verbose_level(arg_list: Vec<&str>) -> u64 {
|
fn get_verbose_level(arg_list: Vec<&str>) -> u64 {
|
||||||
return args::get_verbose(args::get_matches_for(arg_list).unwrap());
|
return args::get_verbose(&args::get_matches_for(arg_list).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(get_verbose_level(vec!["mdp", "build", "-v"]), 1);
|
assert_eq!(get_verbose_level(vec!["mdp", "build", "-v"]), 1);
|
||||||
|
|
Reference in a new issue