From 67285becdb9f1757bff82c6d4e5c74db7475e7e0 Mon Sep 17 00:00:00 2001 From: Tomas Kukosa Date: Sun, 9 Jan 2022 18:42:30 +0100 Subject: [PATCH] Migrate to clap v3 --- Cargo.toml | 12 +++++------ src/error.rs | 2 +- src/lib.rs | 56 ++++++++++++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6297a16..1e2b166 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,21 +24,21 @@ ctrl-pipe = ["async-api", "tokio/fs", "tokio-util/codec"] [dependencies] bytes = "1.1.0" -clap = "2.34.0" +clap = "3.0.5" log = "0.4.14" pcap-file = "1.1.1" -futures = { version = "0.3.18", optional = true } -tokio = { version = "1.14.0", optional = true } +futures = { version = "0.3.19", optional = true } +tokio = { version = "1.15.0", optional = true } tokio-util = { version = "0.6.9", optional = true } [dev-dependencies] ctrlc = "3.2.1" rand = "0.8.4" serialport = "4.0.1" -simplelog = "0.11.0" -futures = "0.3.16" +simplelog = "0.11.1" +futures = "0.3.19" tokio-serial = "5.4.1" -tokio = { version = "1.14.0", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.15.0", features = ["macros", "rt-multi-thread"] } tokio-util = { version = "0.6.9", features = ["codec"] } [[example]] diff --git a/src/error.rs b/src/error.rs index 75caf78..5ba1650 100644 --- a/src/error.rs +++ b/src/error.rs @@ -72,7 +72,7 @@ impl From for ExtcapError { fn from(error: clap::Error) -> Self { ExtcapError { kind: ExtcapErrorKind::Clap, - message: error.to_string(), + message: format!("{:?}: {}", error.kind, error.to_string()), } } } diff --git a/src/lib.rs b/src/lib.rs index 05b3776..1acc945 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -232,9 +232,9 @@ type TillCaptureResult = Result, ExtcapError>; #[derive(Default)] pub struct Extcap<'a> { step: ExtcapStep, - app: Option>, + app: Option>, app_args: HashSet, // optional user arguments added from interfaces - matches: Option>, + matches: Option, version: Option, helppage: Option, ws_version: Option, @@ -249,23 +249,22 @@ impl<'a> Extcap<'a> { /// Creates a new instance of an `Extcap` requiring a name. pub fn new(name: &'a str) -> Self { let app = App::new(name) - .setting(AppSettings::UnifiedHelpMessage) .setting(AppSettings::AllowNegativeNumbers) //.template(HELP_TEMPLATE) .arg( - Arg::with_name(OPT_EXTCAP_VERSION) + Arg::new(OPT_EXTCAP_VERSION) .long(OPT_EXTCAP_VERSION) .help("Wireshark version") .takes_value(true) .value_name("ver"), ) .arg( - Arg::with_name(OPT_EXTCAP_INTERFACES) + Arg::new(OPT_EXTCAP_INTERFACES) .long(OPT_EXTCAP_INTERFACES) .help("List the extcap Interfaces"), ) .arg( - Arg::with_name(OPT_EXTCAP_INTERFACE) + Arg::new(OPT_EXTCAP_INTERFACE) .long(OPT_EXTCAP_INTERFACE) .help("Specify the extcap interface") .takes_value(true) @@ -273,29 +272,29 @@ impl<'a> Extcap<'a> { .conflicts_with(OPT_EXTCAP_INTERFACES), ) .arg( - Arg::with_name(OPT_EXTCAP_DTLS) + Arg::new(OPT_EXTCAP_DTLS) .long(OPT_EXTCAP_DTLS) .help("List the DLTs"), ) .arg( - Arg::with_name(OPT_EXTCAP_CONFIG) + Arg::new(OPT_EXTCAP_CONFIG) .long(OPT_EXTCAP_CONFIG) .help("List the additional configuration for an interface"), ) .arg( - Arg::with_name(OPT_CAPTURE) + Arg::new(OPT_CAPTURE) .long(OPT_CAPTURE) .help("Run the capture") .requires(OPT_FIFO), ) .group( - ArgGroup::with_name("if_action") + ArgGroup::new("if_action") .args(&[OPT_EXTCAP_DTLS, OPT_EXTCAP_CONFIG, OPT_CAPTURE]) .multiple(false) .requires(OPT_EXTCAP_INTERFACE), ) .arg( - Arg::with_name(OPT_EXTCAP_CAPTURE_FILTER) + Arg::new(OPT_EXTCAP_CAPTURE_FILTER) .long(OPT_EXTCAP_CAPTURE_FILTER) .help("The capture filter") .takes_value(true) @@ -303,7 +302,7 @@ impl<'a> Extcap<'a> { .requires(OPT_CAPTURE), ) .arg( - Arg::with_name(OPT_FIFO) + Arg::new(OPT_FIFO) .long(OPT_FIFO) .help("Dump data to file or fifo") .takes_value(true) @@ -322,23 +321,23 @@ impl<'a> Extcap<'a> { &self.step } - fn take_app(&mut self) -> App<'a, 'a> { + fn take_app(&mut self) -> App<'a> { self.app.take().expect("Extcap invalid state: already run") } fn update_app(&mut self, f: F) where - F: FnOnce(App<'a, 'a>) -> App<'a, 'a>, + F: FnOnce(App<'a>) -> App<'a>, { self.app = Some(f(self.take_app())); } - fn app_arg(&mut self, arg: Arg<'a, 'a>) { + fn app_arg(&mut self, arg: Arg<'a>) { self.app = Some(self.take_app().arg(arg)); } /// Get parsed command line arguments. Provided by `clap::App`. - pub fn get_matches(&self) -> &ArgMatches<'a> { + pub fn get_matches(&self) -> &ArgMatches { self.matches .as_ref() .expect("Extcap invalid state: not run yet") @@ -367,7 +366,7 @@ impl<'a> Extcap<'a> { /// Sets the usage string pub fn usage(&mut self, usage: &'a str) { - self.update_app(|a| a.usage(usage)); + self.update_app(|a| a.override_usage(usage)); } /// Sets the after-help string @@ -408,7 +407,7 @@ impl<'a> Extcap<'a> { return; } - let mut arg = Arg::with_name(ifa.get_name()).long(ifa.get_name()); + let mut arg = Arg::new(ifa.get_name()).long(ifa.get_name()); if let Some(hlp) = ifa.get_display() { arg = arg.help(hlp); } @@ -424,7 +423,7 @@ impl<'a> Extcap<'a> { } self.reload_opt = true; self.app_arg( - Arg::with_name(OPT_EXTCAP_RELOAD_OPTION) + Arg::new(OPT_EXTCAP_RELOAD_OPTION) .long(OPT_EXTCAP_RELOAD_OPTION) .help("Reload values for the given argument") .takes_value(true) @@ -441,7 +440,7 @@ impl<'a> Extcap<'a> { } self.control = true; self.app_arg( - Arg::with_name(OPT_EXTCAP_CONTROL_IN) + Arg::new(OPT_EXTCAP_CONTROL_IN) .long(OPT_EXTCAP_CONTROL_IN) .help("The pipe for control messages from toolbar") .takes_value(true) @@ -449,7 +448,7 @@ impl<'a> Extcap<'a> { .requires(OPT_CAPTURE), ); self.app_arg( - Arg::with_name(OPT_EXTCAP_CONTROL_OUT) + Arg::new(OPT_EXTCAP_CONTROL_OUT) .long(OPT_EXTCAP_CONTROL_OUT) .help("The pipe for control messages to toolbar") .takes_value(true) @@ -466,12 +465,12 @@ impl<'a> Extcap<'a> { } self.ifc_debug = true; self.app_arg( - Arg::with_name(OPT_DEBUG) + Arg::new(OPT_DEBUG) .long(OPT_DEBUG) .help("Print additional messages"), ); self.app_arg( - Arg::with_name(OPT_DEBUG_FILE) + Arg::new(OPT_DEBUG_FILE) .long(OPT_DEBUG_FILE) .help("Print debug messages to file") .takes_value(true) @@ -530,7 +529,16 @@ impl<'a> Extcap<'a> { fn run_till_capture(&mut self, listener: &mut T) -> TillCaptureResult<()> { // Save matches for listener - self.matches = Some(self.take_app().get_matches_safe()?); + self.matches = match self.take_app().try_get_matches() { + Ok(m) => Some(m), + Err(cerr) => match cerr.kind { + clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => { + print!("{}", cerr.to_string()); + return Ok(TillCaptureOutcome::Finish(())); + } + _ => return Err(cerr.into()), + }, + }; // Determine the step self.step = if self.get_matches().is_present(OPT_EXTCAP_INTERFACES) {