From f43a9c0034fe8d9b8dda490eae6b8139251f73ba Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 7 Jan 2023 22:53:09 +0000 Subject: [PATCH] Add CLI parsing using clap --- Cargo.lock | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- src/client.rs | 2 +- src/config.rs | 9 +++++++++ src/main.rs | 18 +++++++++++++++++- 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index bf50ed2..b2389c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39" dependencies = [ "bitflags", + "clap_derive", "clap_lex", "is-terminal", "once_cell", @@ -139,6 +140,19 @@ dependencies = [ ] [[package]] +name = "clap_derive" +version = "4.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "clap_lex" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -407,6 +421,12 @@ dependencies = [ ] [[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.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -663,6 +683,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] name = "proc-macro2" version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1040,6 +1084,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" [[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index 749a597..625ecc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ actix = "0.13" actix-rt = "2.7" anyhow = "1.0" chrono = "0.4" -clap = { version = "4.0", features = ["cargo"] } +clap = { version = "4.0", features = ["cargo", "derive", "std", "suggestions", "color"] } futures = "0.3" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } diff --git a/src/client.rs b/src/client.rs index 07e17b7..20451da 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,7 +6,7 @@ use actix::{ }; use clap::{crate_name, crate_version}; use futures::FutureExt; -use irc_proto::{error::ProtocolError, ChannelExt, Command, Message, Response, Prefix}; +use irc_proto::{error::ProtocolError, ChannelExt, Command, Message, Prefix, Response}; use tokio::time::Instant; use tracing::{debug, error, info_span, instrument, warn, Instrument, Span}; diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..042fdef --- /dev/null +++ b/src/config.rs @@ -0,0 +1,9 @@ +use clap::Parser; + +#[derive(Parser)] +#[clap(version = clap::crate_version!(), author = clap::crate_authors!())] +pub struct Args { + /// Turn debugging information on + #[clap(short, long, action = clap::ArgAction::Count)] + pub verbose: u8, +} diff --git a/src/main.rs b/src/main.rs index 269868c..cc9072c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,18 @@ use std::collections::HashMap; use actix::{io::FramedWrite, Actor, Addr, AsyncContext}; use actix_rt::System; +use clap::Parser; use irc_proto::IrcCodec; use tokio::{net::TcpListener, time::Instant}; use tokio_util::codec::FramedRead; use tracing::{error, info, info_span, Instrument}; use tracing_subscriber::EnvFilter; -use crate::{client::Client, messages::UserConnected, server::Server}; +use crate::{client::Client, config::Args, messages::UserConnected, server::Server}; pub mod channel; pub mod client; +pub mod config; pub mod connection; pub mod messages; pub mod server; @@ -23,6 +25,20 @@ pub const SERVER_NAME: &str = "my.cool.server"; #[actix_rt::main] async fn main() -> anyhow::Result<()> { + // parse CLI arguments + let opts: Args = Args::parse(); + + // overrides the RUST_LOG variable to our own value based on the + // amount of `-v`s that were passed when calling the service + std::env::set_var( + "RUST_LOG", + match opts.verbose { + 1 => "debug", + 2 => "trace", + _ => "info", + }, + ); + let subscriber = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .pretty(); -- libgit2 1.7.2