tap-rs 1.6.3

A lightweight CLI library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# Tap - a CLI parsing library for Rust

[![Crates.io](https://img.shields.io/crates/v/tap-rs.svg)](https://crates.io/crates/tap-rs)
[![GitHub](https://img.shields.io/badge/GitHub-repo-181717?logo=github)](https://github.com/pt-main/tap/tree/rust)
[![Docs.rs](https://docs.rs/tap-rs/badge.svg)](https://docs.rs/tap-rs)

```bash
cargo add tap-rs
```

**Tap** is a small library for building CLIs in Rust. It parses arguments, commands, and flags, prints colored text, and generates help. One dependency: `string_template`; macros are optional.

## Features

- Colored output via short codes `[?GN]`, `[?RD]`, `[?YW]`.
- Automatic help generation with grouping of commands that share the same handler.
- Subcommands.
- Typed parsing of positional arguments via `ArgsExt`.
- Flags `--flag`, `--key=value`, `--key:value` in `ArgsP`.
- Macros `commands!`, `subcommands!`, `args!`, `bail!`, `ensure!`, `cprintln!`, and others.
- Renderers with templates, typing, and JSON serialization.

## Quick start

A simple CLI with a `hello` command:

```rust
use tap::{
    argsparsing::ArgsP,
    engine::structs::{CmdInfo, Engine, ErrorType},
    tooling::add_help_handler,
};

fn hello_handler(_: &mut Engine, args: &[&str]) -> ErrorType {
    let name = args.first().unwrap_or(&"World");
    println!("Hello, {}!", name);
    Ok(())
}

fn main() {
    let mut ap = ArgsP::new();
    let mut engine = Engine::new(&mut ap, "MyApp v1.0".to_string());

    engine.new_command(
        "hello",
        CmdInfo::new(hello_handler, vec![], vec!["name"], false, "Say hello"),
    );

    add_help_handler(vec!["help", "-h"], &mut engine);

    if let Err(e) = engine.main() {
        eprintln!("Error: {}", e);
    }
}
```

Run:

```bash
$ cargo run hello Rust
Hello, Rust!
$ cargo run hello
Hello, World!
$ cargo run help
MyApp v1.0

╭───────  Command [help OR -h]
⎬─ Args: (none)
⎬─ Docs:
│     Generate and print help message
╰─────── 
╭───────  Command [hello]
⎬─ Args: [name]
⎬─ Docs:
│     Say hello
╰─────── 
```

The frame characters in the `help` output are what `generate_help` actually prints. They are not README decoration.

## Quick start with macros

The same thing, but shorter:

```rust
use tap::{
    argsparsing::ArgsP, commands,
    engine::structs::{Engine, ErrorType},
    tooling::add_help_handler,
};

fn hello_handler(_: &mut Engine, args: &[&str]) -> ErrorType {
    let name = args.first().copied().unwrap_or("World");
    println!("Hello, {}!", name);
    Ok(())
}

fn main() {
    let mut ap = ArgsP::new();
    let mut engine = Engine::new(&mut ap, "MyApp v1.0".to_string());

    commands! {
        engine;
        "hello" => hello_handler { optional: ["name"], docs: "Say hello" }
    }

    add_help_handler(vec!["help", "-h"], &mut engine);

    if let Err(e) = engine.main() {
        eprintln!("Error: {}", e);
    }
}
```

The `commands!` macro creates `CmdInfo` and registers the command itself. See the Macros section below.

## Commands and arguments

```rust
fn my_handler(e: &mut Engine, args: &[&str]) -> ErrorType { ... }

engine.new_command(
    "copy",
    CmdInfo::new(
        my_handler,                    // handler
        vec!["src", "dst"],            // required
        vec!["force"],                 // optional
        false,                         // unlimited: true - accepts any number of arguments
        "Copy source to destination",  // description for help
    ),
);
```

Help will show:

```
╭───────  Command [copy]
⎬─ Args: <src>, <dst>, [force]
⎬─ Docs:
│     Copy source to destination
╰─────── 
```

### Subcommands

Subcommands are commands that receive only arguments, without `&mut Engine`. Their handler has the type `fn(&[&str]) -> ErrorType`.

```rust
fn echo_handler(raw: &[&str]) -> ErrorType {
    println!("echo: {}", raw.join(" "));
    Ok(())
}

engine.new_subcommand(
    "echo",
    CmdInfo::new(echo_handler, vec![], vec![], true, "Echo back args"),
);
```

Register via `new_subcommand` / `new_subcommands` or the `subcommands!` macro.

### Parsing arguments via `ArgsExt`

The `ArgsExt` trait provides typed access to a slice of arguments:

```rust
use tap::argsparsing::ArgsExt;

fn add_handler(_: &mut Engine, raw: &[&str]) -> ErrorType {
    let a: f64 = raw.arg(0)?;
    let b: f64 = raw.arg(1)?;
    let label = raw.arg_or(2, "sum");   // if the argument is absent, returns "sum"
    println!("{}: {} + {} = {}", label, a, b, a + b);
    Ok(())
}
```

Errors contain the argument index, not a bare parser error.

## Flags

Flags are written as `--flag`, `--key=value`, or `--key:value`. They are parsed automatically in `ArgsP`:

- `args_p.flags` - list of flags without values.
- `args_p.values` - dictionary key -> value.

Flags are removed from `args` before the handler is called. Inspect them only via `e.args_p` or the `Engine` helper methods.

```rust
fn my_handler(e: &mut Engine, _: &[&str]) -> ErrorType {
    if e.flag("verbose") {
        println!("verbose mode");
    }
    if let Some(out) = e.value("output") {
        println!("output file: {}", out);
    }
    let workers: u16 = e.value_t("workers")?.unwrap_or(4);
    println!("workers: {}", workers);
    Ok(())
}
```

- `Engine::flag(name)` - `bool`.
- `Engine::value(name)` - `Option<&str>`.
- `Engine::value_t::<T>(name)` - `Result<Option<T>, String>`; a parse error comes with the flag name.

### `--` as a separator

`--` disables flag parsing. Everything after it goes into `args` as is:

```
myapp run -- --x --y    # args = ["--x", "--y"], flags = []
```

## Macros

All macros are exported from the crate root, i.e. they are called as `tap::commands!`, `tap::args!`, etc.

### `commands!` and `subcommands!`

```rust
tap::commands! {
    engine;
    "greet" => greet_handler {
        optional: ["name"],
        docs: "Greet someone",
    }
    "add" | "sum" => add_handler {
        required: ["a", "b"],
        docs: "Sum two numbers",
    }
    "echo" => echo_handler {
        unlimited: true,
        docs: "Echo args",
    }
}
```

The `required`, `optional`, and `unlimited` fields are optional; `docs` is mandatory. `"name" | "alias"` registers multiple names with one handler.

`subcommands!` works the same way, but the handler must be `fn(&[&str]) -> ErrorType`.

### `args!`

Binds positional arguments to typed variables:

```rust
fn add_handler(_: &mut Engine, raw: &[&str]) -> ErrorType {
    tap::args! { raw;
        a: f64,
        b: f64,
        label: String = "sum".to_string(),
    }
    println!("{}: {} + {} = {}", label, a, b, a + b);
    Ok(())
}
```

The form `name: Type` is a required argument; `name: Type = default` is optional. Each argument consumes one position; order matters. An error comes with the argument name and index.

### `bail!` and `ensure!`

```rust
fn h(_: &mut Engine, raw: &[&str]) -> ErrorType {
    tap::ensure!(!raw.is_empty(), "at least one argument required");
    let n: u32 = raw[0].parse().map_err(|_| format!("bad number: {}", raw[0]))?;
    tap::ensure!(n > 0, "must be positive, got {}", n);
    Ok(())
}
```

`bail!` returns `Err` early from the handler. `ensure!` does the same, but conditionally.

### `cprint!`, `cprintln!`, `ceprintln!`

Analogues of `print!` / `println!` / `eprintln!`, but with automatic coloring of `[?COLOR]` codes:

```rust
tap::cprintln!("[?GN]Success![?RT] File saved as [?YW]config.toml[?RT]");
```

### `scope!`, `template!`, `template_colored!`

Shorthands for working with `Renderable`:

```rust
use tap::tooling::renderable::Renderable;

let mut r = Renderable::new();
tap::scope!(r;
    "name"        => "Rust",
    "version:int" => "1",
    "stable:bool" => "true",
);
tap::template!(r, "plain", "Hello, {{name}} (v{{version}})");
tap::template_colored!(r, "colored", "[?GN]Hello, {{name}}[?RT]");
```

## Colors

Colored output via `[?COLOR]` codes:

- `[?GN]`, `[?GREEN]` - green
- `[?BGN]`, `[?BGREEN]` - bright green
- `[?BACKGREEN]`, `[?BKGN]` - green background
- `[?BACKBGREEN]`, `[?BKBGN]` - bright green background
- `[?BOLD]`, `[?BD]` - bold
- `[?DIM]`, `[?DM]` - dim
- `[?ITALIC]`, `[?IT]` - italic
- `[?UNDERLINE]`, `[?UE]` - underlined
- `[?BLINK]`, `[?BL]` - blinking
- `[?REVERSE]`, `[?RV]` - reversed
- `[?STRIKETHROUGH]`, `[?ST]` - strikethrough
- `[?RT]` - reset

Format: `[?<BACKGROUND><BRIGHT><COLOR>]`.

- `<BACKGROUND>` - prefix `BK` or `BACK` for background, optional.
- `<BRIGHT>` - prefix `B` for the bright variant, optional.
- `<COLOR>` - name or two letters: the first and last letters of the name, written uppercase. `GN` = green.

### Palette

**Standard 16**: black, red, green, yellow, blue, magenta, cyan, white and their bright variants. All are available as backgrounds.

**Extended xterm-256** (also available as backgrounds):

| Name     | Abbrev. | Name     | Abbrev. |
|----------|---------|----------|---------|
| orange   | `OE`    | lime     | `LE`    |
| pink     | `PK`    | teal     | `TL`    |
| purple   | `PE`    | navy     | `NY`    |
| violet   | `VT`    | gold     | `GD`    |
| brown    | `BN`    | silver   | `SR`    |
| gray     | `GY`    | maroon   | `MN`    |

Examples: `[?OE]` - orange, `[?BACKGOLD]` / `[?BKGD]` - gold background, `[?BKOE]` - orange background.

### Usage

```rust
use tap::tooling::colors::print::println;
use tap::tooling::colors::set::colorize;

let raw = "[?GN]Success![?RT] File saved as [?YW]config.toml[?RT]";

println(raw);                        // prints already colored
let colored: String = colorize(raw); // or get a string
```

`println` and `print` in `colors::print` already call `colorize` inside - no need to color twice.

Globally disable colors:

```rust
use tap::tooling::colors::set::COLOR_ENABLED;
use std::sync::atomic::Ordering;

COLOR_ENABLED.store(false, Ordering::Relaxed);
```

Unknown codes `[?FOO]` are not consumed - they remain in the string as is.

## Grouping commands in help

Commands with identical `CmdInfo` are automatically grouped into one line as aliases:

```rust
engine.new_commands(
    vec!["help", "-h"],
    CmdInfo::new(help_handler, vec![], vec![], false, "Show help"),
);
```

Help will show `[help / -h]`.

`required`, `optional`, `unlimited`, and `docs` are compared. If any of them differs, there will be no grouping.

## Renderers

The `tooling::renderable` module templates output by scope:

```rust
use tap::tooling::renderable::{
    Renderable, add_template_render, add_template_render_colored,
};

let mut r = Renderable::new();
r.scope.insert("name", "Rust");
r.scope.insert("version:int", "1");
r.scope.insert("stable:bool", "true");

add_template_render("Hello, {{name}} (v{{version}})", "plain", &mut r);
add_template_render_colored("[?GN]Hello, {{name}}[?RT]", "colored", &mut r);

println!("{}", r.render_with("plain").unwrap());
println!("{}", r.render_with("colored").unwrap());
println!("{}", r.render_json().unwrap());   // {"name":"Rust","stable":true,"version":1}
```

The key suffix sets the type during JSON serialization:

- `:int` - integer; the string must match `-?(0|[1-9][0-9]*)`.
- `:float` - floating-point number with optional exponent.
- `:bool` - only `"true"` or `"false"`.
- `:raw` - the value is inserted into JSON as is, without quotes or validation.
- Without a suffix - string.

`render_json` returns `Result`. On an invalid value it returns `RenderError::InvalidValue`, rather than silently inserting garbage into JSON.

For an array of strings there is `render_json_array(&[Renderable])`:

```rust
use tap::tooling::renderable::render_json_array;

let json = render_json_array(&rows)?;   // [{"...":...},{"...":...}]
```

## Example

The repository has `examples/demo.rs`. It demonstrates all the main features: macros, flags, typed arguments, subcommands, colors, renderers, and JSON.

```bash
cargo run --example demo -- help
cargo run --example demo -- greet Rust
cargo run --example demo -- greet --loud Rust
cargo run --example demo -- add 2 3.5
cargo run --example demo -- serve 127.0.0.1 8080 --verbose --tls=on --workers=8
cargo run --example demo -- report
cargo run --example demo -- echo hello world
```

---

> MIT - see the LICENSE file for details.
> 2026, By Pt.