diff options
Diffstat (limited to '')
| -rw-r--r-- | README.md | 38 | ||||
| -rw-r--r-- | example/README.md.template | 4 | ||||
| -rw-r--r-- | example/comptime-clap.zig | 9 | ||||
| -rw-r--r-- | example/simple.zig | 9 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 4 |
5 files changed, 40 insertions, 24 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 | ||
| @@ -46,15 +48,18 @@ pub fn main() !void { | |||
| 46 | if (args.flag("--help")) | 48 | if (args.flag("--help")) |
| 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 | } |
| 53 | 57 | ||
| 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 | ||
| @@ -125,9 +131,11 @@ pub fn main() !void { | |||
| 125 | if (args.flag("--help")) | 131 | if (args.flag("--help")) |
| 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 | } |
| 132 | 140 | ||
| 133 | ``` | 141 | ``` |
| @@ -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 | ||
| @@ -179,12 +187,12 @@ pub fn main() !void { | |||
| 179 | // arg.param will point to the parameter which matched the argument. | 187 | // arg.param will point to the parameter which matched the argument. |
| 180 | switch (arg.param.id) { | 188 | switch (arg.param.id) { |
| 181 | 'h' => debug.warn("Help!\n", .{}), | 189 | 'h' => debug.warn("Help!\n", .{}), |
| 182 | 'n' => debug.warn("--number = {}\n", .{ arg.value.? }), | 190 | 'n' => debug.warn("--number = {}\n", .{arg.value.?}), |
| 183 | 191 | ||
| 184 | // arg.value == null, if arg.param.takes_value == false. | 192 | // arg.value == null, if arg.param.takes_value == false. |
| 185 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" | 193 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" |
| 186 | // or "-a 10". | 194 | // or "-a 10". |
| 187 | 'f' => debug.warn("{}\n", .{ arg.value.? }), | 195 | 'f' => debug.warn("{}\n", .{arg.value.?}), |
| 188 | else => unreachable, | 196 | else => unreachable, |
| 189 | } | 197 | } |
| 190 | } | 198 | } |
diff --git a/example/README.md.template b/example/README.md.template index 2afbe86..65b507d 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -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 | ||
| @@ -28,7 +29,8 @@ The simplest way to use this library is to just call the `clap.parse` function. | |||
| 28 | ``` | 29 | ``` |
| 29 | 30 | ||
| 30 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates | 31 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates |
| 31 | that the strings you pass to `option` and `flag` are actually parameters that the program can take: | 32 | that the strings you pass to `option`, `options` and `flag` are actually parameters that the |
| 33 | program can take: | ||
| 32 | 34 | ||
| 33 | ```zig | 35 | ```zig |
| 34 | {} | 36 | {} |
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index d5c84fe..d709e48 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig | |||
| @@ -9,10 +9,11 @@ pub fn main() !void { | |||
| 9 | // First we specify what parameters our program can take. | 9 | // First we specify what parameters our program can take. |
| 10 | // We can use `parseParam` to parse a string to a `Param(Help)` | 10 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 11 | const params = comptime [_]clap.Param(clap.Help){ | 11 | const params = comptime [_]clap.Param(clap.Help){ |
| 12 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 12 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 13 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 13 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, |
| 14 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 14 | clap.Param(clap.Help){ | 15 | clap.Param(clap.Help){ |
| 15 | .takes_value = true, | 16 | .takes_value = .One, |
| 16 | }, | 17 | }, |
| 17 | }; | 18 | }; |
| 18 | 19 | ||
| @@ -29,6 +30,8 @@ pub fn main() !void { | |||
| 29 | debug.warn("--help\n", .{}); | 30 | debug.warn("--help\n", .{}); |
| 30 | if (args.option("--number")) |n| | 31 | if (args.option("--number")) |n| |
| 31 | debug.warn("--number = {}\n", .{n}); | 32 | debug.warn("--number = {}\n", .{n}); |
| 33 | for (args.options("--string")) |s| | ||
| 34 | debug.warn("--string = {}\n", .{s}); | ||
| 32 | for (args.positionals()) |pos| | 35 | for (args.positionals()) |pos| |
| 33 | debug.warn("{}\n", .{pos}); | 36 | debug.warn("{}\n", .{pos}); |
| 34 | } | 37 | } |
diff --git a/example/simple.zig b/example/simple.zig index 3510317..adea9f9 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -7,10 +7,11 @@ pub fn main() !void { | |||
| 7 | // First we specify what parameters our program can take. | 7 | // First we specify what parameters our program can take. |
| 8 | // We can use `parseParam` to parse a string to a `Param(Help)` | 8 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 9 | const params = comptime [_]clap.Param(clap.Help){ | 9 | const params = comptime [_]clap.Param(clap.Help){ |
| 10 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 10 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 11 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 11 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, |
| 12 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 12 | clap.Param(clap.Help){ | 13 | clap.Param(clap.Help){ |
| 13 | .takes_value = true, | 14 | .takes_value = .One, |
| 14 | }, | 15 | }, |
| 15 | }; | 16 | }; |
| 16 | 17 | ||
| @@ -21,6 +22,8 @@ pub fn main() !void { | |||
| 21 | debug.warn("--help\n", .{}); | 22 | debug.warn("--help\n", .{}); |
| 22 | if (args.option("--number")) |n| | 23 | if (args.option("--number")) |n| |
| 23 | debug.warn("--number = {}\n", .{n}); | 24 | debug.warn("--number = {}\n", .{n}); |
| 25 | for (args.options("--string")) |s| | ||
| 26 | debug.warn("--string = {}\n", .{s}); | ||
| 24 | for (args.positionals()) |pos| | 27 | for (args.positionals()) |pos| |
| 25 | debug.warn("{}\n", .{pos}); | 28 | debug.warn("{}\n", .{pos}); |
| 26 | } | 29 | } |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index faf388a..b92a9e6 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -15,11 +15,11 @@ pub fn main() !void { | |||
| 15 | clap.Param(u8){ | 15 | clap.Param(u8){ |
| 16 | .id = 'n', | 16 | .id = 'n', |
| 17 | .names = clap.Names{ .short = 'n', .long = "number" }, | 17 | .names = clap.Names{ .short = 'n', .long = "number" }, |
| 18 | .takes_value = true, | 18 | .takes_value = .One, |
| 19 | }, | 19 | }, |
| 20 | clap.Param(u8){ | 20 | clap.Param(u8){ |
| 21 | .id = 'f', | 21 | .id = 'f', |
| 22 | .takes_value = true, | 22 | .takes_value = .One, |
| 23 | }, | 23 | }, |
| 24 | }; | 24 | }; |
| 25 | 25 | ||