Skip to content

Commit

Permalink
chore: upgrade clap to v4, refactor example arguments (#616)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Avital <[email protected]>
  • Loading branch information
p-avital and Pierre Avital authored Dec 7, 2023
1 parent 9f7a37e commit db235af
Show file tree
Hide file tree
Showing 32 changed files with 748 additions and 1,387 deletions.
220 changes: 115 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async-std = { version = "=1.12.0", default-features = false } # Default features
async-trait = "0.1.60"
base64 = "0.21.4"
bincode = "1.3.3"
clap = "3.2.23"
clap = { version = "4.4.11", features = ["derive"] }
const_format = "0.2.30"
crc = "3.0.1"
criterion = "0.5"
Expand Down Expand Up @@ -128,7 +128,7 @@ rustls = { version = "0.21.5", features = ["dangerous_configuration"] }
rustls-native-certs = "0.6.2"
rustls-pemfile = "1.0.2"
schemars = "0.8.12"
secrecy = {version = "0.8.0", features = ["serde", "alloc"]}
secrecy = { version = "0.8.0", features = ["serde", "alloc"] }
serde = { version = "1.0.154", default-features = false, features = [
"derive",
] } # Default features are disabled due to usage in no_std crates
Expand All @@ -137,7 +137,7 @@ serde_yaml = "0.9.19"
sha3 = "0.10.6"
shared_memory = "0.12.4"
shellexpand = "3.0.0"
socket2 = { version ="0.5.1", features = [ "all" ] }
socket2 = { version = "0.5.1", features = ["all"] }
stop-token = "0.7.0"
syn = "2.0"
tide = "0.16.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ transport_unixpipe = ["zenoh/transport_unixpipe"]

[dependencies]
async-std = { workspace = true, features = ["attributes"] }
clap = { workspace = true }
clap = { workspace = true, features = ["derive"] }
env_logger = { workspace = true }
flume = { workspace = true }
futures = { workspace = true }
Expand Down
60 changes: 13 additions & 47 deletions examples/examples/z_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use clap::{App, Arg};
use clap::Parser;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_examples::CommonArgs;

#[async_std::main]
async fn main() {
Expand All @@ -31,51 +32,16 @@ async fn main() {
session.close().res().await.unwrap();
}

fn parse_args() -> (Config, String) {
let args = App::new("zenoh delete example")
.arg(
Arg::from_usage("-m, --mode=[MODE] 'The zenoh session mode (peer by default).")
.possible_values(["peer", "client"]),
)
.arg(Arg::from_usage(
"-e, --connect=[ENDPOINT]... 'Endpoints to connect to.'",
))
.arg(Arg::from_usage(
"-l, --listen=[ENDPOINT]... 'Endpoints to listen on.'",
))
.arg(Arg::from_usage(
"-c, --config=[FILE] 'A configuration file.'",
))
.arg(
Arg::from_usage(
"-k, --key=[KEYEXPR] 'The key expression matching resources to delete.'",
)
.default_value("demo/example/zenoh-rs-put"),
)
.arg(Arg::from_usage(
"--no-multicast-scouting 'Disable the multicast-based scouting mechanism.'",
))
.get_matches();

let mut config = if let Some(conf_file) = args.value_of("config") {
Config::from_file(conf_file).unwrap()
} else {
Config::default()
};
if let Some(Ok(mode)) = args.value_of("mode").map(|mode| mode.parse()) {
config.set_mode(Some(mode)).unwrap();
}
if let Some(values) = args.values_of("connect") {
config.connect.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if let Some(values) = args.values_of("listen") {
config.listen.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if args.is_present("no-multicast-scouting") {
config.scouting.multicast.set_enabled(Some(false)).unwrap();
}

let key_expr = args.value_of("key").unwrap().to_string();
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
struct Args {
#[arg(short, long, default_value = "demo/example/zenoh-rs-put")]
/// The key expression to write to.
key: KeyExpr<'static>,
#[command(flatten)]
common: CommonArgs,
}

(config, key_expr)
fn parse_args() -> (Config, KeyExpr<'static>) {
let args = Args::parse();
(args.common.into(), args.key)
}
67 changes: 16 additions & 51 deletions examples/examples/z_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use clap::{App, Arg};
use clap::Parser;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_examples::CommonArgs;
use zenoh_ext::SubscriberForward;

#[async_std::main]
Expand All @@ -34,55 +35,19 @@ async fn main() {
subscriber.forward(publisher).await.unwrap();
}

fn parse_args() -> (Config, String, String) {
let args = App::new("zenoh sub example")
.arg(
Arg::from_usage("-m, --mode=[MODE] 'The zenoh session mode (peer by default).")
.possible_values(["peer", "client"]),
)
.arg(Arg::from_usage(
"-e, --connect=[ENDPOINT]... 'Endpoints to connect to.'",
))
.arg(Arg::from_usage(
"-l, --listen=[ENDPOINT]... 'Endpoints to listen on.'",
))
.arg(
Arg::from_usage("-k, --key=[KEYEXPR] 'The key expression to subscribe to.'")
.default_value("demo/example/**"),
)
.arg(
Arg::from_usage("-f, --forward=[KEYEXPR] 'The key expression to forward to.'")
.default_value("demo/forward"),
)
.arg(Arg::from_usage(
"-c, --config=[FILE] 'A configuration file.'",
))
.arg(Arg::from_usage(
"--no-multicast-scouting 'Disable the multicast-based scouting mechanism.'",
))
.get_matches();

let mut config = if let Some(conf_file) = args.value_of("config") {
Config::from_file(conf_file).unwrap()
} else {
Config::default()
};
if let Some(Ok(mode)) = args.value_of("mode").map(|mode| mode.parse()) {
config.set_mode(Some(mode)).unwrap();
}
if let Some(values) = args.values_of("connect") {
config.connect.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if let Some(values) = args.values_of("listen") {
config.listen.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if args.is_present("no-multicast-scouting") {
config.scouting.multicast.set_enabled(Some(false)).unwrap();
}

let key_expr = args.value_of("key").unwrap().to_string();

let forward = args.value_of("forward").unwrap().to_string();
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
struct Args {
#[arg(short, long, default_value = "demo/example/**")]
/// The key expression to subscribe to.
key: KeyExpr<'static>,
#[arg(short, long, default_value = "demo/forward")]
/// The key expression to forward to.
forward: KeyExpr<'static>,
#[command(flatten)]
common: CommonArgs,
}

(config, key_expr, forward)
fn parse_args() -> (Config, KeyExpr<'static>, KeyExpr<'static>) {
let args = Args::parse();
(args.common.into(), args.key, args.forward)
}
112 changes: 45 additions & 67 deletions examples/examples/z_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use clap::{App, Arg};
use clap::Parser;
use std::convert::TryFrom;
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_examples::CommonArgs;

#[async_std::main]
async fn main() {
Expand Down Expand Up @@ -49,72 +50,49 @@ async fn main() {
}
}

fn parse_args() -> (Config, String, Option<String>, QueryTarget, Duration) {
let args = App::new("zenoh query example")
.arg(
Arg::from_usage("-m, --mode=[MODE] 'The zenoh session mode (peer by default).")
.possible_values(["peer", "client"]),
)
.arg(Arg::from_usage(
"-e, --connect=[ENDPOINT]... 'Endpoints to connect to.'",
))
.arg(Arg::from_usage(
"-l, --listen=[ENDPOINT]... 'Endpoints to listen on.'",
))
.arg(
Arg::from_usage("-s, --selector=[SELECTOR] 'The selection of resources to query'")
.default_value("demo/example/**"),
)
.arg(Arg::from_usage(
"-v, --value=[VALUE] 'An optional value to put in the query.'",
))
.arg(
Arg::from_usage("-t, --target=[TARGET] 'The target queryables of the query'")
.possible_values(["BEST_MATCHING", "ALL", "ALL_COMPLETE"])
.default_value("BEST_MATCHING"),
)
.arg(
Arg::from_usage("-o, --timeout=[TIME] 'The query timeout in milliseconds'")
.default_value("10000"),
)
.arg(Arg::from_usage(
"-c, --config=[FILE] 'A configuration file.'",
))
.arg(Arg::from_usage(
"--no-multicast-scouting 'Disable the multicast-based scouting mechanism.'",
))
.get_matches();

let mut config = if let Some(conf_file) = args.value_of("config") {
Config::from_file(conf_file).unwrap()
} else {
Config::default()
};
if let Some(Ok(mode)) = args.value_of("mode").map(|mode| mode.parse()) {
config.set_mode(Some(mode)).unwrap();
}
if let Some(values) = args.values_of("connect") {
config.connect.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if let Some(values) = args.values_of("listen") {
config.listen.endpoints = values.map(|v| v.parse().unwrap()).collect();
}
if args.is_present("no-multicast-scouting") {
config.scouting.multicast.set_enabled(Some(false)).unwrap();
}

let selector = args.value_of("selector").unwrap().to_string();

let value = args.value_of("value").map(ToOwned::to_owned);

let target = match args.value_of("target") {
Some("BEST_MATCHING") => QueryTarget::BestMatching,
Some("ALL") => QueryTarget::All,
Some("ALL_COMPLETE") => QueryTarget::AllComplete,
_ => QueryTarget::default(),
};
#[derive(clap::ValueEnum, Clone, Copy, Debug)]
#[value(rename_all = "SCREAMING_SNAKE_CASE")]
enum Qt {
BestMatching,
All,
AllComplete,
}

let timeout = Duration::from_millis(args.value_of("timeout").unwrap().parse::<u64>().unwrap());
#[derive(Parser, Clone, Debug)]
struct Args {
#[arg(short, long, default_value = "demo/example/**")]
/// The selection of resources to query
selector: Selector<'static>,
#[arg(short, long)]
/// An optional value to put in the query.
value: Option<String>,
#[arg(short, long, default_value = "BEST_MATCHING")]
/// The target queryables of the query.
target: Qt,
#[arg(short = 'o', long, default_value = "10000")]
/// The query timeout in milliseconds.
timeout: u64,
#[command(flatten)]
common: CommonArgs,
}

(config, selector, value, target, timeout)
fn parse_args() -> (
Config,
Selector<'static>,
Option<String>,
QueryTarget,
Duration,
) {
let args = Args::parse();
(
args.common.into(),
args.selector,
args.value,
match args.target {
Qt::BestMatching => QueryTarget::BestMatching,
Qt::All => QueryTarget::All,
Qt::AllComplete => QueryTarget::AllComplete,
},
Duration::from_millis(args.timeout),
)
}
Loading

0 comments on commit db235af

Please sign in to comment.