diff --git a/src/args.rs b/src/args.rs index 54b37d0..8a60156 100644 --- a/src/args.rs +++ b/src/args.rs @@ -37,7 +37,7 @@ pub fn get_matches_for(args: Vec<&str>) -> Result> { 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(); m.occurrences_of("verbose") + sub.occurrences_of("verbose") } diff --git a/src/main.rs b/src/main.rs index f80acab..c2b180d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ extern crate serde_derive; extern crate serde_yaml; +use std::io::{self, Write}; +use std::process::exit; + mod args; mod config; mod process; @@ -14,11 +17,38 @@ mod utils; #[cfg(test)] mod tests; +use clap::ArgMatches; +use config::Config; + +fn ok_or_exit(res: Result) -> 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() { let args = args::get_matches(); - if args.subcommand_name().unwrap() == "build" { - let mut config = config::get_config().expect("Config error"); - config.verbosity = args::get_verbose(args); - process::build(config); + let subcommand = args.subcommand_name().expect("subcommand error"); + + match subcommand { + "build" => { + let config = get_config(&args); + ok_or_exit(process::build(config)); + } + cmd => { + writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap(); + exit(1); + } } } diff --git a/src/process.rs b/src/process.rs index b1be3d8..84fbfc0 100644 --- a/src/process.rs +++ b/src/process.rs @@ -2,7 +2,8 @@ use input::read_input_files; use config::Config; -pub fn build(config: Config) { - let input = read_input_files(config.input).unwrap(); +pub fn build(config: Config) -> Result<(), String> { + let input = try!(read_input_files(config.input)); println!("{}", input); + return Ok(()); } diff --git a/src/tests/args.rs b/src/tests/args.rs index fdad971..e3571e8 100644 --- a/src/tests/args.rs +++ b/src/tests/args.rs @@ -16,7 +16,7 @@ fn incorrect_subcommand() { #[test] fn verbose_number() { 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);