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"
[dependencies]
async-stream = "0.2.1"
atoi = "0.3.2"
bytes = "0.5.4"
clap = { git = "https://github.com/clap-rs/clap/" }
structopt = "0.3.14"
tokio = { version = "0.2.20", features = ["full"] }
tracing = "0.1.13"
tracing-futures = { version = "0.2.3", features = ["tokio"] }
tracing-subscriber = "0.2.2"
async-stream = "0.2.1"
[dev-dependencies]
# 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 bytes::Bytes;
use clap::Clap;
use std::num::ParseIntError;
use std::str;
use std::time::Duration;
use structopt::StructOpt;
#[derive(Clap, Debug)]
#[clap(name = "mini-redis-cli", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Issue Redis commands")]
#[derive(StructOpt, Debug)]
#[structopt(name = "mini-redis-cli", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Issue Redis commands")]
struct Cli {
#[clap(subcommand)]
#[structopt(subcommand)]
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,
#[clap(name = "port", long = "--port", default_value = DEFAULT_PORT)]
#[structopt(name = "port", long = "--port", default_value = DEFAULT_PORT)]
port: String,
}
#[derive(Clap, Debug)]
#[derive(StructOpt, Debug)]
enum Command {
/// Get the value of key.
Get {
@@ -32,11 +32,11 @@ enum Command {
key: String,
/// Value to set.
#[clap(parse(from_str = bytes_from_str))]
#[structopt(parse(from_str = bytes_from_str))]
value: Bytes,
/// 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>,
},
}
@@ -55,7 +55,7 @@ async fn main() -> mini_redis::Result<()> {
tracing_subscriber::fmt::try_init()?;
// Parse command line arguments
let cli = Cli::parse();
let cli = Cli::from_args();
// 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 clap::Clap;
use structopt::StructOpt;
use tokio::net::TcpListener;
use tokio::signal;
@@ -18,7 +18,7 @@ pub async fn main() -> mini_redis::Result<()> {
// see https://docs.rs/tracing for more info
tracing_subscriber::fmt::try_init()?;
let cli = Cli::parse();
let cli = Cli::from_args();
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
// Bind a TCP listener
@@ -27,9 +27,9 @@ pub async fn main() -> mini_redis::Result<()> {
server::run(listener, signal::ctrl_c()).await
}
#[derive(Clap, Debug)]
#[clap(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
#[derive(StructOpt, Debug)]
#[structopt(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
struct Cli {
#[clap(name = "port", long = "--port")]
#[structopt(name = "port", long = "--port")]
port: Option<String>,
}