Skip to main content

Crate cli_ui

Crate cli_ui 

Source
Expand description

§cli-ui

Styled CLI framework for Rust — typed argument parsing, consistent help, progress output, and summary blocks with zero boilerplate.

§Quick start — single command

use std::path::PathBuf;
use cli_ui::{CliOptions, header, phase, step, ok, summary};
use cli_ui::styles::{paint, CYAN, YELLOW, DIM, OK};
use cli_ui::progress::format_bytes;

#[derive(CliOptions)]
#[cli(about = "compress images", theme = "green")]
struct Opt {
    #[arg(positional, validate(exists, is_file, ext("png","jpg","webp")))]
    input: PathBuf,

    #[arg(positional, validate(is_dir), action(create_dir_all))]
    output: PathBuf,

    #[arg(section = "Quality", short = 'q', long = "quality",
          default = 85, validate(range(1, 100)))]
    quality: u32,

    #[arg(section = "Quality", long = "format",
          default = "webp", validate(one_of("webp","jpeg","png")))]
    format: String,

    #[arg(section = "Performance", short = 'j', long = "jobs",
          default = env("JOBS", 4), validate(range(1, 256)))]
    jobs: usize,
}

fn main() {
    let opt = Opt::parse();
    header!("imgopt", env!("CARGO_PKG_VERSION"), "compress images", "lossless by default");
    phase!("init", "reading {}", opt.input.display());
    summary! {
        done: "Done",
        "output" => paint(CYAN, &opt.output.display().to_string()),
        section,
        "time" => paint(DIM, "120ms"),
    }
}

§Quick start — subcommands

use cli_ui::{CliCommand, CliOptions, Result};

#[derive(CliOptions)]
struct Global {
    #[arg(short = 'v', long = "verbose", negatable)]
    verbose: bool,
}

#[derive(CliCommand)]
#[cli(name = "mytool", about = "...", theme = "cyan", global = Global)]
enum Cmd {
    /// Download a file
    #[cli(alias = "dl")]
    Download(DownloadOpt),
    /// Show status
    Status,
}

fn main() -> Result<()> {
    match Cmd::parse()? {
        Cmd::Download(opt) => download(Cmd::global(), opt),
        Cmd::Status        => status(Cmd::global()),
    }
    Ok(())
}

§Interactive prompts

cli-ui also ships clack-style interactive prompts behind the interactive feature — text, select, multiselect, confirm, password, autocomplete, date, path, spinner, framed log lines, sequential tasks. Read the prompt module docs for the full mental model; here’s the whole API surface in five lines:

use cli_ui::prompt::prelude::*;

intro("Set up");
let name = text("Your name").run().or_cancel("Bye.");
let go   = confirm("Continue?").default(true).run().or_cancel("Bye.");
outro(format!("Hi {name}, {}", if go { "going" } else { "stopping" }));

Re-exports§

pub use progress::format_bytes;
pub use progress::Progress;
pub use summary::Summary;

Modules§

complete
Runtime helpers for smart shell completions.
help
Help renderer — called from generated help() method.
macros
User-facing macros for runtime output.
progress
Download progress tracking.
prompt
Interactive prompts — clack/prompts-style.
styles
ANSI style constants and theme support.
summary
Summary block renderer.
term
Terminal introspection — width detection and tty check.

Macros§

bail
Print an error message and exit with code 1.
header
Print the app header with a themed badge.
ok
Print a success line outside of a Progress block.
phase
Print a phase line: ▶ tag message
step
Print a step header: ▸ message
substep
Print a CSS/asset sub-step: └─ url → local
summary
Build and print a styled summary block at the end of a run.

Structs§

CliError
Error returned by CliCommand::parse and CliOptions::parse_args.

Traits§

CliCommand
Implemented by #[derive(CliCommand)] on enums.
CliOptions
Implemented by #[derive(CliOptions)] on structs.

Functions§

glob_match
Simple glob matching — supports *, ?, [abc].
print_error
Print a styled error line to stderr.
print_missing_args
Print “missing required arguments” error.
print_missing_subcommand
Print “missing subcommand” error.
print_sub_usage
Print “usage: app subcommand” header for subcommand help.
print_unit_help
Print help for a unit variant (no options).
print_unknown_subcommand
Print “unknown subcommand” error with did-you-mean suggestion.
print_version
Print a themed version badge to stderr.
print_warning
Print a yellow warning line to stderr (non-fatal).

Type Aliases§

Result
Result<T> alias used throughout cli-ui.

Derive Macros§

CliCommand
Derive typed subcommand dispatch for an enum.
CliOptions
Derive typed argument parsing for a struct.