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
App | An application: a command tree and how to run it. |
Command | One command of the tree, usually built by command. |
Context | What a handler receives: I/O, allocators, output, and the parsed command line. |
Flag | One flag's descriptor, usually derived from a field of a flags struct. |
Messages | Every sentence the library shows, replaceable to translate them: App.messages. |
Theme | How help and the runner's messages look: App.theme. |
Candidate | What a completer (.complete of a flag or positional, or of .args) returns. |
command | Builds a Command from a declarative spec at compile time. |
flags | The flags a struct's fields become: what they mean from field specs, how they are spelled from per-place options. |
args | The positionals a struct's fields become, in order. |
FlagOptions | The options flags takes: per field, its spelling; flag groups. |
ArgOptions | The options args takes: per field, its name, completion, and prompt. |
Complete | A completer for a flag's or positional's .complete: .from(fn). |
flagsOf | The flags of a plain flags struct, for building a Command by hand. |
Defaults | Where flag defaults come from besides the command line and the environment, such as a configuration file: the type of App.defaults. |
defaults | Flag defaults from a configuration: App.defaults = .from(&config, .{}). |
help | Help pages: what --help writes, and searching them (help --search). |
complete | Shell completion: the scripts, the completion command, and the __complete2 protocol. |
docs | Documentation of a command tree: Markdown, man pages, HTML, JSON. |
json | A command tree as JSON, for tools that read it. |
terminal | What the terminal is: its width, its colors, whether the run is interactive. |
interrupt | Ctrl-C as a request to stop: App.cancel_on_interrupt. |
prompt | Asking for missing flags: App.prompter. |
repl | Running a command tree as an interactive shell. |
plugins | Commands from other programs on PATH, as git runs git-<name>: App.plugins. |
pager | Showing long text through $PAGER: App.pager. |
i18n | Translations of the library's sentences and the app's own: App.translations. |
flow | Commands that are graphs of questions and actions: setup wizards, installers, deploys. |
Flow | A flow over a state type and an enum of steps. |
version | --version text with the commit and date of the build. |
testing | Running an app in a test: `testing.run(app, args, .{ .env = ... |
internal | The library's own parts, public for its files and for the curious, but not stable: they change in any release (see docs/stability.md). |