diff options
| author | Teddy Wing | 2018-10-05 18:18:55 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-10-05 18:18:55 +0200 |
| commit | 42fa76b3472c2dae8fd6db6649058e54e9f67003 (patch) | |
| tree | c29053ef205e0497213a1b2c182ee41c6169e1ba | |
| parent | 9a36a0c9a7144e1450fd76afed2f67f4fa3e9b1e (diff) | |
| download | dome-key-map-42fa76b3472c2dae8fd6db6649058e54e9f67003.tar.bz2 | |
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.
| -rw-r--r-- | src/config.rs | 49 | ||||
| -rw-r--r-- | src/lib.rs | 3 |
2 files changed, 52 insertions, 0 deletions
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 +} @@ -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}; |
