Refactor complete CLI code

This commit is contained in:
Matthias Beyer 2015-10-25 19:47:17 +01:00
parent e19f7117e6
commit 3556e5adc6
3 changed files with 15 additions and 46 deletions

View file

@ -1,70 +1,37 @@
extern crate clap; extern crate clap;
use clap::{App, ArgMatches}; use clap::{App, ArgMatches};
struct ConfigBase {
verbosity : bool,
debugging : bool,
}
pub struct ModuleConfig { pub struct ModuleConfig {
base : ConfigBase,
pub load : bool, pub load : bool,
} }
impl ModuleConfig { impl ModuleConfig {
pub fn new() -> ModuleConfig { pub fn new() -> ModuleConfig {
ModuleConfig { ModuleConfig {
base: ConfigBase {
verbosity: false,
debugging: false,
},
load: false, load: false,
} }
} }
} }
pub struct Config { pub struct Config<'a> {
base : ConfigBase,
pub module_configs : Vec<ModuleConfig>, pub module_configs : Vec<ModuleConfig>,
pub cli_matches : ArgMatches<'a, 'a>,
} }
impl Config { impl<'a> Config<'a> {
pub fn new() -> Config { pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> Config<'a> {
Config { Config {
base: ConfigBase {
verbosity: false,
debugging: false,
},
module_configs: vec![], module_configs: vec![],
cli_matches: app.get_matches(),
} }
} }
pub fn is_verbose(&self) -> bool { pub fn is_verbose(&self) -> bool {
self.base.verbosity self.cli_matches.is_present("verbose")
} }
pub fn is_debugging(&self) -> bool { 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");
}

View file

@ -2,6 +2,7 @@
use cli::Config; use cli::Config;
use runtime::Runtime; use runtime::Runtime;
use clap::App;
mod cli; mod cli;
mod runtime; mod runtime;
@ -9,8 +10,9 @@ mod module;
mod storage; mod storage;
fn main() { fn main() {
let mut config = Config::new(); let yaml = load_yaml!("../etc/cli.yml");
cli::configure(&mut config); let app = App::from_yaml(yaml);
let mut config = Config::new(app);
let rt = Runtime::new(config); let rt = Runtime::new(config);

View file

@ -3,13 +3,13 @@ pub use cli::Config;
use std::io::stderr; use std::io::stderr;
use std::io::Write; use std::io::Write;
pub struct Runtime { pub struct Runtime<'a> {
config : Config, config : Config<'a>,
} }
impl Runtime { impl<'a> Runtime<'a> {
pub fn new(config : Config) -> Runtime { pub fn new(config : Config<'a>) -> Runtime<'a> {
Runtime { Runtime {
config: config, config: config,
} }