From 2e3e9acf7b3cad0712cba06181811ccca5a07b36 Mon Sep 17 00:00:00 2001 From: tottoto Date: Tue, 7 Feb 2023 23:45:32 +0900 Subject: [PATCH] chore(interop): Use pico-args instead of clap (#1242) --- interop/Cargo.toml | 4 ++-- interop/src/bin/client.rs | 32 +++++++++++++++++--------------- interop/src/bin/server.rs | 15 +++++++++++---- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/interop/Cargo.toml b/interop/Cargo.toml index 106e7d4..bc975e6 100644 --- a/interop/Cargo.toml +++ b/interop/Cargo.toml @@ -16,8 +16,8 @@ path = "src/bin/server.rs" [dependencies] async-stream = "0.3" -clap = {version = ">=4.0.26, <4.1", features = ["derive"]} -clap_lex = "=0.3.0" # keeps msrv to 1.60 +strum = {version = "0.24", features = ["derive"]} +pico-args = {version = "0.5", features = ["eq-separator"]} console = "0.15" futures-core = "0.3" futures-util = "0.3" diff --git a/interop/src/bin/client.rs b/interop/src/bin/client.rs index f93baf1..5d8bf63 100644 --- a/interop/src/bin/client.rs +++ b/interop/src/bin/client.rs @@ -1,29 +1,31 @@ -use clap::{ArgAction, Parser}; use interop::client; -use std::time::Duration; +use std::{str::FromStr, time::Duration}; use tonic::transport::Endpoint; use tonic::transport::{Certificate, ClientTlsConfig}; -#[derive(Parser)] +#[derive(Debug)] struct Opts { - #[clap(name = "use_tls", long, action = ArgAction::SetTrue)] use_tls: bool, - - #[arg( - long = "test_case", - use_value_delimiter = true, - num_args(1..), - value_enum, - action = ArgAction::Append - )] test_case: Vec, } +impl Opts { + fn parse() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + Ok(Self { + use_tls: pargs.contains("--use_tls"), + test_case: pargs.value_from_fn("--test_case", |test_case| { + test_case.split(',').map(Testcase::from_str).collect() + })?, + }) + } +} + #[tokio::main] async fn main() -> Result<(), Box> { interop::trace_init(); - let matches = Opts::parse(); + let matches = Opts::parse()?; let test_cases = matches.test_case; @@ -99,8 +101,8 @@ async fn main() -> Result<(), Box> { Ok(()) } -#[derive(Debug, Copy, Clone, clap::ValueEnum)] -#[clap(rename_all = "snake_case")] +#[derive(Debug, strum::EnumString)] +#[strum(serialize_all = "snake_case")] enum Testcase { EmptyUnary, CacheableUnary, diff --git a/interop/src/bin/server.rs b/interop/src/bin/server.rs index bd446a9..3c3cc04 100644 --- a/interop/src/bin/server.rs +++ b/interop/src/bin/server.rs @@ -1,19 +1,26 @@ -use clap::{ArgAction, Parser}; use interop::server; use tonic::transport::Server; use tonic::transport::{Identity, ServerTlsConfig}; -#[derive(Parser)] +#[derive(Debug)] struct Opts { - #[clap(name = "use_tls", long, action = ArgAction::SetTrue)] use_tls: bool, } +impl Opts { + fn parse() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + Ok(Self { + use_tls: pargs.contains("--use_tls"), + }) + } +} + #[tokio::main] async fn main() -> std::result::Result<(), Box> { interop::trace_init(); - let matches = Opts::parse(); + let matches = Opts::parse()?; let addr = "127.0.0.1:10000".parse().unwrap();