diff --git a/Cargo.lock b/Cargo.lock index 42ede5b..03425fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,6 +126,11 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "pico-args" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pkg-config" version = "0.3.17" @@ -146,6 +151,7 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pico-args 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "signal-hook 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -264,6 +270,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" "checksum num-traits 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "443c53b3c3531dfcbfa499d8893944db78474ad7a1d87fa2d94d1a2231693ac6" "checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" +"checksum pico-args 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" diff --git a/Cargo.toml b/Cargo.toml index 3ad77d6..8da6ba4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,9 @@ serde_json = "1.0.42" toml = "0.5.5" dbus = {version = "*", optional = true} shlex = "0.1.1" +pico-args = "0.3" [features] dbus_support = ["dbus"] linux_eventfd = [] -cgroups = [] \ No newline at end of file +cgroups = [] diff --git a/src/bin/rustysd.rs b/src/bin/rustysd.rs index 6c871c8..3902013 100644 --- a/src/bin/rustysd.rs +++ b/src/bin/rustysd.rs @@ -201,67 +201,49 @@ fn start_signal_handler_thread( handle } +const USAGE: &'static str = "Usage: rustysd [-c | --config PATH] [-d | --dry-run] [-h | --help]"; + #[derive(Default)] struct CliArgs { conf_path: Option, dry_run: bool, show_help: bool, - unknown_arg: Option + free_args: Vec } -fn parse_args() -> CliArgs { - let args = std::env::args().collect::>(); - // ignore exec name - let args = &args[1..]; - - let mut cli_args = CliArgs::default(); - let mut idx = 0; - while idx < args.len() { - match args[idx].as_str() { - "-c" | "--config" => { - if args.len() < idx { - unrecoverable_error(format!("config flag set but no path given")); - } else { - let path_str = args[idx + 1].clone(); - let p = std::path::PathBuf::from(path_str); - if !p.exists() { - unrecoverable_error(format!("config path given that does not exist")); - } - if !p.is_dir() { - unrecoverable_error(format!("config path given that is not a directory")); - } - cli_args.conf_path = Some(p); - idx += 2; - } - } - "-d" | "--dry-run" => { - cli_args.dry_run = true; - idx += 1; - } - "-h" | "--help" => { - cli_args.show_help = true; - idx += 1; - } - unknown => { - cli_args.unknown_arg = Some(unknown.to_string()); - break; - } - } - } - cli_args +fn parse_args() -> Result { + let mut args = pico_args::Arguments::from_env(); + Ok(CliArgs { + conf_path: args.opt_value_from_str(["-c", "--config"])?, + dry_run: args.contains(["-d", "--dry-run"]), + show_help: args.contains(["-h", "--help"]), + free_args: args.free()?, + }) } fn main() { pid1_specific_setup(); - let cli_args = parse_args(); + let cli_args = parse_args() + .unwrap_or_else(|e| { + unrecoverable_error(e.to_string()); + unreachable!(); + }); + + if let Some(path) = &cli_args.conf_path { + if !path.exists() { + unrecoverable_error(format!("config path given that does not exist")); + } + if !path.is_dir() { + unrecoverable_error(format!("config path given that is not a directory")); + } + } - let usage = "Usage: rustysd [-c | --config PATH] [-d | --dry-run] [-h | --help]"; if cli_args.show_help { - println!("{}", usage); + println!("{}", USAGE); std::process::exit(0); - } else if let Some(unknown) = cli_args.unknown_arg { - unrecoverable_error(format!("{}\n\nUnknown cli arg: {}", usage, unknown)); + } else if cli_args.free_args.len() > 0 { + unrecoverable_error(format!("{}\n\nUnknown cli arg(s): {:?}", USAGE, cli_args.free_args)); } let (log_conf, conf) = config::load_config(&cli_args.conf_path);