Skip to content

Commit 069a8e5

Browse files
authored
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.
1 parent 24d9d9f commit 069a8e5

File tree

5 files changed

+91
-71
lines changed

5 files changed

+91
-71
lines changed

Cargo.lock

Lines changed: 64 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async-stream = "0.3.0"
2525
atoi = "0.3.2"
2626
bytes = "1"
2727
rand = "0.8.5"
28-
structopt = "0.3.14"
28+
clap = { version = "3.1.18", features = ["derive"] }
2929
tokio = { version = "1", features = ["full"] }
3030
tokio-stream = "0.1"
3131
tracing = "0.1.34"
@@ -45,4 +45,4 @@ opentelemetry-otlp = { version = "0.10.0", optional = true }
4545
tokio = { version = "1", features = ["test-util"] }
4646

4747
[features]
48-
otel = ["dep:opentelemetry", "dep:tracing-opentelemetry", "dep:opentelemetry-aws", "dep:opentelemetry-otlp"]
48+
otel = ["dep:opentelemetry", "dep:tracing-opentelemetry", "dep:opentelemetry-aws", "dep:opentelemetry-otlp"]

src/bin/cli.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
use mini_redis::{client, DEFAULT_PORT};
22

33
use bytes::Bytes;
4+
use clap::{Parser, Subcommand};
45
use std::num::ParseIntError;
56
use std::str;
67
use std::time::Duration;
7-
use structopt::StructOpt;
88

9-
#[derive(StructOpt, Debug)]
10-
#[structopt(name = "mini-redis-cli", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Issue Redis commands")]
9+
#[derive(Parser, Debug)]
10+
#[clap(
11+
name = "mini-redis-cli",
12+
version,
13+
author,
14+
about = "Issue Redis commands"
15+
)]
1116
struct Cli {
12-
#[structopt(subcommand)]
17+
#[clap(subcommand)]
1318
command: Command,
1419

15-
#[structopt(name = "hostname", long = "--host", default_value = "127.0.0.1")]
20+
#[clap(name = "hostname", long, default_value = "127.0.0.1")]
1621
host: String,
1722

18-
#[structopt(name = "port", long = "--port", default_value = DEFAULT_PORT)]
19-
port: String,
23+
#[clap(long, default_value_t = DEFAULT_PORT)]
24+
port: u16,
2025
}
2126

22-
#[derive(StructOpt, Debug)]
27+
#[derive(Subcommand, Debug)]
2328
enum Command {
2429
/// Get the value of key.
2530
Get {
@@ -32,19 +37,19 @@ enum Command {
3237
key: String,
3338

3439
/// Value to set.
35-
#[structopt(parse(from_str = bytes_from_str))]
40+
#[clap(parse(from_str = bytes_from_str))]
3641
value: Bytes,
3742

3843
/// Expire the value after specified amount of time
39-
#[structopt(parse(try_from_str = duration_from_ms_str))]
44+
#[clap(parse(try_from_str = duration_from_ms_str))]
4045
expires: Option<Duration>,
4146
},
4247
/// Publisher to send a message to a specific channel.
4348
Publish {
4449
/// Name of channel
4550
channel: String,
4651

47-
#[structopt(parse(from_str = bytes_from_str))]
52+
#[clap(parse(from_str = bytes_from_str))]
4853
/// Message to publish
4954
message: Bytes,
5055
},
@@ -70,7 +75,7 @@ async fn main() -> mini_redis::Result<()> {
7075
tracing_subscriber::fmt::try_init()?;
7176

7277
// Parse command line arguments
73-
let cli = Cli::from_args();
78+
let cli = Cli::parse();
7479

7580
// Get the remote address to connect to
7681
let addr = format!("{}:{}", cli.host, cli.port);

src/bin/server.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use mini_redis::{server, DEFAULT_PORT};
1010

11-
use structopt::StructOpt;
11+
use clap::Parser;
1212
use tokio::net::TcpListener;
1313
use tokio::signal;
1414

@@ -32,8 +32,8 @@ use tracing_subscriber::{
3232
pub async fn main() -> mini_redis::Result<()> {
3333
set_up_logging()?;
3434

35-
let cli = Cli::from_args();
36-
let port = cli.port.as_deref().unwrap_or(DEFAULT_PORT);
35+
let cli = Cli::parse();
36+
let port = cli.port.unwrap_or(DEFAULT_PORT);
3737

3838
// Bind a TCP listener
3939
let listener = TcpListener::bind(&format!("127.0.0.1:{}", port)).await?;
@@ -43,11 +43,11 @@ pub async fn main() -> mini_redis::Result<()> {
4343
Ok(())
4444
}
4545

46-
#[derive(StructOpt, Debug)]
47-
#[structopt(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
46+
#[derive(Parser, Debug)]
47+
#[clap(name = "mini-redis-server", version, author, about = "A Redis server")]
4848
struct Cli {
49-
#[structopt(name = "port", long = "--port")]
50-
port: Option<String>,
49+
#[clap(long)]
50+
port: Option<u16>,
5151
}
5252

5353
#[cfg(not(feature = "otel"))]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use shutdown::Shutdown;
5555
/// Default port that a redis server listens on.
5656
///
5757
/// Used if no port is specified.
58-
pub const DEFAULT_PORT: &str = "6379";
58+
pub const DEFAULT_PORT: u16 = 6379;
5959

6060
/// Error returned by most functions.
6161
///

0 commit comments

Comments
 (0)