Skip to content

Commit

Permalink
Merge pull request #144 from siketyan/feat/quiet-verbose
Browse files Browse the repository at this point in the history
feat: Add --quiet / --verbose options in app global
  • Loading branch information
siketyan authored Apr 25, 2023
2 parents c297acd + 043d97f commit fbcfbe7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down
15 changes: 0 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit fbcfbe7

Please sign in to comment.