diff options
| author | 2019-11-27 23:13:31 +0900 | |
|---|---|---|
| committer | 2019-11-27 23:13:31 +0900 | |
| commit | a305e818bd986c599fff17141617bc4f890276cf (patch) | |
| tree | ff0a5c133c271d83de3e71ebc023ffc08bb1e3c9 /example | |
| parent | Breaking: OsIterator will now get the exe on init (diff) | |
| download | zig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.gz zig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.xz zig-clap-a305e818bd986c599fff17141617bc4f890276cf.zip | |
Add clap.parse as the simplest way of using the lib
Diffstat (limited to 'example')
| -rw-r--r-- | example/README.md.template | 50 | ||||
| -rw-r--r-- | example/comptime-clap-error.zig | 19 | ||||
| -rw-r--r-- | example/simple-error.zig | 15 | ||||
| -rw-r--r-- | example/simple.zig | 26 |
4 files changed, 71 insertions, 39 deletions
diff --git a/example/README.md.template b/example/README.md.template index 373a037..1fd90b0 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -15,45 +15,55 @@ A simple and easy to use command line argument parser library for Zig. | |||
| 15 | 15 | ||
| 16 | ## Examples | 16 | ## Examples |
| 17 | 17 | ||
| 18 | ### `StreamingClap` | 18 | ### `clap.parse` |
| 19 | 19 | ||
| 20 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | 20 | The simplest way to use this library is to just call the `clap.parse` function. |
| 21 | `args.Iterator` to provide it with arguments lazily. | ||
| 22 | 21 | ||
| 23 | ```zig | 22 | ```zig |
| 24 | {} | 23 | {} |
| 25 | ``` | 24 | ``` |
| 26 | 25 | ||
| 27 | ### `ComptimeClap` | 26 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates |
| 28 | 27 | that the strings you pass to `option` and `flag` are actually parameters that the program can take: | |
| 29 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes | ||
| 30 | them available through three functions (`flag`, `option`, `positionals`). | ||
| 31 | |||
| 32 | ```zig | ||
| 33 | {} | ||
| 34 | ``` | ||
| 35 | |||
| 36 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) | ||
| 37 | and validates that the strings you pass to `option` and `flag` are actually parameters that the | ||
| 38 | program can take: | ||
| 39 | 28 | ||
| 40 | ```zig | 29 | ```zig |
| 41 | {} | 30 | {} |
| 42 | ``` | 31 | ``` |
| 43 | 32 | ||
| 44 | ``` | 33 | ``` |
| 45 | zig-clap/src/comptime.zig:109:17: error: --helps is not a parameter. | 34 | zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter. |
| 46 | @compileError(name ++ " is not a parameter."); | 35 | @compileError(name ++ " is not a parameter."); |
| 47 | ^ | 36 | ^ |
| 48 | zig-clap/src/comptime.zig:77:45: note: called from here | 37 | zig-clap/clap/comptime.zig:77:45: note: called from here |
| 49 | const param = comptime findParam(name); | 38 | const param = comptime findParam(name); |
| 50 | ^ | 39 | ^ |
| 51 | zig-clap/example/comptime-clap-error.zig:18:18: note: called from here | 40 | zig-clap/clap.zig:238:31: note: called from here |
| 41 | return a.clap.flag(name); | ||
| 42 | ^ | ||
| 43 | zig-clap/example/simple-error.zig:16:18: note: called from here | ||
| 52 | _ = args.flag("--helps"); | 44 | _ = args.flag("--helps"); |
| 53 | ^ | ||
| 54 | ``` | 45 | ``` |
| 55 | 46 | ||
| 56 | Ofc, this limits you to parameters that are comptime known. | 47 | ### `ComptimeClap` |
| 48 | |||
| 49 | The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument | ||
| 50 | iterator. | ||
| 51 | |||
| 52 | ```zig | ||
| 53 | {} | ||
| 54 | ``` | ||
| 55 | |||
| 56 | ### `StreamingClap` | ||
| 57 | |||
| 58 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | ||
| 59 | `args.Iterator` to provide it with arguments lazily. | ||
| 60 | |||
| 61 | ```zig | ||
| 62 | {} | ||
| 63 | ``` | ||
| 64 | |||
| 65 | Currently, this parse is the only parser that allow an array of `Param` that | ||
| 66 | is generated at runtime. | ||
| 57 | 67 | ||
| 58 | ### `help` | 68 | ### `help` |
| 59 | 69 | ||
diff --git a/example/comptime-clap-error.zig b/example/comptime-clap-error.zig deleted file mode 100644 index 3209b74..0000000 --- a/example/comptime-clap-error.zig +++ /dev/null | |||
| @@ -1,19 +0,0 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | const allocator = std.heap.direct_allocator; | ||
| 6 | |||
| 7 | const params = [_]clap.Param(void){clap.Param(void){ | ||
| 8 | .names = clap.Names{ .short = 'h', .long = "help" }, | ||
| 9 | }}; | ||
| 10 | |||
| 11 | var iter = clap.args.OsIterator.init(allocator); | ||
| 12 | defer iter.deinit(); | ||
| 13 | const exe = try iter.next(); | ||
| 14 | |||
| 15 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); | ||
| 16 | defer args.deinit(); | ||
| 17 | |||
| 18 | _ = args.flag("--helps"); | ||
| 19 | } | ||
diff --git a/example/simple-error.zig b/example/simple-error.zig new file mode 100644 index 0000000..fc72a03 --- /dev/null +++ b/example/simple-error.zig | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | // First we specify what parameters our program can take. | ||
| 6 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 7 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 8 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | ||
| 9 | }; | ||
| 10 | |||
| 11 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | ||
| 12 | defer args.deinit(); | ||
| 13 | |||
| 14 | _ = args.flag("--helps"); | ||
| 15 | } | ||
diff --git a/example/simple.zig b/example/simple.zig new file mode 100644 index 0000000..f791447 --- /dev/null +++ b/example/simple.zig | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | |||
| 6 | pub fn main() !void { | ||
| 7 | // First we specify what parameters our program can take. | ||
| 8 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 9 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 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, | ||
| 12 | clap.Param(clap.Help){ | ||
| 13 | .takes_value = true, | ||
| 14 | }, | ||
| 15 | }; | ||
| 16 | |||
| 17 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | ||
| 18 | defer args.deinit(); | ||
| 19 | |||
| 20 | if (args.flag("--help")) | ||
| 21 | debug.warn("--help\n"); | ||
| 22 | if (args.option("--number")) |n| | ||
| 23 | debug.warn("--number = {}\n", n); | ||
| 24 | for (args.positionals()) |pos| | ||
| 25 | debug.warn("{}\n", pos); | ||
| 26 | } | ||