Skip to content

Commit

Permalink
Add clap-verbosity-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Jun 12, 2024
1 parent 74ff2c0 commit 323aa7b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 49 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
anyhow = "1.0.86"
arboard = "3.4.0"
clap = "4.5.7"
clap-verbosity-flag = "2.2.0"
color-eyre = "0.6.3"
enumflags2 = "0.7.10"
exec = "0.3.1"
Expand All @@ -42,7 +43,7 @@ rstest = "0.21.0"
serde = { version = "1.0.203", features = ["derive"] }
shellexpand = "3.1.0"
single-instance = "0.3.3"
stderrlog = "0.6.0"
stderrlog = { version = "0.6.0", default-features = false }
sysinfo = "0.30.12"
system_shutdown = "4.0.1"
thiserror = "1.0.61"
Expand Down
1 change: 1 addition & 0 deletions gravel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gravel-provider-system = { path = "../gravel-provider-system" }
gravel-provider-websearch = { path = "../gravel-provider-websearch" }

clap = { workspace = true, features = ["derive"] }
clap-verbosity-flag.workspace = true
color-eyre.workspace = true
figment = { workspace = true, features = ["yaml"] }
hostname.workspace = true
Expand Down
45 changes: 2 additions & 43 deletions gravel/src/init/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use log::LevelFilter;
use clap_verbosity_flag::{Verbosity, WarnLevel};

pub fn cli() -> Args {
Args::parse()
Expand All @@ -9,47 +9,6 @@ pub fn cli() -> Args {
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Args {
/// enable debug loglevel
#[command(flatten)]
pub verbosity: Verbosity,
}

#[derive(clap::Args, Debug, Clone, Default)]
pub struct Verbosity {
#[arg(
long,
short = 'v',
action = clap::ArgAction::Count,
global = true,
help = "Increase logging verbosity",
)]
verbose: u8,

#[arg(
long,
short = 'q',
action = clap::ArgAction::Count,
global = true,
help = "Decrease logging verbosity",
conflicts_with = "verbose",
)]
quiet: u8,
}

impl Verbosity {
pub fn verbosity(&self) -> i8 {
// 1 == Warn
1 - (self.quiet as i8) + (self.verbose as i8)
}

pub fn log_level(&self) -> LevelFilter {
match self.verbosity() {
i8::MIN..=-1 => LevelFilter::Off,
0 => LevelFilter::Error,
1 => LevelFilter::Warn,
2 => LevelFilter::Info,
3 => LevelFilter::Debug,
4..=i8::MAX => LevelFilter::Trace,
}
}
pub verbosity: Verbosity<WarnLevel>,
}
1 change: 0 additions & 1 deletion gravel/src/init/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use log::LevelFilter;

pub fn logging(level: LevelFilter) {
stderrlog::new()
.timestamp(stderrlog::Timestamp::Off)
.verbosity(level)
.init()
.expect("this must never be called twice");
Expand Down
2 changes: 1 addition & 1 deletion gravel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn run() {
let executable = env::current_exe().unwrap();

let args = init::cli();
init::logging(args.verbosity.log_level());
init::logging(args.verbosity.log_level_filter());

let config = init::config();

Expand Down

0 comments on commit 323aa7b

Please sign in to comment.