1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use log::Level;

/// Easily add a `--verbose` flag to CLIs using Structopt
///
/// # Examples
///
/// ```rust
/// extern crate clap_verbosity_flag;
/// #[macro_use] extern crate structopt;
///
/// use structopt::StructOpt;
/// use clap_verbosity_flag::Verbosity;
///
/// /// Le CLI
/// #[derive(Debug, StructOpt)]
/// struct Cli {
///     #[structopt(flatten)]
///     verbose: Verbosity,
/// }
/// #
/// # fn main() {}
/// ```
#[derive(StructOpt, Debug, Clone)]
pub struct Verbosity {
  /// Print more log output
  #[structopt(long = "verbosity", short = "v", parse(from_occurrences))]
  verbosity: u8,
  /// Suppress all log output
  #[structopt(long = "quiet", short = "q")]
  quiet: bool,
}

impl Verbosity {
  /// Get the log level.
  pub fn log_level(&self) -> Option<Level> {
    if self.quiet {
      match self.verbosity {
        0 => None,
        1 => Some(Level::Error),
        2 => Some(Level::Warn),
        3 => Some(Level::Info),
        4 => Some(Level::Debug),
        _ => Some(Level::Trace),
      }
    } else {
      match self.verbosity {
        0 => Some(Level::Info),
        1 => Some(Level::Debug),
        _ => Some(Level::Trace),
      }
    }
  }
}

use std::fmt;

impl fmt::Display for Verbosity {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self.verbosity)
  }
}