Replace structopt with clap (#98)

* Replace `structopt` with `clap`

* Change `port` options from `String` to `u16`

* Shorten clap options config

`name` and `long` use the filed name by default.
This commit is contained in:
Paul Lange
2022-07-19 18:34:12 +02:00
committed by GitHub
parent 24d9d9f466
commit 069a8e5ee0
5 changed files with 91 additions and 71 deletions

113
Cargo.lock generated
View File

@@ -116,17 +116,41 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
version = "3.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
checksum = "ab8b79fe3946ceb4a0b1c080b4018992b8d27e9ff363644c1c9b6387c854614d"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
@@ -297,6 +321,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@@ -472,11 +502,11 @@ dependencies = [
"async-stream",
"atoi",
"bytes",
"clap",
"opentelemetry",
"opentelemetry-aws",
"opentelemetry-otlp",
"rand",
"structopt",
"tokio",
"tokio-stream",
"tracing",
@@ -575,6 +605,12 @@ dependencies = [
"tonic-build",
]
[[package]]
name = "os_str_bytes"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4"
[[package]]
name = "parking_lot"
version = "0.12.1"
@@ -702,7 +738,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5"
dependencies = [
"bytes",
"heck",
"heck 0.3.3",
"itertools",
"lazy_static",
"log",
@@ -869,33 +905,9 @@ dependencies = [
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
@@ -923,14 +935,20 @@ dependencies = [
]
[[package]]
name = "textwrap"
version = "0.11.0"
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"unicode-width",
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "thiserror"
version = "1.0.31"
@@ -1220,24 +1238,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[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"
@@ -1341,6 +1347,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@@ -25,7 +25,7 @@ async-stream = "0.3.0"
atoi = "0.3.2"
bytes = "1"
rand = "0.8.5"
structopt = "0.3.14"
clap = { version = "3.1.18", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
tracing = "0.1.34"
@@ -45,4 +45,4 @@ opentelemetry-otlp = { version = "0.10.0", optional = true }
tokio = { version = "1", features = ["test-util"] }
[features]
otel = ["dep:opentelemetry", "dep:tracing-opentelemetry", "dep:opentelemetry-aws", "dep:opentelemetry-otlp"]
otel = ["dep:opentelemetry", "dep:tracing-opentelemetry", "dep:opentelemetry-aws", "dep:opentelemetry-otlp"]

View File

@@ -1,25 +1,30 @@
use mini_redis::{client, DEFAULT_PORT};
use bytes::Bytes;
use clap::{Parser, Subcommand};
use std::num::ParseIntError;
use std::str;
use std::time::Duration;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "mini-redis-cli", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Issue Redis commands")]
#[derive(Parser, Debug)]
#[clap(
name = "mini-redis-cli",
version,
author,
about = "Issue Redis commands"
)]
struct Cli {
#[structopt(subcommand)]
#[clap(subcommand)]
command: Command,
#[structopt(name = "hostname", long = "--host", default_value = "127.0.0.1")]
#[clap(name = "hostname", long, default_value = "127.0.0.1")]
host: String,
#[structopt(name = "port", long = "--port", default_value = DEFAULT_PORT)]
port: String,
#[clap(long, default_value_t = DEFAULT_PORT)]
port: u16,
}
#[derive(StructOpt, Debug)]
#[derive(Subcommand, Debug)]
enum Command {
/// Get the value of key.
Get {
@@ -32,11 +37,11 @@ enum Command {
key: String,
/// Value to set.
#[structopt(parse(from_str = bytes_from_str))]
#[clap(parse(from_str = bytes_from_str))]
value: Bytes,
/// Expire the value after specified amount of time
#[structopt(parse(try_from_str = duration_from_ms_str))]
#[clap(parse(try_from_str = duration_from_ms_str))]
expires: Option<Duration>,
},
/// Publisher to send a message to a specific channel.
@@ -44,7 +49,7 @@ enum Command {
/// Name of channel
channel: String,
#[structopt(parse(from_str = bytes_from_str))]
#[clap(parse(from_str = bytes_from_str))]
/// Message to publish
message: Bytes,
},
@@ -70,7 +75,7 @@ async fn main() -> mini_redis::Result<()> {
tracing_subscriber::fmt::try_init()?;
// Parse command line arguments
let cli = Cli::from_args();
let cli = Cli::parse();
// Get the remote address to connect to
let addr = format!("{}:{}", cli.host, cli.port);

View File

@@ -8,7 +8,7 @@
use mini_redis::{server, DEFAULT_PORT};
use structopt::StructOpt;
use clap::Parser;
use tokio::net::TcpListener;
use tokio::signal;
@@ -32,8 +32,8 @@ use tracing_subscriber::{
pub async fn main() -> mini_redis::Result<()> {
set_up_logging()?;
let cli = Cli::from_args();
let port = cli.port.as_deref().unwrap_or(DEFAULT_PORT);
let cli = Cli::parse();
let port = cli.port.unwrap_or(DEFAULT_PORT);
// Bind a TCP listener
let listener = TcpListener::bind(&format!("127.0.0.1:{}", port)).await?;
@@ -43,11 +43,11 @@ pub async fn main() -> mini_redis::Result<()> {
Ok(())
}
#[derive(StructOpt, Debug)]
#[structopt(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
#[derive(Parser, Debug)]
#[clap(name = "mini-redis-server", version, author, about = "A Redis server")]
struct Cli {
#[structopt(name = "port", long = "--port")]
port: Option<String>,
#[clap(long)]
port: Option<u16>,
}
#[cfg(not(feature = "otel"))]

View File

@@ -55,7 +55,7 @@ use shutdown::Shutdown;
/// Default port that a redis server listens on.
///
/// Used if no port is specified.
pub const DEFAULT_PORT: &str = "6379";
pub const DEFAULT_PORT: u16 = 6379;
/// Error returned by most functions.
///