diff options
| author | 2018-11-14 14:06:20 +0100 | |
|---|---|---|
| committer | 2018-11-14 14:06:20 +0100 | |
| commit | c564b168785e740c37f47da4942825b25cb8b4ec (patch) | |
| tree | 880252c7a63bdc91f23ba5e13b593d4ca9ad8277 /README.md | |
| parent | Zig fmt (diff) | |
| download | zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.gz zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.xz zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.zip | |
Restructured and make StreamingClap simpler
* Also added a ComptimeClap
Diffstat (limited to '')
| -rw-r--r-- | README.md | 100 |
1 files changed, 96 insertions, 4 deletions
| @@ -1,17 +1,109 @@ | |||
| 1 | # zig-clap | 1 | # zig-clap |
| 2 | 2 | ||
| 3 | A simple and easy to use command line argument parser library for Zig. | 3 | A simple and easy to use command line argument parser library for Zig. |
| 4 | It's ment as a thin layer of abstraction over parsing arguments which allows | ||
| 5 | for further abstraction on top, such as filling in a `HashMap`. | ||
| 6 | 4 | ||
| 7 | ## Features | 5 | ## Features |
| 8 | 6 | ||
| 9 | See [example](https://github.com/Hejsil/zig-clap/blob/38a51948069f405864ab327826b5975a6d0c93a8/test.zig#L200-L247). | ||
| 10 | * Short arguments `-a` | 7 | * Short arguments `-a` |
| 11 | * Chaining `-abc` where `a` and `b` does not take values. | 8 | * Chaining `-abc` where `a` and `b` does not take values. |
| 12 | * Long arguments `--long` | 9 | * Long arguments `--long` |
| 13 | * Bare arguments `bare` | ||
| 14 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | 10 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 15 | * Short args also support passing values with no spacing or `=` (`-a100`) | 11 | * Short args also support passing values with no spacing or `=` (`-a100`) |
| 16 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) | 12 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) |
| 17 | 13 | ||
| 14 | ## Examples | ||
| 15 | |||
| 16 | ### `StreamingClap` | ||
| 17 | |||
| 18 | The `StreamingClap` is base of all the other parsers. It's a streaming parser that uses an | ||
| 19 | `args.Iterator` to provide it with arguments lazily. | ||
| 20 | |||
| 21 | ```rust | ||
| 22 | const params = []clap.Param(u8){ | ||
| 23 | clap.Param(u8).init('h', false, clap.Names.prefix("help")), | ||
| 24 | clap.Param(u8).init('n', true, clap.Names.prefix("number")), | ||
| 25 | clap.Param(u8).init('f', true, clap.Names.positional()), | ||
| 26 | }; | ||
| 27 | |||
| 28 | var os_iter = clap.args.OsIterator.init(allocator); | ||
| 29 | const iter = &os_iter.iter; | ||
| 30 | defer os_iter.deinit(); | ||
| 31 | |||
| 32 | const exe = try iter.next(); | ||
| 33 | |||
| 34 | var parser = clap.StreamingClap(u8, clap.args.OsIterator.Error).init(params, iter); | ||
| 35 | |||
| 36 | while (try parser.next()) |arg| { | ||
| 37 | switch (arg.param.id) { | ||
| 38 | 'h' => debug.warn("Help!\n"), | ||
| 39 | 'n' => debug.warn("--number = {}\n", arg.value.?), | ||
| 40 | 'f' => debug.warn("{}\n", arg.value.?), | ||
| 41 | else => unreachable, | ||
| 42 | } | ||
| 43 | } | ||
| 44 | ``` | ||
| 45 | |||
| 46 | ### `ComptimeClap` | ||
| 47 | |||
| 48 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes | ||
| 49 | them available through three functions (`flag`, `option`, `positionals`). | ||
| 50 | |||
| 51 | ```rust | ||
| 52 | const params = comptime []clap.Param(void){ | ||
| 53 | clap.Param(void).init({}, false, clap.Names.prefix("help")), | ||
| 54 | clap.Param(void).init({}, true, clap.Names.prefix("number")), | ||
| 55 | clap.Param(void).init({}, true, clap.Names.positional()), | ||
| 56 | }; | ||
| 57 | |||
| 58 | var os_iter = clap.args.OsIterator.init(allocator); | ||
| 59 | const iter = &os_iter.iter; | ||
| 60 | defer os_iter.deinit(); | ||
| 61 | |||
| 62 | const exe = try iter.next(); | ||
| 63 | |||
| 64 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator.Error, iter); | ||
| 65 | defer args.deinit(); | ||
| 66 | |||
| 67 | if (args.flag("--help")) | ||
| 68 | debug.warn("Help!\n"); | ||
| 69 | if (args.option("--number")) |n| | ||
| 70 | debug.warn("--number = {}\n", n); | ||
| 71 | for (args.positionals()) |pos| | ||
| 72 | debug.warn("{}\n", pos); | ||
| 73 | ``` | ||
| 74 | |||
| 75 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) | ||
| 76 | and validates that the strings you pass to `option` and `flag` are actually parameters that the | ||
| 77 | program can take: | ||
| 78 | |||
| 79 | ```rust | ||
| 80 | const params = comptime []clap.Param(void){ | ||
| 81 | clap.Param(void).init({}, false, clap.Names.prefix("help")), | ||
| 82 | }; | ||
| 83 | |||
| 84 | var os_iter = clap.args.OsIterator.init(allocator); | ||
| 85 | const iter = &os_iter.iter; | ||
| 86 | defer os_iter.deinit(); | ||
| 87 | |||
| 88 | const exe = try iter.next(); | ||
| 89 | |||
| 90 | var args = try clap.ComptimeClap(params).parse(allocator, clap.args.OsIterator.Error, iter); | ||
| 91 | defer args.deinit(); | ||
| 92 | |||
| 93 | if (args.flag("--helps")) | ||
| 94 | debug.warn("Help!\n"); | ||
| 95 | ``` | ||
| 96 | |||
| 97 | ``` | ||
| 98 | zig-clap/src/comptime.zig:103:17: error: --helps is not a parameter. | ||
| 99 | @compileError(name ++ " is not a parameter."); | ||
| 100 | ^ | ||
| 101 | zig-clap/src/comptime.zig:71:45: note: called from here | ||
| 102 | const param = comptime findParam(name); | ||
| 103 | ^ | ||
| 104 | zig-clap/example/comptime-clap.zig:41:18: note: called from here | ||
| 105 | if (args.flag("--helps")) | ||
| 106 | ^ | ||
| 107 | ``` | ||
| 108 | |||
| 109 | Ofc, this limits you to use only parameters that are comptime known. | ||