Seamless Command Line Integration with ZIG.
Define and handle commands with ease.
Support for short and long options, with or without values.
Enforce the presence of required options for commands.
Provide flexibility to commands by allowing additional configuration without being mandatory.
Supports Windows, Linux and macOS.
πΉ π Quick Start β A quick guide to get you started with the library.
πΉ π API Reference β Detailed documentation of available functions.
If you have not already added the library to your project, please review the installation guide for more information.
const cli = @import("io").terminal.cli;
Now letβs create our command line application, but before that we need to define the
commands
andoptions
used to handle the user input arguments in the terminal, for example:
const commands = [_]types.command {
types.command {
.name = "test", // Name of the command
.func = &methods.commands.testFN, // Function associated with the command
.req = &.{ "option1", "option2" }, // Required options for 'test' command
.opt = &.{ "option3" }, // Optional options for 'test' command
},
types.command {
.name = "help", // Name of the command
.func = &methods.commands.helpFN, // Function associated with the command
}
};
now we have two commands (
test
andhelp
), thehelp
command can be used without any options but thetest
command required (option1
andoption2
) and optionallyoption3
.
Now lets define the command options.
const options = [_]types.option {
types.option {
.name = "option1",
.short = '1',
.long = "option1",
},
types.option {
.name = "option2",
.short = '2',
.long = "option2",
},
types.option {
.name = "option3",
.short = '3',
.long = "option3",
.func = &methods.options.option3FN
},
};
After defining the commands and its options, we need to define the handlers.
const methods = struct {
pub const commands = struct {
pub fn testFN(_options: []const types.option) bool {
return true;
}
pub fn helpFN(_: []const types.option) bool {
return true;
}
};
pub const options = struct {
pub fn option3FN(_val : []const u8) bool {
return true;
}
};
};
Pass your commands, options, and enable/disable debugging.
pub fn main() !void {
try cli.start(&commands, &options, true);
}
Compile and execute your application.
./your_app test -1 valOne -2 valTwo -3 valThree
Kickstart your project with our lite-cli template for a swift setup.
Index |
---|
Methods |
Types |
Errors |
Function | Description |
---|---|
start |
Starts the CLI application. |
slice
=[]const u8
Field | Type | Description |
---|---|---|
name |
slice |
Name of the command. |
func |
*const fn ([]const option) bool |
Function to execute the command. |
req |
[]const slice |
Required options. |
opt |
[]const slice |
Optional options. |
Field | Type | Description |
---|---|---|
name |
slice |
Name of the option. |
func |
*const fn (slice) bool |
Function to execute the option. |
short |
u8 |
Short form, e.g., -n/-N |
long |
slice |
Long form, e.g., βname. |
value |
slice |
Value of the option. |
Error |
---|
NoArgsProvided |
UnknownCommand |
UnknownOption |
MissingRequiredOption |
UnexpectedArgument |
CommandExecutionFailed |
Detailed terminal information ensuring cross-platform compatibility.
Comprehensive terminal settings for cross-platform compatibility.
Utility functions for ANSI escape code manipulation and terminal styling.
Robust event handling for terminal key presses and mouse events.
Interactive prompts for user input.