Show the crate…

1 unstable release

0.0.1 Dec 16, 2023

#19 in #verbose

38 downloads per month
Used in 4 crates

GPL-3.0-only

4KB

ccli

Common command line interface.

//! Common command line interface.

use anyhow::Error;
pub use clap::{self, Parser};
pub use color_eyre::{eyre::eyre, Result};
use tracing_subscriber::filter::EnvFilter;

/// Shared application interface.
pub trait App: Parser {
    /// Verbose logging level.
    fn verbose(&self) -> u8;

    /// Run application.
    fn run(&self) -> anyhow::Result<()>;

    /// Start application.
    fn start() -> Result<()> {
        color_eyre::install()?;

        let app = Self::parse();
        let name = Self::command().get_name().to_string();
        let env =
            EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new(match app.verbose() {
                0 => format!("{name}=info"),
                1 => format!("{name}=debug"),
                2 => "debug".into(),
                _ => "trace".into(),
            }));

        tracing_subscriber::fmt().with_env_filter(env).init();
        app.run().map_err(|e| eyre!("Failed to run app, {e}"))?;
        Ok(())
    }
}

LICENSE

GPL-3.0

Dependencies

~8–16MB
~195K SLoC