From 6ed943e5574f0ba70856095297844c9eff3ec3e0 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 16 Mar 2020 09:02:49 +0100 Subject: [PATCH] Use structopt instead of docopt --- Cargo.toml | 2 +- examples/cpu_monitor.rs | 59 +++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 733ad3445..3f07e7f7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ version = "1.0" default-features = false [dev-dependencies] -docopt = "1" +structopt = "0.3" lazy_static = "1" rand = "0.7" rand_xorshift = "0.2" diff --git a/examples/cpu_monitor.rs b/examples/cpu_monitor.rs index bbe13164a..e923575c6 100644 --- a/examples/cpu_monitor.rs +++ b/examples/cpu_monitor.rs @@ -1,47 +1,36 @@ -use docopt::Docopt; use std::io; -use std::process; +use structopt::StructOpt; -const USAGE: &str = " -Usage: cpu_monitor [options] - cpu_monitor --help - -A test for monitoring how much CPU usage Rayon consumes under various -scenarios. This test is intended to be executed interactively, like so: - - cargo run --example cpu_monitor -- tasks_ended - -The list of scenarios you can try are as follows: - -- tasks_ended: after all tasks have finished, go to sleep -- task_stall_root: a root task stalls for a very long time -- task_stall_scope: a task in a scope stalls for a very long time - -Options: - -h, --help Show this message. - -d N, --depth N Control how hard the dummy task works [default: 27] -"; +#[derive(StructOpt, Debug)] +pub enum ArgScenario { + /// after all tasks have finished, go to sleep + TaskEnded, + /// a root task stalls for a very long time + TaskStallRoot, + /// a task in a scope stalls for a very long time + TaskStallScope, +} -#[derive(serde::Deserialize)] +#[derive(StructOpt, Debug)] +#[structopt( + about = "A test for monitoring how much CPU usage Rayon consumes under various scenarios." +)] pub struct Args { - arg_scenario: String, + #[structopt(subcommand)] + arg_scenario: ArgScenario, + /// Control how hard the dummy task works + #[structopt(short = "d", long = "depth", default_value = "27")] flag_depth: usize, } fn main() { - let args: &Args = &Docopt::new(USAGE) - .and_then(|d| d.deserialize()) - .unwrap_or_else(|e| e.exit()); + use ArgScenario::*; + let args: Args = Args::from_args(); - match &args.arg_scenario[..] { - "tasks_ended" => tasks_ended(args), - "task_stall_root" => task_stall_root(args), - "task_stall_scope" => task_stall_scope(args), - _ => { - println!("unknown scenario: `{}`", args.arg_scenario); - println!("try --help"); - process::exit(1); - } + match args.arg_scenario { + TaskEnded => tasks_ended(&args), + TaskStallRoot => task_stall_root(&args), + TaskStallScope => task_stall_scope(&args), } }