From 5f7b75d5523d9581eca5a72a362868ff517992e8 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 25 Feb 2022 19:40:00 +0100 Subject: 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. --- example/README.md.template | 51 ++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 34 deletions(-) (limited to 'example/README.md.template') 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. {s} ``` -The data structure returned has lookup speed on par with array access (`arr[i]`) and validates -that the strings you pass to `option`, `options` and `flag` are actually parameters that the -program can take: +The result will contain an `args` field and a `positionals` field. `args` will have one field +for each none positional parameter of your program. The name of the field will be the longest +name of the parameter. + +The fields in `args` are typed. The type is based on the name of the value the parameter takes. +Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. + +Note that this is only the case because `clap.parsers.default` has a field called `usize` which +contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default` +if you want some other mapping. ```zig {s} ``` -``` -zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter. - @compileError(name ++ " is not a parameter."); - ^ -zig-clap/clap/comptime.zig:77:45: note: called from here - const param = comptime findParam(name); - ^ -zig-clap/clap.zig:238:31: note: called from here - return a.clap.flag(name); - ^ -zig-clap/example/simple-error.zig:16:18: note: called from here - _ = args.flag("--helps"); -``` - -There is also a `parseEx` variant that takes an argument iterator. +### `streaming.Clap` -### `StreamingClap` - -The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an +The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an `args.Iterator` to provide it with arguments lazily. ```zig @@ -65,8 +56,9 @@ is generated at runtime. ### `help` -The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the -program can take. +The `help` prints a simple list of all parameters the program can take. It expects the +`Id` to have a `description` method and an `value` method so that it can provide that +in the output. ```zig {s} @@ -78,19 +70,10 @@ $ zig-out/bin/help --help -v, --version Output version information and exit. ``` -The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of -`Param(Help)`. - -The `helpEx` is the generic version of `help`. It can print a help message for any -`Param` give that the caller provides functions for getting the help and value strings. - -The `helpFull` is even more generic, allowing the functions that get the help and value strings -to return errors and take a context as a parameter. - ### `usage` -The `usage`, `usageEx` and `usageFull` are functions for printing a small abbreviated version -of the help message. +The `usage` prints a small abbreviated version of the help message. It expects the `Id` +to have a `value` method so it can provide that in the output. ```zig {s} -- cgit v1.2.3