|
| 1 | +# clapp-zig |
| 2 | + |
| 3 | +A Zig implementation of the clapp CLI framework - an elegant, feature-rich library for building powerful command-line applications with interactive prompts, beautiful styling, and comprehensive tooling. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +### Core CLI Framework |
| 8 | + |
| 9 | +- **Powerful CLI Framework** - Build robust command-line applications with an elegant API |
| 10 | +- **Type-Safe** - Leverages Zig's compile-time safety for reliable CLI applications |
| 11 | +- **Command & Option Support** - Full support for commands, options, aliases, and arguments |
| 12 | +- **Help & Version Generation** - Automatic help text and version information |
| 13 | +- **Flexible Parsing** - Parse command-line arguments with ease |
| 14 | +- **Memory Safe** - Explicit memory management with allocators |
| 15 | + |
| 16 | +### Styling & Formatting |
| 17 | + |
| 18 | +- **ANSI Colors** - Full color support (red, green, blue, yellow, cyan, magenta, etc.) |
| 19 | +- **Text Decorations** - Bold, italic, underline, dim, strikethrough, and more |
| 20 | +- **Themeable** - Customizable theme colors (primary, success, warning, error, info) |
| 21 | +- **Box & Panel Drawing** - Beautiful bordered boxes and panels |
| 22 | +- **Table Formatting** - Professional table rendering with borders and headers |
| 23 | + |
| 24 | +### Interactive Prompts |
| 25 | + |
| 26 | +- **Text Input** - Prompts with validation, placeholders, and defaults |
| 27 | +- **Confirm Prompts** - Yes/no confirmations with default values |
| 28 | +- **Select Prompts** - Single-choice selection from a list |
| 29 | +- **Multi-Select** - Multiple-choice selections |
| 30 | +- **Password Input** - Masked password entry |
| 31 | +- **Intro/Outro** - Session start and end messages |
| 32 | + |
| 33 | +### Progress & Feedback |
| 34 | + |
| 35 | +- **Spinners** - Animated loading indicators with multiple styles |
| 36 | +- **Progress Bars** - Visual progress tracking with percentages |
| 37 | +- **Task Lists** - Track multiple task statuses (pending, running, success, error) |
| 38 | +- **Log Messages** - Styled log output |
| 39 | + |
| 40 | +### Advanced Features |
| 41 | + |
| 42 | +- **Config File Support** - Load settings from JSON files |
| 43 | +- **Environment Variables** - Parse env vars with prefix filtering |
| 44 | +- **Shell Completion** - Generate completion scripts for bash, zsh, fish, PowerShell |
| 45 | +- **Typo Suggestions** - "Did you mean?" suggestions using Levenshtein distance |
| 46 | +- **Fuzzy Matching** - Smart command and option matching |
| 47 | +- **Enhanced Errors** - Beautiful error messages with suggestions and hints |
| 48 | + |
| 49 | +### Testing Utilities |
| 50 | + |
| 51 | +- **Mock Streams** - Mock stdin/stdout/stderr for testing |
| 52 | +- **Test Context** - Isolated test environment for CLI apps |
| 53 | +- **Assertion Helpers** - Convenient test assertions |
| 54 | +- **Output Capture** - Capture and verify CLI output |
| 55 | + |
| 56 | +## Installation |
| 57 | + |
| 58 | +### Using Zig Package Manager |
| 59 | + |
| 60 | +Add clapp to your `build.zig.zon`: |
| 61 | + |
| 62 | +```zig |
| 63 | +.dependencies = .{ |
| 64 | + .clapp = .{ |
| 65 | + .path = "path/to/clapp/packages/zig", |
| 66 | + }, |
| 67 | +}, |
| 68 | +``` |
| 69 | + |
| 70 | +Then in your `build.zig`: |
| 71 | + |
| 72 | +```zig |
| 73 | +const clapp = b.dependency("clapp", .{ |
| 74 | + .target = target, |
| 75 | + .optimize = optimize, |
| 76 | +}); |
| 77 | +
|
| 78 | +exe.root_module.addImport("clapp", clapp.module("clapp")); |
| 79 | +``` |
| 80 | + |
| 81 | +## Quick Start |
| 82 | + |
| 83 | +### Basic CLI Example |
| 84 | + |
| 85 | +```zig |
| 86 | +const std = @import("std"); |
| 87 | +const clapp = @import("clapp"); |
| 88 | +
|
| 89 | +pub fn main() !void { |
| 90 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 91 | + defer _ = gpa.deinit(); |
| 92 | + const allocator = gpa.allocator(); |
| 93 | +
|
| 94 | + // Create a CLI instance |
| 95 | + var cli_instance = try clapp.CLI.init(allocator, "todo-app"); |
| 96 | + defer cli_instance.deinit(); |
| 97 | +
|
| 98 | + // Set version and enable help |
| 99 | + try cli_instance.version("1.0.0", "-v, --version"); |
| 100 | + try cli_instance.help(); |
| 101 | +
|
| 102 | + // Add "add" command |
| 103 | + var add_cmd = try cli_instance.command("add <task>", "Add a new task", .{}); |
| 104 | + try add_cmd.option("-p, --priority <level>", "Priority level", .{ .default = "medium" }); |
| 105 | +
|
| 106 | + add_cmd.action(struct { |
| 107 | + fn callback(args: []const []const u8, options: std.StringHashMap([]const u8)) !void { |
| 108 | + const stdout = std.io.getStdOut().writer(); |
| 109 | + const task = args[0]; |
| 110 | + const priority = options.get("priority") orelse "medium"; |
| 111 | + try stdout.print("Adding task: {s} with priority: {s}\n", .{ task, priority }); |
| 112 | + } |
| 113 | + }.callback); |
| 114 | +
|
| 115 | + // Parse command line arguments |
| 116 | + const args = try std.process.argsAlloc(allocator); |
| 117 | + defer std.process.argsFree(allocator, args); |
| 118 | +
|
| 119 | + _ = try cli_instance.parse(args, .{ .run = true }); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Commands |
| 124 | + |
| 125 | +Commands define actions your CLI can perform: |
| 126 | + |
| 127 | +```zig |
| 128 | +// Command with required argument |
| 129 | +var cmd = try cli_instance.command("install <package>", "Install a package", .{}); |
| 130 | +
|
| 131 | +// Command with optional argument |
| 132 | +var cmd = try cli_instance.command("list [filter]", "List items", .{}); |
| 133 | +
|
| 134 | +// Command with variadic arguments |
| 135 | +var cmd = try cli_instance.command("remove <...files>", "Remove files", .{}); |
| 136 | +``` |
| 137 | + |
| 138 | +### Options |
| 139 | + |
| 140 | +Add options to commands or globally: |
| 141 | + |
| 142 | +```zig |
| 143 | +// Global option (available to all commands) |
| 144 | +try cli_instance.option("--config <path>", "Config file path", .{}); |
| 145 | +
|
| 146 | +// Command-specific option |
| 147 | +var cmd = try cli_instance.command("build", "Build the project", .{}); |
| 148 | +try cmd.option("-o, --output <dir>", "Output directory", .{ .default = "dist" }); |
| 149 | +
|
| 150 | +// Boolean flag |
| 151 | +try cmd.option("-w, --watch", "Watch for changes", .{}); |
| 152 | +
|
| 153 | +// Negated option |
| 154 | +try cmd.option("--no-color", "Disable colors", .{}); |
| 155 | +``` |
| 156 | + |
| 157 | +### Aliases |
| 158 | + |
| 159 | +Commands can have aliases: |
| 160 | + |
| 161 | +```zig |
| 162 | +var cmd = try cli_instance.command("install <package>", "Install a package", .{}); |
| 163 | +try cmd.alias("i"); |
| 164 | +try cmd.alias("add"); |
| 165 | +``` |
| 166 | + |
| 167 | +### Examples |
| 168 | + |
| 169 | +Add usage examples to your CLI: |
| 170 | + |
| 171 | +```zig |
| 172 | +try cli_instance.example(.{ .string = " $ myapp install lodash" }); |
| 173 | +try cli_instance.example(.{ .string = " $ myapp install lodash --save-dev" }); |
| 174 | +``` |
| 175 | + |
| 176 | +## API Reference |
| 177 | + |
| 178 | +### CLI |
| 179 | + |
| 180 | +- `init(allocator, name)` - Create a new CLI instance |
| 181 | +- `deinit()` - Clean up resources |
| 182 | +- `command(raw_name, description, config)` - Add a command |
| 183 | +- `option(raw_name, description, config)` - Add a global option |
| 184 | +- `version(version, flags)` - Set version number |
| 185 | +- `help()` - Enable help flag |
| 186 | +- `parse(argv, options)` - Parse command line arguments |
| 187 | + |
| 188 | +### Command |
| 189 | + |
| 190 | +- `option(raw_name, description, config)` - Add an option to the command |
| 191 | +- `alias(name)` - Add an alias for the command |
| 192 | +- `action(callback)` - Set the action callback |
| 193 | +- `usage(text)` - Set custom usage text |
| 194 | +- `example(example)` - Add an example |
| 195 | + |
| 196 | +### Configuration |
| 197 | + |
| 198 | +#### CommandConfig |
| 199 | + |
| 200 | +```zig |
| 201 | +pub const CommandConfig = struct { |
| 202 | + allow_unknown_options: bool = false, |
| 203 | + ignore_option_default_value: bool = false, |
| 204 | +}; |
| 205 | +``` |
| 206 | + |
| 207 | +#### OptionConfig |
| 208 | + |
| 209 | +```zig |
| 210 | +pub const OptionConfig = struct { |
| 211 | + default: ?[]const u8 = null, |
| 212 | +}; |
| 213 | +``` |
| 214 | + |
| 215 | +## Building |
| 216 | + |
| 217 | +Build the library: |
| 218 | + |
| 219 | +```bash |
| 220 | +zig build |
| 221 | +``` |
| 222 | + |
| 223 | +Run tests: |
| 224 | + |
| 225 | +```bash |
| 226 | +zig build test |
| 227 | +``` |
| 228 | + |
| 229 | +Build and run the example: |
| 230 | + |
| 231 | +```bash |
| 232 | +zig build example |
| 233 | +zig build run-example |
| 234 | +``` |
| 235 | + |
| 236 | +## Comparison with TypeScript Version |
| 237 | + |
| 238 | +This Zig implementation closely mirrors the TypeScript version's API while leveraging Zig's features: |
| 239 | + |
| 240 | +| Feature | TypeScript | Zig | |
| 241 | +|---------|-----------|-----| |
| 242 | +| Memory Management | Automatic GC | Explicit allocators | |
| 243 | +| Error Handling | Exceptions | Error unions | |
| 244 | +| Type System | Structural | Nominal | |
| 245 | +| Callbacks | Functions | Function pointers | |
| 246 | +| Async | Promises | Async/await (future) | |
| 247 | + |
| 248 | +## License |
| 249 | + |
| 250 | +MIT |
| 251 | + |
| 252 | +## Credits |
| 253 | + |
| 254 | +This is a Zig port of [clapp](https://github.com/stacksjs/clapp), which was inspired by: |
| 255 | + |
| 256 | +- [CAC](https://github.com/cacjs/cac) - The original CLI framework |
| 257 | +- [clack](https://github.com/bombshell-dev/clack) - Interactive prompts |
| 258 | + |
| 259 | +## Contributing |
| 260 | + |
| 261 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments