From 42fa76b3472c2dae8fd6db6649058e54e9f67003 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 5 Oct 2018 18:18:55 +0200 Subject: Add a command line option parser Parse command line options in this library. Need to figure out how to communicate these over FFI to the Objective-C code. --- src/config.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ 2 files changed, 52 insertions(+) create mode 100644 src/config.rs (limited to 'src') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..66769a9 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,49 @@ +use getopts::Options; + +#[no_mangle] +#[derive(Default)] +struct Args { + reload: bool, + daemon: bool, +} + +#[no_mangle] +#[derive(Default)] +pub struct Config { + args: Args, +} + +fn print_usage(opts: Options) { + let brief = "Usage: dome-key [options]"; + print!("{}", opts.usage(&brief)); +} + +pub fn parse_args(args: &[String]) -> Config { + let mut opts = Options::new(); + + opts.optflag("d", "daemon", "run the daemon in the current shell"); + opts.optflag("r", "reload-mappings", "reload the mappings file"); + opts.optflag("h", "help", "print this help menu"); + + let matches = match opts.parse(args) { + Ok(m) => m, + Err(e) => panic!(e), + }; + + let mut config = Config::default(); + + if matches.opt_present("h") { + print_usage(opts); + return config; + } + + if matches.opt_present("r") { + config.args.reload = true; + } else if matches.opt_present("d") { + config.args.daemon = true; + } else { + print_usage(opts); + } + + config +} diff --git a/src/lib.rs b/src/lib.rs index 8dc8e93..1f57edb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ extern crate autopilot; #[macro_use] extern crate combine; +extern crate getopts; extern crate libc; #[macro_use] @@ -13,8 +14,10 @@ extern crate stderrlog; extern crate xdg; mod cocoa_bridge; +mod config; mod parser; use parser::{Action, HeadphoneButton, MapAction, MapGroup, MapKind}; pub use cocoa_bridge::*; +pub use config::{Config, parse_args}; -- cgit v1.2.3