From 206386cf9db475f6ea14a81a777000317df66db0 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sun, 13 Aug 2023 21:15:46 +0200 Subject: [PATCH] Upgrade clap --- Cargo.lock | 135 +++++++++++++++++++++++++++++++--------------- Cargo.toml | 2 +- src/bin/bank.rs | 80 ++++++++++++--------------- src/bin/toydb.rs | 10 ++-- src/bin/toysql.rs | 34 +++++------- 5 files changed, 143 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc27b3249..72a1e9de3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,12 +23,52 @@ dependencies = [ ] [[package]] -name = "ansi_term" -version = "0.11.0" +name = "anstream" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" dependencies = [ - "winapi", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c677ab05e09154296dd37acecd46420c17b9713e8366facafa8fc0885167cf4c" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", ] [[package]] @@ -129,34 +169,41 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.33.4" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826bf7bc84f9435630275cb8e802a4a0ec792b615969934bd16d42ffed10f207" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ - "ansi_term", "atty", "bitflags 1.2.1", - "strsim 0.8.0", - "textwrap 0.11.0", - "unicode-width", - "vec_map", + "clap_derive", + "clap_lex 0.2.4", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", ] [[package]] name = "clap" -version = "3.2.25" +version = "4.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd" dependencies = [ - "atty", - "bitflags 1.2.1", - "clap_derive", - "clap_lex", - "indexmap", + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa" +dependencies = [ + "anstream", + "anstyle", + "clap_lex 0.5.0", "once_cell", - "strsim 0.10.0", - "termcolor", - "textwrap 0.16.0", + "strsim", ] [[package]] @@ -181,6 +228,12 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + [[package]] name = "clipboard-win" version = "4.5.0" @@ -192,6 +245,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "config" version = "0.13.3" @@ -571,6 +630,17 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi 0.3.2", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1305,12 +1375,6 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e08d8363704e6c71fc928674353e6b7c23dcea9d82d7012c8faf2a3a025f8d0" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.10.0" @@ -1371,15 +1435,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "textwrap" version = "0.16.0" @@ -1517,7 +1572,7 @@ name = "toydb" version = "0.1.0" dependencies = [ "bincode", - "clap 2.33.4", + "clap 4.3.21", "config", "derivative", "futures", @@ -1589,12 +1644,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index de9eda6c0..21cdd710b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ default-run = "toydb" [dependencies] bincode = "~1.3.3" -clap = "~2.33.3" +clap = { version = "4.3.21", features = ["cargo"] } config = "~0.13.3" derivative = "~2.2.0" futures = "~0.3.15" diff --git a/src/bin/bank.rs b/src/bin/bank.rs index e29aa4527..d23013ab7 100644 --- a/src/bin/bank.rs +++ b/src/bin/bank.rs @@ -1,7 +1,7 @@ /* * Simulates a bank, by creating a set of accounts and making concurrent transfers between them: * - * - Connect to the given toyDB hosts (-h default 127.0.0.1:9605, can give multiple) + * - Connect to the given toyDB hosts (-H default 127.0.0.1:9605, can give multiple) * - Create C customers (-C default 100) * - Create a accounts per customer with initial balance 100 (-a default 10) * - Spawn c concurrent workers (-c default 8) @@ -17,7 +17,6 @@ #![warn(clippy::all)] -use clap::{app_from_crate, crate_authors, crate_description, crate_name, crate_version}; use futures::stream::TryStreamExt as _; use rand::distributions::Distribution; use rand::Rng as _; @@ -29,71 +28,57 @@ use toydb::error::{Error, Result}; #[tokio::main] async fn main() -> Result<()> { - let opts = app_from_crate!() - .arg( - clap::Arg::with_name("host") - .short("h") + let args = clap::command!() + .about("A bank workload, which makes concurrent transfers between accounts.") + .args([ + clap::Arg::new("host") + .short('H') .long("host") .help("Host to connect to, optionally with port number") - .takes_value(true) - .number_of_values(1) - .multiple(true) - .required(true) + .num_args(1..) .default_value("127.0.0.1:9605"), - ) - .arg( - clap::Arg::with_name("concurrency") - .short("c") + clap::Arg::new("concurrency") + .short('c') .long("concurrency") .help("Concurrent workers to spawn") - .takes_value(true) - .required(true) + .value_parser(clap::value_parser!(u64)) .default_value("8"), - ) - .arg( - clap::Arg::with_name("customers") - .short("C") + clap::Arg::new("customers") + .short('C') .long("customers") .help("Number of customers to create") - .takes_value(true) - .required(true) + .value_parser(clap::value_parser!(u64)) .default_value("100"), - ) - .arg( - clap::Arg::with_name("accounts") - .short("a") + clap::Arg::new("accounts") + .short('a') .long("accounts") .help("Number of accounts to create per customer") - .takes_value(true) - .required(true) + .value_parser(clap::value_parser!(u64)) .default_value("10"), - ) - .arg( - clap::Arg::with_name("transactions") - .short("t") + clap::Arg::new("transactions") + .short('t') .long("transactions") .help("Number of account transfers to execute") - .takes_value(true) - .required(true) + .value_parser(clap::value_parser!(u64)) .default_value("1000"), - ) + ]) .get_matches(); Bank::new( - opts.values_of("host").unwrap().map(String::from).collect(), - opts.value_of("concurrency").unwrap().parse()?, - opts.value_of("customers").unwrap().parse()?, - opts.value_of("accounts").unwrap().parse()?, + args.get_many::("host").unwrap().collect(), + *args.get_one::("concurrency").unwrap(), + *args.get_one("customers").unwrap(), + *args.get_one("accounts").unwrap(), ) .await? - .run(opts.value_of("transactions").unwrap().parse()?) + .run(*args.get_one("transactions").unwrap()) .await } struct Bank { clients: Pool, - customers: i64, - customer_accounts: i64, + customers: u64, + customer_accounts: u64, } impl Bank { @@ -103,8 +88,8 @@ impl Bank { async fn new( addrs: Vec, concurrency: u64, - customers: i64, - accounts: i64, + customers: u64, + accounts: u64, ) -> Result { Ok(Self { clients: Pool::new(addrs, concurrency).await?, @@ -203,9 +188,10 @@ impl Bank { /// Verifies that all invariants hold (same total balance, no negative balances). async fn verify(&self) -> Result<()> { let client = self.clients.get().await; - let expect = self.customers * self.customer_accounts * Self::INITIAL_BALANCE as i64; + let expect = self.customers * self.customer_accounts * Self::INITIAL_BALANCE; let balance = - client.execute("SELECT SUM(balance) FROM account").await?.into_value()?.integer()?; + client.execute("SELECT SUM(balance) FROM account").await?.into_value()?.integer()? + as u64; if balance != expect { return Err(Error::Value(format!( "Expected total balance {}, found {}", @@ -225,7 +211,7 @@ impl Bank { } /// Transfers a random amount between two customers, retrying serialization failures. - async fn transfer(&self, from: i64, to: i64) -> Result<()> { + async fn transfer(&self, from: u64, to: u64) -> Result<()> { let client = self.clients.get().await; let attempts = Rc::new(Cell::new(0_u8)); let start = std::time::Instant::now(); diff --git a/src/bin/toydb.rs b/src/bin/toydb.rs index 6f32c7614..415c61186 100644 --- a/src/bin/toydb.rs +++ b/src/bin/toydb.rs @@ -6,7 +6,6 @@ #![warn(clippy::all)] -use clap::{app_from_crate, crate_authors, crate_description, crate_name, crate_version}; use serde_derive::Deserialize; use std::collections::HashMap; use toydb::error::{Error, Result}; @@ -15,17 +14,16 @@ use toydb::Server; #[tokio::main] async fn main() -> Result<()> { - let opts = app_from_crate!() + let args = clap::command!() .arg( - clap::Arg::with_name("config") - .short("c") + clap::Arg::new("config") + .short('c') .long("config") .help("Configuration file path") - .takes_value(true) .default_value("/etc/toydb.yaml"), ) .get_matches(); - let cfg = Config::new(opts.value_of("config").unwrap())?; + let cfg = Config::new(args.get_one::("config").unwrap().as_ref())?; let loglevel = cfg.log_level.parse::()?; let mut logconfig = simplelog::ConfigBuilder::new(); diff --git a/src/bin/toysql.rs b/src/bin/toysql.rs index 88d82245a..7fc2e0165 100644 --- a/src/bin/toysql.rs +++ b/src/bin/toysql.rs @@ -5,7 +5,6 @@ #![warn(clippy::all)] -use clap::{app_from_crate, crate_authors, crate_description, crate_name, crate_version}; use rustyline::validate::{ValidationContext, ValidationResult, Validator}; use rustyline::{error::ReadlineError, Editor, Modifiers}; use rustyline_derive::{Completer, Helper, Highlighter, Hinter}; @@ -17,37 +16,30 @@ use toydb::Client; #[tokio::main] async fn main() -> Result<()> { - let opts = app_from_crate!() - .arg(clap::Arg::with_name("command")) - .arg(clap::Arg::with_name("headers").short("H").long("headers").help("Show column headers")) - .arg( - clap::Arg::with_name("host") - .short("h") + let opts = clap::command!() + .name("toysql") + .about("A ToyDB client.") + .args([ + clap::Arg::new("command"), + clap::Arg::new("host") + .short('H') .long("host") .help("Host to connect to") - .takes_value(true) - .required(true) .default_value("127.0.0.1"), - ) - .arg( - clap::Arg::with_name("port") - .short("p") + clap::Arg::new("port") + .short('p') .long("port") .help("Port number to connect to") - .takes_value(true) - .required(true) + .value_parser(clap::value_parser!(u16)) .default_value("9605"), - ) + ]) .get_matches(); let mut toysql = - ToySQL::new(opts.value_of("host").unwrap(), opts.value_of("port").unwrap().parse()?) + ToySQL::new(opts.get_one::("host").unwrap(), *opts.get_one("port").unwrap()) .await?; - if opts.is_present("headers") { - toysql.show_headers = true - } - if let Some(command) = opts.value_of("command") { + if let Some(command) = opts.get_one::<&str>("command") { toysql.execute(command).await } else { toysql.run().await