diff options
| author | 2022-02-25 19:40:00 +0100 | |
|---|---|---|
| committer | 2022-03-09 18:12:40 +0100 | |
| commit | 5f7b75d5523d9581eca5a72a362868ff517992e8 (patch) | |
| tree | 5e874f9c935e0d7c838ea5aadf270ce2929f8e8a /example | |
| parent | Bump actions/checkout from 2.4.0 to 3 (diff) | |
| download | zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.gz zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.xz zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.zip | |
Allow for clap to parse argument values into types
This changes
- `.flag`, `.option`, `.options` and `.positionals` are now just fields
you access on the result of `parse` and `parseEx`.
- `clap.ComptimeClap` has been removed.
- `clap.StreamingClap` is now called `clap.streaming.Clap`
- `parse` and `parseEx` now takes a `value_parsers` argument that
provides the parsers to parse values.
- Remove `helpEx`, `helpFull`, `usageEx` and `usageFull`. They now just
expect `Id` to have methods for getting the description and value
texts.
Diffstat (limited to 'example')
| -rw-r--r-- | example/README.md.template | 51 | ||||
| -rw-r--r-- | example/help.zig | 8 | ||||
| -rw-r--r-- | example/simple-error.zig | 13 | ||||
| -rw-r--r-- | example/simple-ex.zig | 39 | ||||
| -rw-r--r-- | example/simple.zig | 24 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 2 | ||||
| -rw-r--r-- | example/usage.zig | 14 |
7 files changed, 59 insertions, 92 deletions
diff --git a/example/README.md.template b/example/README.md.template index 7f5c545..9fbc1cc 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -27,33 +27,24 @@ The simplest way to use this library is to just call the `clap.parse` function. | |||
| 27 | {s} | 27 | {s} |
| 28 | ``` | 28 | ``` |
| 29 | 29 | ||
| 30 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates | 30 | The result will contain an `args` field and a `positionals` field. `args` will have one field |
| 31 | that the strings you pass to `option`, `options` and `flag` are actually parameters that the | 31 | for each none positional parameter of your program. The name of the field will be the longest |
| 32 | program can take: | 32 | name of the parameter. |
| 33 | |||
| 34 | The fields in `args` are typed. The type is based on the name of the value the parameter takes. | ||
| 35 | Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. | ||
| 36 | |||
| 37 | Note that this is only the case because `clap.parsers.default` has a field called `usize` which | ||
| 38 | contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default` | ||
| 39 | if you want some other mapping. | ||
| 33 | 40 | ||
| 34 | ```zig | 41 | ```zig |
| 35 | {s} | 42 | {s} |
| 36 | ``` | 43 | ``` |
| 37 | 44 | ||
| 38 | ``` | 45 | ### `streaming.Clap` |
| 39 | zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter. | ||
| 40 | @compileError(name ++ " is not a parameter."); | ||
| 41 | ^ | ||
| 42 | zig-clap/clap/comptime.zig:77:45: note: called from here | ||
| 43 | const param = comptime findParam(name); | ||
| 44 | ^ | ||
| 45 | zig-clap/clap.zig:238:31: note: called from here | ||
| 46 | return a.clap.flag(name); | ||
| 47 | ^ | ||
| 48 | zig-clap/example/simple-error.zig:16:18: note: called from here | ||
| 49 | _ = args.flag("--helps"); | ||
| 50 | ``` | ||
| 51 | |||
| 52 | There is also a `parseEx` variant that takes an argument iterator. | ||
| 53 | 46 | ||
| 54 | ### `StreamingClap` | 47 | The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an |
| 55 | |||
| 56 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | ||
| 57 | `args.Iterator` to provide it with arguments lazily. | 48 | `args.Iterator` to provide it with arguments lazily. |
| 58 | 49 | ||
| 59 | ```zig | 50 | ```zig |
| @@ -65,8 +56,9 @@ is generated at runtime. | |||
| 65 | 56 | ||
| 66 | ### `help` | 57 | ### `help` |
| 67 | 58 | ||
| 68 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the | 59 | The `help` prints a simple list of all parameters the program can take. It expects the |
| 69 | program can take. | 60 | `Id` to have a `description` method and an `value` method so that it can provide that |
| 61 | in the output. | ||
| 70 | 62 | ||
| 71 | ```zig | 63 | ```zig |
| 72 | {s} | 64 | {s} |
| @@ -78,19 +70,10 @@ $ zig-out/bin/help --help | |||
| 78 | -v, --version Output version information and exit. | 70 | -v, --version Output version information and exit. |
| 79 | ``` | 71 | ``` |
| 80 | 72 | ||
| 81 | The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of | ||
| 82 | `Param(Help)`. | ||
| 83 | |||
| 84 | The `helpEx` is the generic version of `help`. It can print a help message for any | ||
| 85 | `Param` give that the caller provides functions for getting the help and value strings. | ||
| 86 | |||
| 87 | The `helpFull` is even more generic, allowing the functions that get the help and value strings | ||
| 88 | to return errors and take a context as a parameter. | ||
| 89 | |||
| 90 | ### `usage` | 73 | ### `usage` |
| 91 | 74 | ||
| 92 | The `usage`, `usageEx` and `usageFull` are functions for printing a small abbreviated version | 75 | The `usage` prints a small abbreviated version of the help message. It expects the `Id` |
| 93 | of the help message. | 76 | to have a `value` method so it can provide that in the output. |
| 94 | 77 | ||
| 95 | ```zig | 78 | ```zig |
| 96 | {s} | 79 | {s} |
diff --git a/example/help.zig b/example/help.zig index de3b707..f3edb58 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -7,12 +7,12 @@ pub fn main() !void { | |||
| 7 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 7 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 8 | }; | 8 | }; |
| 9 | 9 | ||
| 10 | var args = try clap.parse(clap.Help, ¶ms, .{}); | 10 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); |
| 11 | defer args.deinit(); | 11 | defer res.deinit(); |
| 12 | 12 | ||
| 13 | // clap.help is a function that can print a simple help message, given a | 13 | // clap.help is a function that can print a simple help message, given a |
| 14 | // slice of Param(Help). There is also a helpEx, which can print a | 14 | // slice of Param(Help). There is also a helpEx, which can print a |
| 15 | // help message for any Param, but it is more verbose to call. | 15 | // help message for any Param, but it is more verbose to call. |
| 16 | if (args.flag("--help")) | 16 | if (res.args.help) |
| 17 | return clap.help(std.io.getStdErr().writer(), ¶ms); | 17 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 18 | } | 18 | } |
diff --git a/example/simple-error.zig b/example/simple-error.zig deleted file mode 100644 index c04a9c6..0000000 --- a/example/simple-error.zig +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | const clap = @import("clap"); | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 6 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | ||
| 7 | }; | ||
| 8 | |||
| 9 | var args = try clap.parse(clap.Help, ¶ms, .{}); | ||
| 10 | defer args.deinit(); | ||
| 11 | |||
| 12 | _ = args.flag("--helps"); | ||
| 13 | } | ||
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d2dc77e..6cb4c3f 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -6,43 +6,38 @@ const io = std.io; | |||
| 6 | const process = std.process; | 6 | const process = std.process; |
| 7 | 7 | ||
| 8 | pub fn main() !void { | 8 | pub fn main() !void { |
| 9 | const allocator = std.heap.page_allocator; | ||
| 10 | |||
| 11 | // First we specify what parameters our program can take. | 9 | // First we specify what parameters our program can take. |
| 12 | // 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)` |
| 13 | const params = comptime [_]clap.Param(clap.Help){ | 11 | const params = comptime [_]clap.Param(clap.Help){ |
| 14 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 12 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 15 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 13 | clap.parseParam("-n, --number <INT> An option parameter, which takes a value.") catch unreachable, |
| 16 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | 14 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, |
| 17 | clap.parseParam("<POS>...") catch unreachable, | 15 | clap.parseParam("<FILE>...") catch unreachable, |
| 18 | }; | 16 | }; |
| 19 | 17 | ||
| 20 | var iter = try process.ArgIterator.initWithAllocator(allocator); | 18 | // Declare our own parsers which are used to map the argument strings to other |
| 21 | defer iter.deinit(); | 19 | // types. |
| 22 | 20 | const parsers = comptime .{ | |
| 23 | // Skip exe argument | 21 | .STR = clap.parsers.string, |
| 24 | _ = iter.next(); | 22 | .FILE = clap.parsers.string, |
| 23 | .INT = clap.parsers.int(usize, 10), | ||
| 24 | }; | ||
| 25 | 25 | ||
| 26 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 27 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | ||
| 28 | // care about the extra information `Diagnostics` provides. | ||
| 29 | var diag = clap.Diagnostic{}; | 26 | var diag = clap.Diagnostic{}; |
| 30 | var args = clap.parseEx(clap.Help, ¶ms, &iter, .{ | 27 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 31 | .allocator = allocator, | ||
| 32 | .diagnostic = &diag, | 28 | .diagnostic = &diag, |
| 33 | }) catch |err| { | 29 | }) catch |err| { |
| 34 | // Report useful error and exit | ||
| 35 | diag.report(io.getStdErr().writer(), err) catch {}; | 30 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 36 | return err; | 31 | return err; |
| 37 | }; | 32 | }; |
| 38 | defer args.deinit(); | 33 | defer res.deinit(); |
| 39 | 34 | ||
| 40 | if (args.flag("--help")) | 35 | if (res.args.help) |
| 41 | debug.print("--help\n", .{}); | 36 | debug.print("--help\n", .{}); |
| 42 | if (args.option("--number")) |n| | 37 | if (res.args.number) |n| |
| 43 | debug.print("--number = {s}\n", .{n}); | 38 | debug.print("--number = {}\n", .{n}); |
| 44 | for (args.options("--string")) |s| | 39 | for (res.args.string) |s| |
| 45 | debug.print("--string = {s}\n", .{s}); | 40 | debug.print("--string = {s}\n", .{s}); |
| 46 | for (args.positionals()) |pos| | 41 | for (res.positionals) |pos| |
| 47 | debug.print("{s}\n", .{pos}); | 42 | debug.print("{s}\n", .{pos}); |
| 48 | } | 43 | } |
diff --git a/example/simple.zig b/example/simple.zig index ff6d301..11e975e 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -8,29 +8,31 @@ pub fn main() !void { | |||
| 8 | // First we specify what parameters our program can take. | 8 | // First we specify what parameters our program can take. |
| 9 | // We can use `parseParam` to parse a string to a `Param(Help)` | 9 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 10 | const params = comptime [_]clap.Param(clap.Help){ | 10 | const params = comptime [_]clap.Param(clap.Help){ |
| 11 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 11 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 12 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 12 | clap.parseParam("-n, --number <usize> An option parameter, which takes a value.") catch unreachable, |
| 13 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | 13 | clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable, |
| 14 | clap.parseParam("<POS>...") catch unreachable, | 14 | clap.parseParam("<str>...") catch unreachable, |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | // Initalize our diagnostics, which can be used for reporting useful errors. | 17 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 18 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 18 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 19 | // care about the extra information `Diagnostics` provides. | 19 | // care about the extra information `Diagnostics` provides. |
| 20 | var diag = clap.Diagnostic{}; | 20 | var diag = clap.Diagnostic{}; |
| 21 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | 21 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 22 | .diagnostic = &diag, | ||
| 23 | }) catch |err| { | ||
| 22 | // Report useful error and exit | 24 | // Report useful error and exit |
| 23 | diag.report(io.getStdErr().writer(), err) catch {}; | 25 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 24 | return err; | 26 | return err; |
| 25 | }; | 27 | }; |
| 26 | defer args.deinit(); | 28 | defer res.deinit(); |
| 27 | 29 | ||
| 28 | if (args.flag("--help")) | 30 | if (res.args.help) |
| 29 | debug.print("--help\n", .{}); | 31 | debug.print("--help\n", .{}); |
| 30 | if (args.option("--number")) |n| | 32 | if (res.args.number) |n| |
| 31 | debug.print("--number = {s}\n", .{n}); | 33 | debug.print("--number = {}\n", .{n}); |
| 32 | for (args.options("--string")) |s| | 34 | for (res.args.string) |s| |
| 33 | debug.print("--string = {s}\n", .{s}); | 35 | debug.print("--string = {s}\n", .{s}); |
| 34 | for (args.positionals()) |pos| | 36 | for (res.positionals) |pos| |
| 35 | debug.print("{s}\n", .{pos}); | 37 | debug.print("{s}\n", .{pos}); |
| 36 | } | 38 | } |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index a7ab7d8..cacda56 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -32,7 +32,7 @@ pub fn main() !void { | |||
| 32 | // This is optional. You can also leave the `diagnostic` field unset if you | 32 | // This is optional. You can also leave the `diagnostic` field unset if you |
| 33 | // don't care about the extra information `Diagnostic` provides. | 33 | // don't care about the extra information `Diagnostic` provides. |
| 34 | var diag = clap.Diagnostic{}; | 34 | var diag = clap.Diagnostic{}; |
| 35 | var parser = clap.StreamingClap(u8, process.ArgIterator){ | 35 | var parser = clap.streaming.Clap(u8, process.ArgIterator){ |
| 36 | .params = ¶ms, | 36 | .params = ¶ms, |
| 37 | .iter = &iter, | 37 | .iter = &iter, |
| 38 | .diagnostic = &diag, | 38 | .diagnostic = &diag, |
diff --git a/example/usage.zig b/example/usage.zig index 368a6b3..20d4736 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -3,17 +3,17 @@ const std = @import("std"); | |||
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const params = comptime [_]clap.Param(clap.Help){ | 5 | const params = comptime [_]clap.Param(clap.Help){ |
| 6 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 6 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 7 | clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, | 7 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 8 | clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, | 8 | clap.parseParam(" --value <str> An option parameter, which takes a value.") catch unreachable, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | var args = try clap.parse(clap.Help, ¶ms, .{}); | 11 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); |
| 12 | defer args.deinit(); | 12 | defer res.deinit(); |
| 13 | 13 | ||
| 14 | // clap.usage is a function that can print a simple usage message, given a | 14 | // clap.usage is a function that can print a simple usage message, given a |
| 15 | // slice of Param(Help). There is also a usageEx, which can print a | 15 | // slice of Param(Help). There is also a usageEx, which can print a |
| 16 | // usage message for any Param, but it is more verbose to call. | 16 | // usage message for any Param, but it is more verbose to call. |
| 17 | if (args.flag("--help")) | 17 | if (res.args.help) |
| 18 | return clap.usage(std.io.getStdErr().writer(), ¶ms); | 18 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 19 | } | 19 | } |