diff options
| author | 2021-11-29 17:01:36 +0100 | |
|---|---|---|
| committer | 2021-11-29 17:09:26 +0100 | |
| commit | b2059e5d086731a37f5988bfc6bfdb0848f14ece (patch) | |
| tree | c2ebf7806f383f18ebc7253bbe9e6e9da2db39d4 /README.md | |
| parent | Fix chaining typo (diff) | |
| download | zig-clap-b2059e5d086731a37f5988bfc6bfdb0848f14ece.tar.gz zig-clap-b2059e5d086731a37f5988bfc6bfdb0848f14ece.tar.xz zig-clap-b2059e5d086731a37f5988bfc6bfdb0848f14ece.zip | |
Improve help and usage examples
Instead of just calling these function, have the examples be small
programs that demonstrates how you would actually use them together with
argument parsing.
fixes #57
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 38 |
1 files changed, 23 insertions, 15 deletions
| @@ -180,21 +180,25 @@ const clap = @import("clap"); | |||
| 180 | const std = @import("std"); | 180 | const std = @import("std"); |
| 181 | 181 | ||
| 182 | pub fn main() !void { | 182 | pub fn main() !void { |
| 183 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 184 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 185 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | ||
| 186 | }; | ||
| 187 | |||
| 188 | var args = try clap.parse(clap.Help, ¶ms, .{}); | ||
| 189 | defer args.deinit(); | ||
| 190 | |||
| 183 | // clap.help is a function that can print a simple help message, given a | 191 | // clap.help is a function that can print a simple help message, given a |
| 184 | // slice of Param(Help). There is also a helpEx, which can print a | 192 | // slice of Param(Help). There is also a helpEx, which can print a |
| 185 | // help message for any Param, but it is more verbose to call. | 193 | // help message for any Param, but it is more verbose to call. |
| 186 | try clap.help( | 194 | if (args.flag("--help")) |
| 187 | std.io.getStdErr().writer(), | 195 | return clap.help(std.io.getStdErr().writer(), ¶ms); |
| 188 | comptime &.{ | ||
| 189 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 190 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | ||
| 191 | }, | ||
| 192 | ); | ||
| 193 | } | 196 | } |
| 194 | 197 | ||
| 195 | ``` | 198 | ``` |
| 196 | 199 | ||
| 197 | ``` | 200 | ``` |
| 201 | $ zig-out/bin/help --help | ||
| 198 | -h, --help Display this help and exit. | 202 | -h, --help Display this help and exit. |
| 199 | -v, --version Output version information and exit. | 203 | -v, --version Output version information and exit. |
| 200 | ``` | 204 | ``` |
| @@ -218,22 +222,26 @@ const clap = @import("clap"); | |||
| 218 | const std = @import("std"); | 222 | const std = @import("std"); |
| 219 | 223 | ||
| 220 | pub fn main() !void { | 224 | pub fn main() !void { |
| 225 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 226 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 227 | clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, | ||
| 228 | clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, | ||
| 229 | }; | ||
| 230 | |||
| 231 | var args = try clap.parse(clap.Help, ¶ms, .{}); | ||
| 232 | defer args.deinit(); | ||
| 233 | |||
| 221 | // clap.usage is a function that can print a simple usage message, given a | 234 | // clap.usage is a function that can print a simple usage message, given a |
| 222 | // slice of Param(Help). There is also a usageEx, which can print a | 235 | // slice of Param(Help). There is also a usageEx, which can print a |
| 223 | // usage message for any Param, but it is more verbose to call. | 236 | // usage message for any Param, but it is more verbose to call. |
| 224 | try clap.usage( | 237 | if (args.flag("--help")) |
| 225 | std.io.getStdErr().writer(), | 238 | return clap.usage(std.io.getStdErr().writer(), ¶ms); |
| 226 | comptime &.{ | ||
| 227 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 228 | clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, | ||
| 229 | clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, | ||
| 230 | }, | ||
| 231 | ); | ||
| 232 | } | 239 | } |
| 233 | 240 | ||
| 234 | ``` | 241 | ``` |
| 235 | 242 | ||
| 236 | ``` | 243 | ``` |
| 244 | $ zig-out/bin/usage --help | ||
| 237 | [-hv] [--value <N>] | 245 | [-hv] [--value <N>] |
| 238 | ``` | 246 | ``` |
| 239 | 247 | ||