diff options
| author | 2019-08-17 15:21:45 +0200 | |
|---|---|---|
| committer | 2019-08-17 15:21:45 +0200 | |
| commit | 7399ee309e960733c3f6701eba685fbe284365cf (patch) | |
| tree | cb061d7d326a2a5385ecec2dc5bd0d11fa7a4d34 /README.md | |
| parent | build with latest version of zig (diff) | |
| download | zig-clap-7399ee309e960733c3f6701eba685fbe284365cf.tar.gz zig-clap-7399ee309e960733c3f6701eba685fbe284365cf.tar.xz zig-clap-7399ee309e960733c3f6701eba685fbe284365cf.zip | |
adds parseParam
a less verbose way of getting a Param(Help).
Diffstat (limited to '')
| -rw-r--r-- | README.md | 37 |
1 files changed, 12 insertions, 25 deletions
| @@ -91,18 +91,11 @@ pub fn main() !void { | |||
| 91 | const allocator = std.heap.direct_allocator; | 91 | const allocator = std.heap.direct_allocator; |
| 92 | 92 | ||
| 93 | // First we specify what parameters our program can take. | 93 | // First we specify what parameters our program can take. |
| 94 | const params = [_]clap.Param([]const u8){ | 94 | // We can use `parseParam` parse a string to a `Param(Help)` |
| 95 | clap.Param([]const u8){ | 95 | const params = comptime [_]clap.Param(clap.Help){ |
| 96 | .id = "Display this help and exit.", | 96 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 97 | .names = clap.Names{ .short = 'h', .long = "help" }, | 97 | clap.parseParam("-n, --number=NUM An option parameter, which takes a value.") catch unreachable, |
| 98 | }, | 98 | clap.Param(clap.Help){ |
| 99 | clap.Param([]const u8){ | ||
| 100 | .id = "An option parameter, which takes a value.", | ||
| 101 | .names = clap.Names{ .short = 'n', .long = "number" }, | ||
| 102 | .takes_value = true, | ||
| 103 | }, | ||
| 104 | clap.Param([]const u8){ | ||
| 105 | .id = "", | ||
| 106 | .takes_value = true, | 99 | .takes_value = true, |
| 107 | }, | 100 | }, |
| 108 | }; | 101 | }; |
| @@ -116,7 +109,7 @@ pub fn main() !void { | |||
| 116 | const exe = try iter.next(); | 109 | const exe = try iter.next(); |
| 117 | 110 | ||
| 118 | // Finally we can parse the arguments | 111 | // Finally we can parse the arguments |
| 119 | var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter); | 112 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); |
| 120 | defer args.deinit(); | 113 | defer args.deinit(); |
| 121 | 114 | ||
| 122 | if (args.flag("--help")) | 115 | if (args.flag("--help")) |
| @@ -187,19 +180,13 @@ pub fn main() !void { | |||
| 187 | const stderr = &stderr_out_stream.stream; | 180 | const stderr = &stderr_out_stream.stream; |
| 188 | 181 | ||
| 189 | // clap.help is a function that can print a simple help message, given a | 182 | // clap.help is a function that can print a simple help message, given a |
| 190 | // slice of Param([]const u8). There is also a helpEx, which can print a | 183 | // slice of Param(Help). There is also a helpEx, which can print a |
| 191 | // help message for any Param, but it is more verbose to call. | 184 | // help message for any Param, but it is more verbose to call. |
| 192 | try clap.help( | 185 | try clap.help( |
| 193 | stderr, | 186 | stderr, |
| 194 | [_]clap.Param([]const u8){ | 187 | comptime [_]clap.Param(clap.Help){ |
| 195 | clap.Param([]const u8){ | 188 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 196 | .id = "Display this help and exit.", | 189 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 197 | .names = clap.Names{ .short = 'h', .long = "help" }, | ||
| 198 | }, | ||
| 199 | clap.Param([]const u8){ | ||
| 200 | .id = "Output version information and exit.", | ||
| 201 | .names = clap.Names{ .short = 'v', .long = "version" }, | ||
| 202 | }, | ||
| 203 | }, | 190 | }, |
| 204 | ); | 191 | ); |
| 205 | } | 192 | } |
| @@ -211,8 +198,8 @@ pub fn main() !void { | |||
| 211 | -v, --version Output version information and exit. | 198 | -v, --version Output version information and exit. |
| 212 | ``` | 199 | ``` |
| 213 | 200 | ||
| 214 | The `help` function is the simplest to call. It only takes an `OutStream` and a slice of | 201 | The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of |
| 215 | `Param([]const u8)`. This function assumes that the id of each parameter is the help message. | 202 | `Param(Help)`. |
| 216 | 203 | ||
| 217 | The `helpEx` is the generic version of `help`. It can print a help message for any | 204 | The `helpEx` is the generic version of `help`. It can print a help message for any |
| 218 | `Param` give that the caller provides functions for getting the help and value strings. | 205 | `Param` give that the caller provides functions for getting the help and value strings. |