Browse Source

lib: use `clap-verbosity-flag` to simplify code on log verbosity

enables `-q` option for decreasing log level, and one or multiple `-v` option to increase them.

Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
pull/67/head
Zhouqi Jiang 5 months ago
committed by Andelf
parent
commit
79d40520b4
  1. 11
      Cargo.lock
  2. 1
      Cargo.toml
  3. 35
      src/main.rs

11
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",

1
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"

35
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<usize>,
/// Turn debugging information on
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
#[command(flatten)]
verbose: Verbosity<InfoLevel>,
/// 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;

Loading…
Cancel
Save