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;
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<ModuleConfig>,
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");
}

View file

@ -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);

View file

@ -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,
}