Tap - a CLI parsing library for Rust
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:valueinArgsP. - 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:
use ;
Run:
)
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:
use ;
The commands! macro creates CmdInfo and registers the command itself. See the Macros section below.
Commands and arguments
engine.new_command;
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.
engine.new_subcommand;
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:
use ArgsExt;
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.
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!
commands!
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:
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!
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:
cprintln!;
scope!, template!, template_colored!
Shorthands for working with Renderable:
use Renderable;
let mut r = new;
scope!;
template!;
template_colored!;
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>- prefixBKorBACKfor background, optional.<BRIGHT>- prefixBfor 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
use println;
use colorize;
let raw = "[?GN]Success![?RT] File saved as [?YW]config.toml[?RT]";
println; // prints already colored
let colored: String = colorize; // or get a string
println and print in colors::print already call colorize inside - no need to color twice.
Globally disable colors:
use COLOR_ENABLED;
use Ordering;
COLOR_ENABLED.store;
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:
engine.new_commands;
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:
use ;
let mut r = new;
r.scope.insert;
r.scope.insert;
r.scope.insert;
add_template_render;
add_template_render_colored;
println!;
println!;
println!; // {"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]):
use render_json_array;
let json = render_json_array?; // [{"...":...},{"...":...}]
Example
The repository has examples/demo.rs. It demonstrates all the main features: macros, flags, typed arguments, subcommands, colors, renderers, and JSON.
MIT - see the LICENSE file for details. 2026, By Pt.