Crate cli_batteries
source · [−]Expand description
CLI Batteries
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()
}Then in your src/main.rs you define app specific command line arguments using StructOpt and run the app as follows
ⓘ
use cli_batteries::{version, StructOpt};
use std::{path::PathBuf, io::Result};
use tokio::fs::File;
#[derive(StructOpt)]
struct Options {
/// File to read
#[structopt(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
rand: Log and configure random seeds.rayon: Log and configure number of threads.prometheus: Start a Prometheus metrics server.tokio_console: Start a Tokio console server.
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-depsCheck documentation coverage
RUSTDOCFLAGS="-Z unstable-options --show-coverage" cargo doc --workspace --all-features --no-depsTo do
Goals:
Maybe:
Re-exports
pub use structopt;Macros
Structs
Traits
A struct that is converted from command line arguments.
Functions
Wait for the program to shutdown.
Set some compile-time environment variables.
Are we currently shutting down?
Run the program.
Send the signal to shutdown the program.
Derive Macros
Generates the StructOpt impl.