use structopt instead of Clap (#41)

mini-redis uses the CLI derive pattern. Clap does not yet have a release
supporting this pattern. Using structopt allows mini-redis to avoid git
dependencies.
This commit is contained in:
Carl Lerche
2020-05-12 16:48:16 -07:00
committed by GitHub
parent 84f7086238
commit fdba12b964
4 changed files with 430 additions and 458 deletions

854
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,14 +5,14 @@ name = "mini-redis"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
async-stream = "0.2.1"
atoi = "0.3.2" atoi = "0.3.2"
bytes = "0.5.4" bytes = "0.5.4"
clap = { git = "https://github.com/clap-rs/clap/" } structopt = "0.3.14"
tokio = { version = "0.2.20", features = ["full"] } tokio = { version = "0.2.20", features = ["full"] }
tracing = "0.1.13" tracing = "0.1.13"
tracing-futures = { version = "0.2.3", features = ["tokio"] } tracing-futures = { version = "0.2.3", features = ["tokio"] }
tracing-subscriber = "0.2.2" tracing-subscriber = "0.2.2"
async-stream = "0.2.1"
[dev-dependencies] [dev-dependencies]
# Enable test-utilities in dev mode only. This is mostly for tests. # Enable test-utilities in dev mode only. This is mostly for tests.

View File

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

View File

@@ -8,7 +8,7 @@
use mini_redis::{server, DEFAULT_PORT}; use mini_redis::{server, DEFAULT_PORT};
use clap::Clap; use structopt::StructOpt;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::signal; use tokio::signal;
@@ -18,7 +18,7 @@ pub async fn main() -> mini_redis::Result<()> {
// see https://docs.rs/tracing for more info // see https://docs.rs/tracing for more info
tracing_subscriber::fmt::try_init()?; tracing_subscriber::fmt::try_init()?;
let cli = Cli::parse(); let cli = Cli::from_args();
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string()); let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
// Bind a TCP listener // Bind a TCP listener
@@ -27,9 +27,9 @@ pub async fn main() -> mini_redis::Result<()> {
server::run(listener, signal::ctrl_c()).await server::run(listener, signal::ctrl_c()).await
} }
#[derive(Clap, Debug)] #[derive(StructOpt, Debug)]
#[clap(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")] #[structopt(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
struct Cli { struct Cli {
#[clap(name = "port", long = "--port")] #[structopt(name = "port", long = "--port")]
port: Option<String>, port: Option<String>,
} }