1- # ZIG-CLI
2- [ ![ Zig Docs] ( https://img.shields.io/badge/docs-zig-%23f7a41d )] ( https://sam701.github.io/zig-cli )
1+ # zig-cli
32
3+ [ ![ Zig Docs] ( https://img.shields.io/badge/docs-zig-%23f7a41d )] ( https://sam701.github.io/zig-cli )
44
55A simple package for building command line apps in Zig.
66
77Inspired by [ urfave/cli] ( https://github.com/urfave/cli ) Go package.
88
99## Features
10- * command line arguments are parsed into zig values
11- * long and short options: ` --option1 ` , ` -o `
12- * optional ` = ` sign: ` --address=127.0.0.1 ` equals ` --address 127.0.0.1 `
13- * concatenated short options: ` -a -b -c ` equals ` -abc `
14- * subcommands: ` command1 -option1 subcommand2 -option2 `
15- * multiple option values: ` --opt val1 --opt val2 --opt val3 `
16- * enums as option values: ` --opt EnumValue1 `
17- * options value can be read from environment variables with a configured prefix
18- * positional arguments can be mixed with options: ` --opt1 val1 arg1 -v `
19- * stops option parsing after ` -- ` : ` command -- --abc ` will consider ` --abc ` as a positional argument to ` command ` .
20- * errors on missing required options: ` ERROR: option 'ip' is required `
21- * prints help with ` --help `
22- * colored help messages when TTY is attached
10+
11+ - Arguments parsed directly into Zig values
12+ - Long and short options: ` --option ` , ` -o `
13+ - Optional ` = ` sign: ` --address=127.0.0.1 ` equals ` --address 127.0.0.1 `
14+ - Concatenated short options: ` -a -b -c ` equals ` -abc `
15+ - Subcommands: ` cmd -opt subcmd -opt2 `
16+ - Multiple option values: ` --opt val1 --opt val2 --opt val3 `
17+ - Enums as option values: ` --opt EnumValue1 `
18+ - Positional arguments (required and optional), mixed with options: ` --opt val arg1 -v `
19+ - Option values read from environment variables with a configured prefix
20+ - Stops option parsing after ` -- ` : ` cmd -- --abc ` treats ` --abc ` as a positional argument
21+ - Errors on missing required options: ` ERROR: missing required option '--port' `
22+ - App version and author metadata
23+ - Built-in ` --help ` flag with colored output when a TTY is attached
2324
2425## Usage
26+
2527[ API Documentation] ( https://sam701.github.io/zig-cli )
28+
2629``` zig
2730const std = @import("std");
2831const cli = @import("cli");
2932
30- // Define a configuration structure with default values.
3133var config = struct {
3234 host: []const u8 = "localhost",
3335 port: u16 = undefined,
3436}{};
3537
3638pub fn main(init: std.process.Init) !void {
3739 var r = cli.AppRunner.init(&init);
40+ defer r.deinit();
3841
39- // Create an App with a command named "short" that takes host and port options.
4042 const app = cli.App{
4143 .command = cli.Command{
42- .name = "short ",
44+ .name = "server ",
4345 .options = try r.allocOptions(&.{
44- // Define an Option for the "host" command-line argument.
4546 .{
4647 .long_name = "host",
4748 .help = "host to listen on",
4849 .value_ref = r.mkRef(&config.host),
4950 },
50-
51- // Define an Option for the "port" command-line argument.
5251 .{
5352 .long_name = "port",
5453 .help = "port to bind to",
@@ -57,32 +56,40 @@ pub fn main(init: std.process.Init) !void {
5756 },
5857 }),
5958 .target = cli.CommandTarget{
60- .action = cli.CommandAction{ .exec = run_server },
59+ .action = cli.CommandAction{ .exec = run },
6160 },
6261 },
6362 };
6463 return r.run(&app);
6564}
6665
67- // Action function to execute when the "short" command is invoked.
68- fn run_server() !void {
69- // Log a debug message indicating the server is listening on the specified host and port.
70- std.log.debug("server is listening on {s}:{d}", .{ config.host, config.port });
66+ fn run() !void {
67+ std.log.info("listening on {s}:{d}", .{ config.host, config.port });
7168}
7269```
7370
74- ### Using with the Zig package manager
75- Add ` cli ` to your ` build.zig.zon `
76- ```
77- # For zig master
71+ ### Installing with the Zig package manager
72+
73+ ** 1. Fetch the package:**
74+
75+ ``` sh
76+ # For zig master and zig 0.16
7877zig fetch --save git+https://github.com/sam701/zig-cli
7978
8079# For zig 0.15
8180zig fetch --save git+https://github.com/sam701/zig-cli#zig-0.15
8281```
8382
84- ## Printing help
85- See [ ` simple.zig ` ] ( ./examples/simple.zig )
83+ ** 2. Wire it up in ` build.zig ` :**
84+
85+ ``` zig
86+ const cli_dep = b.dependency("cli", .{});
87+ exe.root_module.addImport("cli", cli_dep.module("cli"));
88+ ```
89+
90+ ## Help output
91+
92+ See [ ` simple.zig ` ] ( ./examples/simple.zig ) for a full example with subcommands.
8693
8794```
8895$ ./zig-out/bin/simple sub1 --help
@@ -107,4 +114,5 @@ OPTIONS:
107114```
108115
109116## License
117+
110118MIT
0 commit comments