diff options
Diffstat (limited to 'example')
| -rw-r--r-- | example/README.md.template | 77 | ||||
| -rw-r--r-- | example/comptime-clap-error.zig | 21 | ||||
| -rw-r--r-- | example/comptime-clap.zig | 7 | ||||
| -rw-r--r-- | example/help.zig | 25 |
4 files changed, 125 insertions, 5 deletions
diff --git a/example/README.md.template b/example/README.md.template new file mode 100644 index 0000000..88914fb --- /dev/null +++ b/example/README.md.template | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | # zig-clap | ||
| 2 | |||
| 3 | A simple and easy to use command line argument parser library for Zig. | ||
| 4 | |||
| 5 | ## Features | ||
| 6 | |||
| 7 | * Short arguments `-a` | ||
| 8 | * Chaining `-abc` where `a` and `b` does not take values. | ||
| 9 | * Long arguments `--long` | ||
| 10 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | ||
| 11 | * Short args also support passing values with no spacing or `=` (`-a100`) | ||
| 12 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) | ||
| 13 | |||
| 14 | ## Examples | ||
| 15 | |||
| 16 | ### `StreamingClap` | ||
| 17 | |||
| 18 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | ||
| 19 | `args.Iterator` to provide it with arguments lazily. | ||
| 20 | |||
| 21 | ```zig | ||
| 22 | {} | ||
| 23 | ``` | ||
| 24 | |||
| 25 | ### `ComptimeClap` | ||
| 26 | |||
| 27 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes | ||
| 28 | them available through three functions (`flag`, `option`, `positionals`). | ||
| 29 | |||
| 30 | ```zig | ||
| 31 | {} | ||
| 32 | ``` | ||
| 33 | |||
| 34 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) | ||
| 35 | and validates that the strings you pass to `option` and `flag` are actually parameters that the | ||
| 36 | program can take: | ||
| 37 | |||
| 38 | ```zig | ||
| 39 | {} | ||
| 40 | ``` | ||
| 41 | |||
| 42 | ``` | ||
| 43 | zig-clap/src/comptime.zig:116:17: error: --helps is not a parameter. | ||
| 44 | @compileError(name ++ " is not a parameter."); | ||
| 45 | ^ | ||
| 46 | zig-clap/src/comptime.zig:84:45: note: called from here | ||
| 47 | const param = comptime findParam(name); | ||
| 48 | ^ | ||
| 49 | zig-clap/example/comptime-clap-error.zig:22:18: note: called from here | ||
| 50 | _ = args.flag("--helps"); | ||
| 51 | ^ | ||
| 52 | ``` | ||
| 53 | |||
| 54 | Ofc, this limits you to parameters that are comptime known. | ||
| 55 | |||
| 56 | ### `help` | ||
| 57 | |||
| 58 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the | ||
| 59 | program can take. | ||
| 60 | |||
| 61 | ```zig | ||
| 62 | {} | ||
| 63 | ``` | ||
| 64 | |||
| 65 | ``` | ||
| 66 | -h, --help Display this help and exit. | ||
| 67 | -v, --version Output version information and exit. | ||
| 68 | ``` | ||
| 69 | |||
| 70 | The `help` function is the simplest to call. It only takes an `OutStream` and a slice of | ||
| 71 | `Param([]const u8)`. This function assumes that the id of each parameter is the help message. | ||
| 72 | |||
| 73 | The `helpEx` is the generic version of `help`. It can print a help message for any | ||
| 74 | `Param` give that the caller provides functions for getting the help and value strings. | ||
| 75 | |||
| 76 | The `helpFull` is even more generic, allowing the functions that get the help and value strings | ||
| 77 | to return errors and take a context as a parameter. | ||
diff --git a/example/comptime-clap-error.zig b/example/comptime-clap-error.zig new file mode 100644 index 0000000..93c1af2 --- /dev/null +++ b/example/comptime-clap-error.zig | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | const params = [_]clap.Param(void){clap.Param(void){ | ||
| 6 | .names = clap.Names{ .short = 'h', .long = "help" }, | ||
| 7 | }}; | ||
| 8 | |||
| 9 | var direct_allocator = std.heap.DirectAllocator.init(); | ||
| 10 | const allocator = &direct_allocator.allocator; | ||
| 11 | defer direct_allocator.deinit(); | ||
| 12 | |||
| 13 | var iter = clap.args.OsIterator.init(allocator); | ||
| 14 | defer iter.deinit(); | ||
| 15 | const exe = try iter.next(); | ||
| 16 | |||
| 17 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); | ||
| 18 | defer args.deinit(); | ||
| 19 | |||
| 20 | _ = args.flag("--helps"); | ||
| 21 | } | ||
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index 935381f..695fa62 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig | |||
| @@ -13,7 +13,7 @@ pub fn main() !void { | |||
| 13 | defer direct_allocator.deinit(); | 13 | defer direct_allocator.deinit(); |
| 14 | 14 | ||
| 15 | // First we specify what parameters our program can take. | 15 | // First we specify what parameters our program can take. |
| 16 | const params = comptime [_]clap.Param([]const u8){ | 16 | const params = [_]clap.Param([]const u8){ |
| 17 | clap.Param([]const u8){ | 17 | clap.Param([]const u8){ |
| 18 | .id = "Display this help and exit.", | 18 | .id = "Display this help and exit.", |
| 19 | .names = clap.Names{ .short = 'h', .long = "help" }, | 19 | .names = clap.Names{ .short = 'h', .long = "help" }, |
| @@ -41,11 +41,8 @@ pub fn main() !void { | |||
| 41 | var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter); | 41 | var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter); |
| 42 | defer args.deinit(); | 42 | defer args.deinit(); |
| 43 | 43 | ||
| 44 | // clap.help is a function that can print a simple help message, given a | ||
| 45 | // slice of Param([]const u8). There is also a helpEx, which can print a | ||
| 46 | // help message for any Param, but it is more verbose to call. | ||
| 47 | if (args.flag("--help")) | 44 | if (args.flag("--help")) |
| 48 | return try clap.help(stdout, params); | 45 | debug.warn("--help\n"); |
| 49 | if (args.option("--number")) |n| | 46 | if (args.option("--number")) |n| |
| 50 | debug.warn("--number = {}\n", n); | 47 | debug.warn("--number = {}\n", n); |
| 51 | for (args.positionals()) |pos| | 48 | for (args.positionals()) |pos| |
diff --git a/example/help.zig b/example/help.zig new file mode 100644 index 0000000..35c0258 --- /dev/null +++ b/example/help.zig | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | const stderr_file = try std.io.getStdErr(); | ||
| 6 | var stderr_out_stream = stderr_file.outStream(); | ||
| 7 | const stderr = &stderr_out_stream.stream; | ||
| 8 | |||
| 9 | // clap.help is a function that can print a simple help message, given a | ||
| 10 | // slice of Param([]const u8). There is also a helpEx, which can print a | ||
| 11 | // help message for any Param, but it is more verbose to call. | ||
| 12 | try clap.help( | ||
| 13 | stderr, | ||
| 14 | [_]clap.Param([]const u8){ | ||
| 15 | clap.Param([]const u8){ | ||
| 16 | .id = "Display this help and exit.", | ||
| 17 | .names = clap.Names{ .short = 'h', .long = "help" }, | ||
| 18 | }, | ||
| 19 | clap.Param([]const u8){ | ||
| 20 | .id = "Output version information and exit.", | ||
| 21 | .names = clap.Names{ .short = 'v', .long = "version" }, | ||
| 22 | }, | ||
| 23 | }, | ||
| 24 | ); | ||
| 25 | } | ||