From 3556e5adc6cfc9e1c1c14a4a49e370346909ca17 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 25 Oct 2015 19:47:17 +0100 Subject: [PATCH] Refactor complete CLI code --- src/cli.rs | 47 +++++++---------------------------------------- src/main.rs | 6 ++++-- src/runtime.rs | 8 ++++---- 3 files changed, 15 insertions(+), 46 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index b493943a..114f2b51 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,70 +1,37 @@ extern crate clap; use clap::{App, ArgMatches}; -struct ConfigBase { - verbosity : bool, - debugging : bool, -} - pub struct ModuleConfig { - base : ConfigBase, pub load : bool, } impl ModuleConfig { pub fn new() -> ModuleConfig { ModuleConfig { - base: ConfigBase { - verbosity: false, - debugging: false, - }, load: false, } } } -pub struct Config { - base : ConfigBase, +pub struct Config<'a> { pub module_configs : Vec, + pub cli_matches : ArgMatches<'a, 'a>, } -impl Config { - pub fn new() -> Config { +impl<'a> Config<'a> { + pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> Config<'a> { Config { - base: ConfigBase { - verbosity: false, - debugging: false, - }, module_configs: vec![], + cli_matches: app.get_matches(), } } pub fn is_verbose(&self) -> bool { - self.base.verbosity + self.cli_matches.is_present("verbose") } pub fn is_debugging(&self) -> bool { - self.base.debugging + self.cli_matches.is_present("debugging") } } -pub fn configure(config : &mut Config) { - let yaml = load_yaml!("../etc/cli.yml"); - let matches = App::from_yaml(yaml).get_matches(); - - parse_global_cfg(&matches, &mut config.base); - - if let Some(matches) = matches.subcommand_matches("test") { - if matches.is_present("verbose") { - println!("Printing verbosely..."); - } else { - println!("Printing normally..."); - } - } -} - -fn parse_global_cfg(matches : &ArgMatches<>, config : &mut ConfigBase) { - config.verbosity = matches.is_present("verbose"); - config.debugging = matches.is_present("debugging"); -} - diff --git a/src/main.rs b/src/main.rs index a94d5886..bfcd9726 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use cli::Config; use runtime::Runtime; +use clap::App; mod cli; mod runtime; @@ -9,8 +10,9 @@ mod module; mod storage; fn main() { - let mut config = Config::new(); - cli::configure(&mut config); + let yaml = load_yaml!("../etc/cli.yml"); + let app = App::from_yaml(yaml); + let mut config = Config::new(app); let rt = Runtime::new(config); diff --git a/src/runtime.rs b/src/runtime.rs index fd62f395..4003ac79 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -3,13 +3,13 @@ pub use cli::Config; use std::io::stderr; use std::io::Write; -pub struct Runtime { - config : Config, +pub struct Runtime<'a> { + config : Config<'a>, } -impl Runtime { +impl<'a> Runtime<'a> { - pub fn new(config : Config) -> Runtime { + pub fn new(config : Config<'a>) -> Runtime<'a> { Runtime { config: config, }