Implement CLI

pull/39/head
Takashi Kokubun 3 years ago
parent d76ab68d7c
commit 1a8b2b15d1
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD

16
Cargo.lock generated

@ -55,6 +55,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1847abb9cb65d566acd5942e94aea9c8f547ad02c98e1649326fc0e8910b8b1e"
[[package]]
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
"unicode-width",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
@ -184,6 +193,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"
@ -204,6 +219,7 @@ name = "xremap"
version = "0.1.0"
dependencies = [
"evdev",
"getopts",
"nix",
"serde",
"serde_yaml",

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
evdev = "0.11.3"
getopts = "0.2"
nix = "0.23.1"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"

@ -1,9 +1,12 @@
use evdev::EventType;
use std::env;
use getopts::Options;
use std::error::Error;
use std::process::exit;
use crate::config::Config;
extern crate getopts;
mod config;
mod input;
mod output;
@ -33,15 +36,32 @@ fn event_loop(_config: &Config) -> Result<(), Box<dyn Error>> {
}
}
fn usage(program: &str, opts: Options) -> String {
let brief = format!("Usage: {} CONFIG [options]", program);
opts.usage(&brief)
}
fn abort(message: &str) -> ! {
println!("{}", message);
exit(1);
}
fn main() {
let filename = match env::args().nth(1) {
Some(filename) => filename,
None => abort("Usage: xremap <file>"),
let argv: Vec<String> = env::args().collect();
let program = argv[0].clone();
let mut opts = Options::new();
opts.optmulti("", "device", "device name or path", "NAME");
opts.optflag("h", "help", "print this help menu");
let args = match opts.parse(&argv[1..]) {
Ok(args) => args,
Err(e) => abort(&e.to_string()),
};
let filename = match &args.free.iter().map(String::as_str).collect::<Vec<&str>>()[..] {
&[filename] => filename,
&[..] => abort(&usage(&program, opts)),
};
let config = match config::load_config(&filename) {
Ok(config) => config,

Loading…
Cancel
Save