Skip to content

Commit

Permalink
feat(level): add off, debug, and trace options for default Level
Browse files Browse the repository at this point in the history
add example for changing the default, and mention in the docs
  • Loading branch information
pitoniak32 committed Feb 19, 2024
1 parent 42895a2 commit 4a0a458
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/default_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};

/// Foo
#[derive(Debug, Parser)]
struct DefaultCli {
#[command(flatten)]
verbose: Verbosity,
}

/// Bar
#[derive(Debug, Parser)]
struct InfoCli {
#[command(flatten)]
verbose: Verbosity<InfoLevel>,
}

fn main() {
let error_cli = DefaultCli::parse();

dbg!(&error_cli.verbose);

assert_eq!(
error_cli.verbose.log_level_filter(),
log::LevelFilter::Error
);

let info_cli = InfoCli::parse();

dbg!(&info_cli.verbose);

assert_eq!(info_cli.verbose.log_level_filter(), log::LevelFilter::Info);
}
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! - `-vvv` show debug
//! - `-vvvv` show trace
//!
//! You can also customize the default logging level:
//! You can also customize the default logging level with the `Verbosity<T>` generic:
//! ```rust,no_run
//! # use clap::Parser;
//! use clap_verbosity_flag::{Verbosity, InfoLevel};
Expand All @@ -52,6 +52,7 @@
//! verbose: Verbosity<InfoLevel>,
//! }
//! ```
//! See `default_level` in examples.
//!
//! Or implement our [`LogLevel`] trait to customize the default log level and help output.
Expand Down Expand Up @@ -210,6 +211,36 @@ impl LogLevel for InfoLevel {
}
}

/// Default to [`log::Level::Debug`]
#[derive(Copy, Clone, Debug, Default)]
pub struct DebugLevel;

impl LogLevel for DebugLevel {
fn default() -> Option<log::Level> {
Some(log::Level::Debug)
}
}

/// Default to [`log::Level::Trace`]
#[derive(Copy, Clone, Debug, Default)]
pub struct TraceLevel;

impl LogLevel for TraceLevel {
fn default() -> Option<log::Level> {
Some(log::Level::Trace)
}
}

/// Default to [`None`]
#[derive(Copy, Clone, Debug, Default)]
pub struct OffLevel;

impl LogLevel for OffLevel {
fn default() -> Option<log::Level> {
None
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 4a0a458

Please sign in to comment.