Skip to content

Commit

Permalink
Merge pull request tkeksa#2 from tkeksa/clap3
Browse files Browse the repository at this point in the history
Migrate to clap v3
  • Loading branch information
tkeksa committed Jan 9, 2022
2 parents 06d9872 + 67285be commit 25f79c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl From<clap::Error> for ExtcapError {
fn from(error: clap::Error) -> Self {
ExtcapError {
kind: ExtcapErrorKind::Clap,
message: error.to_string(),
message: format!("{:?}: {}", error.kind, error.to_string()),
}
}
}
Expand Down
56 changes: 32 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ type TillCaptureResult<T> = Result<TillCaptureOutcome<T>, ExtcapError>;
#[derive(Default)]
pub struct Extcap<'a> {
step: ExtcapStep,
app: Option<App<'a, 'a>>,
app: Option<App<'a>>,
app_args: HashSet<String>, // optional user arguments added from interfaces
matches: Option<ArgMatches<'a>>,
matches: Option<ArgMatches>,
version: Option<String>,
helppage: Option<String>,
ws_version: Option<String>,
Expand All @@ -249,61 +249,60 @@ 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)
.value_name("iface")
.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)
.value_name("filter")
.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)
Expand All @@ -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<F>(&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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
Expand All @@ -441,15 +440,15 @@ 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)
.value_name("in-pipe")
.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)
Expand All @@ -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)
Expand Down Expand Up @@ -530,7 +529,16 @@ impl<'a> Extcap<'a> {

fn run_till_capture<T: ExtcapListener>(&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) {
Expand Down

0 comments on commit 25f79c7

Please sign in to comment.