diff --git a/Cargo.lock b/Cargo.lock index a1ef351..54541b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,16 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-verbosity-flag" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb9b20c0dd58e4c2e991c8d203bbeb76c11304d1011659686b5b644bc29aa478" +dependencies = [ + "clap", + "log", +] + [[package]] name = "clap_builder" version = "4.5.2" @@ -944,6 +954,7 @@ dependencies = [ "bitfield", "chrono", "clap", + "clap-verbosity-flag", "hex", "ihex", "indicatif", diff --git a/Cargo.toml b/Cargo.toml index edadf64..9f1b919 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,3 +35,4 @@ indicatif = "0.17.7" serialport = "4.3.0" libloading = "0.8.1" chrono = "0.4.31" +clap-verbosity-flag = "2.2.0" diff --git a/src/main.rs b/src/main.rs index fb18014..a5419c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use wlink::{ }; use clap::{Parser, Subcommand}; +use clap_verbosity_flag::{InfoLevel, Verbosity}; #[derive(clap::Parser)] #[command(author, version, about, long_about = None)] @@ -19,9 +20,8 @@ struct Cli { #[arg(long, short = 'd', value_name = "INDEX")] device: Option, - /// Turn debugging information on - #[arg(short, long, action = clap::ArgAction::Count)] - verbose: u8, + #[command(flatten)] + verbose: Verbosity, /// Detach chip after operation #[arg(long, global = true, default_value = "false")] @@ -172,28 +172,13 @@ fn main() -> Result<()> { let cli = Cli::parse(); // init simplelogger - if cli.verbose >= 2 { - let _ = simplelog::TermLogger::init( - simplelog::LevelFilter::Trace, - simplelog::Config::default(), - simplelog::TerminalMode::Mixed, - simplelog::ColorChoice::Auto, - ); - } else if cli.verbose == 1 { - let _ = simplelog::TermLogger::init( - simplelog::LevelFilter::Debug, - simplelog::Config::default(), - simplelog::TerminalMode::Mixed, - simplelog::ColorChoice::Auto, - ); - } else { - let _ = simplelog::TermLogger::init( - simplelog::LevelFilter::Info, - simplelog::Config::default(), - simplelog::TerminalMode::Mixed, - simplelog::ColorChoice::Auto, - ); - } + simplelog::TermLogger::init( + cli.verbose.log_level_filter(), + simplelog::Config::default(), + simplelog::TerminalMode::Mixed, + simplelog::ColorChoice::Auto, + ) + .expect("initialize simple logger"); let device_index = cli.device.unwrap_or(0); let mut will_detach = !cli.no_detach;