termancy Guide Components Commands API

cli

cli - command-line applications from typed declarations.

const cli = @import("cli");

const ServeFlags = struct {
    port: u16 = 8080,
    verbose: u8 = 0,
};

// What the fields mean (shared with `cfg`), then how they are spelled here.
const Specs = field.Set(.{field.Spec(ServeFlags){ .fields = .{
    .port = .{ .help = "Port to listen on", .checks = &.{.min(1)} },
} }});
const serve_flags = cli.flags(ServeFlags, Specs, .{ .fields = .{
    .port = .{ .short = 'p', .env = "PORT" },
    .verbose = .{ .short = 'v', .count = true },
} });

fn serve(ctx: *cli.Context, flags: ServeFlags) !void {
    try ctx.out.print("listening on {d}\n", .{flags.port});
}

const root = cli.command(.{
    .name = "app",
    .summary = "An example",
    .subcommands = .{
        cli.command(.{ .name = "serve", .flags = serve_flags, .run = serve }),
    },
});

pub fn main(init: std.process.Init) u8 {
    const app: cli.App = .{ .root = &root, .version = "0.1.0" };
    return app.run(init);
}

The module imports std, term (for display widths, color detection, and the styles of Theme), and field (specs, shared with cfg). Flag types may follow the parse convention shared with cfg (pub fn parse(text: []const u8) !T), and Context.changedFlags returns records that cfg.Config.bindFlags accepts.

Declarations

AppAn application: a command tree and how to run it.
CommandOne command of the tree, usually built by command.
ContextWhat a handler receives: I/O, allocators, output, and the parsed command line.
FlagOne flag's descriptor, usually derived from a field of a flags struct.
MessagesEvery sentence the library shows, replaceable to translate them: App.messages.
ThemeHow help and the runner's messages look: App.theme.
CandidateWhat a completer (.complete of a flag or positional, or of .args) returns.
commandBuilds a Command from a declarative spec at compile time.
flagsThe flags a struct's fields become: what they mean from field specs, how they are spelled from per-place options.
argsThe positionals a struct's fields become, in order.
FlagOptionsThe options flags takes: per field, its spelling; flag groups.
ArgOptionsThe options args takes: per field, its name, completion, and prompt.
CompleteA completer for a flag's or positional's .complete: .from(fn).
flagsOfThe flags of a plain flags struct, for building a Command by hand.
DefaultsWhere flag defaults come from besides the command line and the environment, such as a configuration file: the type of App.defaults.
defaultsFlag defaults from a configuration: App.defaults = .from(&config, .{}).
helpHelp pages: what --help writes, and searching them (help --search).
completeShell completion: the scripts, the completion command, and the __complete2 protocol.
docsDocumentation of a command tree: Markdown, man pages, HTML, JSON.
jsonA command tree as JSON, for tools that read it.
terminalWhat the terminal is: its width, its colors, whether the run is interactive.
interruptCtrl-C as a request to stop: App.cancel_on_interrupt.
promptAsking for missing flags: App.prompter.
replRunning a command tree as an interactive shell.
pluginsCommands from other programs on PATH, as git runs git-<name>: App.plugins.
pagerShowing long text through $PAGER: App.pager.
i18nTranslations of the library's sentences and the app's own: App.translations.
flowCommands that are graphs of questions and actions: setup wizards, installers, deploys.
FlowA flow over a state type and an enum of steps.
version--version text with the commit and date of the build.
testingRunning an app in a test: `testing.run(app, args, .{ .env = ...
internalThe library's own parts, public for its files and for the curious, but not stable: they change in any release (see docs/stability.md).

The full API reference