-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgrade clap to v4, refactor example arguments (#616)
Co-authored-by: Pierre Avital <[email protected]>
- Loading branch information
Showing
32 changed files
with
748 additions
and
1,387 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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), | ||
) | ||
} |
Oops, something went wrong.