diff --git a/README.md b/README.md index 91e91d1..6514414 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,9 @@ Commands: help Print this message or the help of the given subcommand(s) Options: - -h, --help Print help information + -q, --quiet Operates quietly. Errors will be reported even if this option is enabled + -v, --verbose Operates verbosely. Traces, debug logs will be reported + -h, --help Print help ``` ### Cloning a repository diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 06b1d47..11820cb 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -10,8 +10,12 @@ mod profile; mod shell; mod version; +use std::io::stderr; + use anyhow::Result; use clap::{Parser, Subcommand}; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::EnvFilter; #[derive(Debug, Subcommand)] pub enum Action { @@ -43,10 +47,34 @@ pub enum Action { pub struct Cli { #[clap(subcommand)] action: Action, + + /// Operates quietly. Errors will be reported even if this option is enabled. + #[clap(short, long, global = true)] + quiet: bool, + + /// Operates verbosely. Traces, debug logs will be reported. + #[clap(short, long, global = true)] + verbose: bool, } impl Cli { pub async fn run(self) -> Result<()> { + tracing_subscriber::fmt() + .compact() + .without_time() + .with_target(false) + .with_env_filter( + EnvFilter::builder() + .with_default_directive(match (self.quiet, self.verbose) { + (true, _) => LevelFilter::ERROR.into(), + (_, true) => LevelFilter::TRACE.into(), + _ => LevelFilter::INFO.into(), + }) + .from_env_lossy(), + ) + .with_writer(stderr) + .init(); + use Action::*; match self.action { Cd(cmd) => cmd.run(), diff --git a/src/main.rs b/src/main.rs index 96dd30a..5d7e264 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,10 @@ mod root; mod rule; mod url; -use std::io::stderr; use std::process::exit; use clap::Parser; use tracing::error; -use tracing_subscriber::filter::LevelFilter; -use tracing_subscriber::EnvFilter; use crate::cmd::Cli; @@ -31,18 +28,6 @@ const BUILD_INFO: &str = build_info::format!( #[tokio::main] async fn main() { - tracing_subscriber::fmt() - .compact() - .without_time() - .with_target(false) - .with_env_filter( - EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(), - ) - .with_writer(stderr) - .init(); - if let Err(e) = Cli::parse().run().await { error!("{}", e); exit(1);