diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 26 |
1 files changed, 17 insertions, 9 deletions
| @@ -14,6 +14,7 @@ into master on every `zig` release. | |||
| 14 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | 14 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 15 | * Short args also support passing values with no spacing or `=` (`-a100`) | 15 | * Short args also support passing values with no spacing or `=` (`-a100`) |
| 16 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) | 16 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) |
| 17 | * Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`) | ||
| 17 | * Print help message from parameter specification. | 18 | * Print help message from parameter specification. |
| 18 | * Parse help message to parameter specification. | 19 | * Parse help message to parameter specification. |
| 19 | 20 | ||
| @@ -33,10 +34,11 @@ pub fn main() !void { | |||
| 33 | // First we specify what parameters our program can take. | 34 | // First we specify what parameters our program can take. |
| 34 | // We can use `parseParam` to parse a string to a `Param(Help)` | 35 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 35 | const params = comptime [_]clap.Param(clap.Help){ | 36 | const params = comptime [_]clap.Param(clap.Help){ |
| 36 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 37 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 37 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 38 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, |
| 39 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 38 | clap.Param(clap.Help){ | 40 | clap.Param(clap.Help){ |
| 39 | .takes_value = true, | 41 | .takes_value = .One, |
| 40 | }, | 42 | }, |
| 41 | }; | 43 | }; |
| 42 | 44 | ||
| @@ -47,6 +49,8 @@ pub fn main() !void { | |||
| 47 | debug.warn("--help\n", .{}); | 49 | debug.warn("--help\n", .{}); |
| 48 | if (args.option("--number")) |n| | 50 | if (args.option("--number")) |n| |
| 49 | debug.warn("--number = {}\n", .{n}); | 51 | debug.warn("--number = {}\n", .{n}); |
| 52 | for (args.options("--string")) |s| | ||
| 53 | debug.warn("--string = {}\n", .{s}); | ||
| 50 | for (args.positionals()) |pos| | 54 | for (args.positionals()) |pos| |
| 51 | debug.warn("{}\n", .{pos}); | 55 | debug.warn("{}\n", .{pos}); |
| 52 | } | 56 | } |
| @@ -54,7 +58,8 @@ pub fn main() !void { | |||
| 54 | ``` | 58 | ``` |
| 55 | 59 | ||
| 56 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates | 60 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates |
| 57 | that the strings you pass to `option` and `flag` are actually parameters that the program can take: | 61 | that the strings you pass to `option`, `options` and `flag` are actually parameters that the |
| 62 | program can take: | ||
| 58 | 63 | ||
| 59 | ```zig | 64 | ```zig |
| 60 | const std = @import("std"); | 65 | const std = @import("std"); |
| @@ -106,10 +111,11 @@ pub fn main() !void { | |||
| 106 | // First we specify what parameters our program can take. | 111 | // First we specify what parameters our program can take. |
| 107 | // We can use `parseParam` to parse a string to a `Param(Help)` | 112 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 108 | const params = comptime [_]clap.Param(clap.Help){ | 113 | const params = comptime [_]clap.Param(clap.Help){ |
| 109 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 114 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 110 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 115 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, |
| 116 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 111 | clap.Param(clap.Help){ | 117 | clap.Param(clap.Help){ |
| 112 | .takes_value = true, | 118 | .takes_value = .One, |
| 113 | }, | 119 | }, |
| 114 | }; | 120 | }; |
| 115 | 121 | ||
| @@ -126,6 +132,8 @@ pub fn main() !void { | |||
| 126 | debug.warn("--help\n", .{}); | 132 | debug.warn("--help\n", .{}); |
| 127 | if (args.option("--number")) |n| | 133 | if (args.option("--number")) |n| |
| 128 | debug.warn("--number = {}\n", .{n}); | 134 | debug.warn("--number = {}\n", .{n}); |
| 135 | for (args.options("--string")) |s| | ||
| 136 | debug.warn("--string = {}\n", .{s}); | ||
| 129 | for (args.positionals()) |pos| | 137 | for (args.positionals()) |pos| |
| 130 | debug.warn("{}\n", .{pos}); | 138 | debug.warn("{}\n", .{pos}); |
| 131 | } | 139 | } |
| @@ -155,11 +163,11 @@ pub fn main() !void { | |||
| 155 | clap.Param(u8){ | 163 | clap.Param(u8){ |
| 156 | .id = 'n', | 164 | .id = 'n', |
| 157 | .names = clap.Names{ .short = 'n', .long = "number" }, | 165 | .names = clap.Names{ .short = 'n', .long = "number" }, |
| 158 | .takes_value = true, | 166 | .takes_value = .One, |
| 159 | }, | 167 | }, |
| 160 | clap.Param(u8){ | 168 | clap.Param(u8){ |
| 161 | .id = 'f', | 169 | .id = 'f', |
| 162 | .takes_value = true, | 170 | .takes_value = .One, |
| 163 | }, | 171 | }, |
| 164 | }; | 172 | }; |
| 165 | 173 | ||