#printing #setting-up #error-handling #cli #cargo-run

axocli

common code for setting up a CLI App and handling errors/printing

4 releases (2 breaking)

0.3.0 Jul 20, 2025
0.2.0 Feb 16, 2024
0.1.1 Nov 24, 2023
0.1.0 Apr 4, 2023

#336 in Command-line interface

Download history 3860/week @ 2026-04-11 3631/week @ 2026-04-18 3329/week @ 2026-04-25 1814/week @ 2026-05-02 1509/week @ 2026-05-09 1860/week @ 2026-05-16 1862/week @ 2026-05-23 2702/week @ 2026-05-30 1323/week @ 2026-06-06 1817/week @ 2026-06-13 1683/week @ 2026-06-20 2244/week @ 2026-06-27 2417/week @ 2026-07-04 3700/week @ 2026-07-11 2269/week @ 2026-07-18 2116/week @ 2026-07-25

10,833 downloads per month
Used in 6 crates (2 directly)

MIT/Apache

21KB
176 lines

axocli

crates.io docs Rust CI

Common code for setting up a CLI App and handling errors/printing.

Example

See examples/axoapp.rs for a walkthrough/example.

Some various interesting example invocations to play with:

# clap help
cargo run --example axoapp -- --help

# success
cargo run --example axoapp -- 5
cargo run --example axoapp -- 5 --output-format=json

# normal error
cargo run --example axoapp -- 2
cargo run --example axoapp -- 2 --output-format=json

# panic (setting RUST_BACKTRACE=1 here is also interesting)
cargo run --example axoapp -- 0
cargo run --example axoapp -- 0 --output-format=json

# clap error
cargo run --example axoapp --

What Does It Do?

First off, it handles setting up:

  • a tracing subscriber (currently the kind that cargo-dist uses, more work needed for oranda's use)
  • a miette formatter (so we can have a shared Look And Feel)
  • a panic handler (to get more control over output if the app panics)
  • (not implemented but likely in the future) a tokio runtime

It also handles printing top-level errors, notably including a json_errors mode where the error gets formatted to json and printed to stdout, while still printing a human-friendly version to stderr. This is done for both an error returned from real_main and panics. So anything expecting machine-readable output from our apps will not freak out and get something unparseable when things error/panic. It will also set the process exit code on error (with std::process::exit, on the assumption that all cleanup was done when we returned/panicked out of real_main).

It also exposes the json diagnostic formatting machinery so you can Write them wherever or turn them into serde_json::Values. This is useful for returning a larger result with diagnostics nested inside of it (say, for reporting warnings).

License

Licensed under either of

at your option.

Dependencies

~7.5–10MB
~192K SLoC