summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-03-23 20:05:28 +0100
committerGravatar Komari Spaghetti2022-03-23 21:48:20 +0100
commit5166a15378a9c32d9b680417807000ba65d06141 (patch)
treebf5b37a04d9bd41128971e7bfd5e1dccef30bb36 /example
parentRefactor parseParam into a state machine (diff)
downloadzig-clap-5166a15378a9c32d9b680417807000ba65d06141.tar.gz
zig-clap-5166a15378a9c32d9b680417807000ba65d06141.tar.xz
zig-clap-5166a15378a9c32d9b680417807000ba65d06141.zip
Add parseParams and friends
Diffstat (limited to 'example')
-rw-r--r--example/README.md.template4
-rw-r--r--example/help.zig9
-rw-r--r--example/simple-ex.zig15
-rw-r--r--example/simple.zig15
-rw-r--r--example/usage.zig11
5 files changed, 29 insertions, 25 deletions
diff --git a/example/README.md.template b/example/README.md.template
index 9fbc1cc..c19c1cd 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -35,8 +35,8 @@ The fields in `args` are typed. The type is based on the name of the value the p
35Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. 35Since `--number` takes a `usize` the field `res.args.number` has the type `usize`.
36 36
37Note that this is only the case because `clap.parsers.default` has a field called `usize` which 37Note that this is only the case because `clap.parsers.default` has a field called `usize` which
38contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default` 38contains a parser that returns `usize`. You can pass in something other than
39if you want some other mapping. 39`clap.parsers.default` if you want some other mapping.
40 40
41```zig 41```zig
42{s} 42{s}
diff --git a/example/help.zig b/example/help.zig
index f3edb58..64d1709 100644
--- a/example/help.zig
+++ b/example/help.zig
@@ -2,10 +2,11 @@ const clap = @import("clap");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub fn main() !void {
5 const params = comptime [_]clap.Param(clap.Help){ 5 const params = comptime clap.parseParamsComptime(
6 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 6 \\-h, --help Display this help and exit.
7 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 7 \\-v, --version Output version information and exit.
8 }; 8 \\
9 );
9 10
10 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{}); 11 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
11 defer res.deinit(); 12 defer res.deinit();
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index 6cb4c3f..d0d214d 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -7,13 +7,14 @@ const process = std.process;
7 7
8pub fn main() !void { 8pub fn main() !void {
9 // First we specify what parameters our program can take. 9 // First we specify what parameters our program can take.
10 // We can use `parseParam` to parse a string to a `Param(Help)` 10 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`
11 const params = comptime [_]clap.Param(clap.Help){ 11 const params = comptime clap.parseParamsComptime(
12 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 12 \\-h, --help Display this help and exit.
13 clap.parseParam("-n, --number <INT> An option parameter, which takes a value.") catch unreachable, 13 \\-n, --number <INT> An option parameter, which takes a value.
14 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 14 \\-s, --string <STR>... An option parameter which can be specified multiple times.
15 clap.parseParam("<FILE>...") catch unreachable, 15 \\<FILE>...
16 }; 16 \\
17 );
17 18
18 // Declare our own parsers which are used to map the argument strings to other 19 // Declare our own parsers which are used to map the argument strings to other
19 // types. 20 // types.
diff --git a/example/simple.zig b/example/simple.zig
index 11e975e..1ac7de5 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -6,13 +6,14 @@ const io = std.io;
6 6
7pub fn main() !void { 7pub fn main() !void {
8 // First we specify what parameters our program can take. 8 // First we specify what parameters our program can take.
9 // We can use `parseParam` to parse a string to a `Param(Help)` 9 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`
10 const params = comptime [_]clap.Param(clap.Help){ 10 const params = comptime clap.parseParamsComptime(
11 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 11 \\-h, --help Display this help and exit.
12 clap.parseParam("-n, --number <usize> An option parameter, which takes a value.") catch unreachable, 12 \\-n, --number <usize> An option parameter, which takes a value.
13 clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable, 13 \\-s, --string <str>... An option parameter which can be specified multiple times.
14 clap.parseParam("<str>...") catch unreachable, 14 \\<str>...
15 }; 15 \\
16 );
16 17
17 // Initalize our diagnostics, which can be used for reporting useful errors. 18 // Initalize our diagnostics, which can be used for reporting useful errors.
18 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 19 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
diff --git a/example/usage.zig b/example/usage.zig
index 20d4736..f57b07c 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -2,11 +2,12 @@ const clap = @import("clap");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub fn main() !void {
5 const params = comptime [_]clap.Param(clap.Help){ 5 const params = comptime clap.parseParamsComptime(
6 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 6 \\-h, --help Display this help and exit.
7 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 7 \\-v, --version Output version information and exit.
8 clap.parseParam(" --value <str> An option parameter, which takes a value.") catch unreachable, 8 \\ --value <str> An option parameter, which takes a value.
9 }; 9 \\
10 );
10 11
11 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{}); 12 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
12 defer res.deinit(); 13 defer res.deinit();