#logging #cli

cli-batteries

Batteries included opinionated command line interfaces

16 unstable releases (3 breaking)

0.4.1 Nov 26, 2022
0.4.0 Oct 8, 2022
0.3.3 Aug 10, 2022
0.3.2 Jul 28, 2022
0.1.4 May 30, 2022

#107 in Command-line interface

Download history 72/week @ 2022-11-27 45/week @ 2022-12-04 119/week @ 2022-12-11 69/week @ 2022-12-18 80/week @ 2022-12-25 97/week @ 2023-01-01 83/week @ 2023-01-08 26/week @ 2023-01-15 101/week @ 2023-01-22 161/week @ 2023-01-29 132/week @ 2023-02-05 130/week @ 2023-02-12 123/week @ 2023-02-19 76/week @ 2023-02-26 264/week @ 2023-03-05 202/week @ 2023-03-12

665 downloads per month
Used in 2 crates

MIT license

75KB
1.5K SLoC

Rust 1.5K SLoC // 0.1% comments Python 177 SLoC // 0.3% comments

CLI Batteries

crates.io docs.rs MIT License dependency status codecov CI

Opinionated batteries-included command line interface runtime utilities.

To use it, add it to your Cargo.toml

[dependencies]
cli-batteries = "0.1"

[build-dependencies]
cli-batteries = "0.1"

and call the [build_rs] function in your build.rs

fn main() {
    cli_batteries::build_rs().unwrap()
}

Then in your src/main.rs you define app specific command line arguments using [clap::Parser][clap] and run the app as follows

use cli_batteries::{version, Parser};
use std::{path::PathBuf, io::Result};
use tokio::fs::File;

#[derive(Parser)]
#[group(skip)]
struct Options {
    /// File to read
    #[clap(long, env, default_value = "Readme.md")]
    file: PathBuf,
}

async fn app(options: Options) -> Result<()> {
    let mut file = File::open(options.file).await?;
    Ok(())
}

fn main() {
    cli_batteries::run(version!(), app);
}

You can see this working in the example project.

Features

  • signals: Handle Ctrl-C, SIGINT and SIGTERM with gracefull shutdown.
  • mimalloc: Use the mimalloc allocator with security hardening features enabled.
  • rand: Log and configure random seeds.
  • rayon: Log and configure number of threads.
  • prometheus: Start a Prometheus metrics server.
  • metered-allocator: Collect metric on memory allocation, enables prometheus.
  • mock-shutdown: Enable the reset_shutdown function that allows re-arming shutdown for testing.
  • tokio-console: Enable the --tokio-console option to start a Tokio console server on http://127.0.0.1:6669/ for async inspection.
  • otlp: Enable the --trace-otlp option to push traces to an OpenTelementry collector.

Building and testing

Format, lint, build and test everything (I recommend creating a shell alias for this):

cargo fmt &&\
cargo clippy --all-features --all-targets &&\
cargo test --workspace --all-features --doc -- --nocapture &&\
cargo test --workspace --all-features --all-targets -- --nocapture &&\
cargo doc --workspace --all-features --no-deps

Check documentation coverage

RUSTDOCFLAGS="-Z unstable-options --show-coverage"  cargo doc --workspace --all-features --no-deps

To do

Goals:

Maybe:


lines of code GitHub contributors GitHub issues GitHub pull requests GitHub Repo stars crates.io

Dependencies

~17–28MB
~540K SLoC