summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
committerGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
commitcfaac64c404fb1c2e892880410aa3b7dd881ea58 (patch)
tree0dca149e43daaaef41f55fa61375ab361c36a39c /example
parentFix minor typos in README.md (diff)
downloadzig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.tar.gz
zig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.tar.xz
zig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.zip
Change clap into generating a struct
This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access. - Move the current `clap.parse` and friends into `clap.untyped.parse` - This is in preperation for `clap.typed.parse`
Diffstat (limited to '')
-rw-r--r--example/README.md.template4
-rw-r--r--example/help.zig10
-rw-r--r--example/simple-ex.zig20
-rw-r--r--example/simple.zig20
-rw-r--r--example/streaming-clap.zig2
-rw-r--r--example/usage.zig12
6 files changed, 34 insertions, 34 deletions
diff --git a/example/README.md.template b/example/README.md.template
index 7f5c545..d792152 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -51,9 +51,9 @@ zig-clap/example/simple-error.zig:16:18: note: called from here
51 51
52There is also a `parseEx` variant that takes an argument iterator. 52There is also a `parseEx` variant that takes an argument iterator.
53 53
54### `StreamingClap` 54### `streaming.Clap`
55 55
56The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 56The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an
57`args.Iterator` to provide it with arguments lazily. 57`args.Iterator` to provide it with arguments lazily.
58 58
59```zig 59```zig
diff --git a/example/help.zig b/example/help.zig
index de3b707..2e7ca2b 100644
--- a/example/help.zig
+++ b/example/help.zig
@@ -3,16 +3,16 @@ const 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.Param(clap.Help){
6 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 6 clap.untyped.parseParam("-h, --help Display this help and exit. ") catch unreachable,
7 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 7 clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable,
8 }; 8 };
9 9
10 var args = try clap.parse(clap.Help, &params, .{}); 10 var res = try clap.untyped.parse(clap.Help, &params, .{});
11 defer args.deinit(); 11 defer res.deinit();
12 12
13 // clap.help is a function that can print a simple help message, given a 13 // clap.help is a function that can print a simple help message, given a
14 // slice of Param(Help). There is also a helpEx, which can print a 14 // slice of Param(Help). There is also a helpEx, which can print a
15 // help message for any Param, but it is more verbose to call. 15 // help message for any Param, but it is more verbose to call.
16 if (args.flag("--help")) 16 if (res.args.help)
17 return clap.help(std.io.getStdErr().writer(), &params); 17 return clap.help(std.io.getStdErr().writer(), &params);
18} 18}
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index d2dc77e..f1a958d 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -11,10 +11,10 @@ pub fn main() !void {
11 // First we specify what parameters our program can take. 11 // First we specify what parameters our program can take.
12 // We can use `parseParam` to parse a string to a `Param(Help)` 12 // We can use `parseParam` to parse a string to a `Param(Help)`
13 const params = comptime [_]clap.Param(clap.Help){ 13 const params = comptime [_]clap.Param(clap.Help){
14 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 14 clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable,
15 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 15 clap.untyped.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
16 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 16 clap.untyped.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
17 clap.parseParam("<POS>...") catch unreachable, 17 clap.untyped.parseParam("<POS>...") catch unreachable,
18 }; 18 };
19 19
20 var iter = try process.ArgIterator.initWithAllocator(allocator); 20 var iter = try process.ArgIterator.initWithAllocator(allocator);
@@ -27,7 +27,7 @@ pub fn main() !void {
27 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 27 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
28 // care about the extra information `Diagnostics` provides. 28 // care about the extra information `Diagnostics` provides.
29 var diag = clap.Diagnostic{}; 29 var diag = clap.Diagnostic{};
30 var args = clap.parseEx(clap.Help, &params, &iter, .{ 30 var res = clap.untyped.parseEx(clap.Help, &params, &iter, .{
31 .allocator = allocator, 31 .allocator = allocator,
32 .diagnostic = &diag, 32 .diagnostic = &diag,
33 }) catch |err| { 33 }) catch |err| {
@@ -35,14 +35,14 @@ pub fn main() !void {
35 diag.report(io.getStdErr().writer(), err) catch {}; 35 diag.report(io.getStdErr().writer(), err) catch {};
36 return err; 36 return err;
37 }; 37 };
38 defer args.deinit(); 38 defer res.deinit();
39 39
40 if (args.flag("--help")) 40 if (res.args.help)
41 debug.print("--help\n", .{}); 41 debug.print("--help\n", .{});
42 if (args.option("--number")) |n| 42 if (res.args.number) |n|
43 debug.print("--number = {s}\n", .{n}); 43 debug.print("--number = {s}\n", .{n});
44 for (args.options("--string")) |s| 44 for (res.args.string) |s|
45 debug.print("--string = {s}\n", .{s}); 45 debug.print("--string = {s}\n", .{s});
46 for (args.positionals()) |pos| 46 for (res.positionals) |pos|
47 debug.print("{s}\n", .{pos}); 47 debug.print("{s}\n", .{pos});
48} 48}
diff --git a/example/simple.zig b/example/simple.zig
index ff6d301..c37e896 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -8,29 +8,29 @@ pub 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 `parseParam` to parse a string to a `Param(Help)`
10 const params = comptime [_]clap.Param(clap.Help){ 10 const params = comptime [_]clap.Param(clap.Help){
11 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 11 clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable,
12 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 12 clap.untyped.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
13 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 13 clap.untyped.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
14 clap.parseParam("<POS>...") catch unreachable, 14 clap.untyped.parseParam("<POS>...") catch unreachable,
15 }; 15 };
16 16
17 // Initalize our diagnostics, which can be used for reporting useful errors. 17 // 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 18 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
19 // care about the extra information `Diagnostics` provides. 19 // care about the extra information `Diagnostics` provides.
20 var diag = clap.Diagnostic{}; 20 var diag = clap.Diagnostic{};
21 var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| { 21 var res = clap.untyped.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| {
22 // Report useful error and exit 22 // Report useful error and exit
23 diag.report(io.getStdErr().writer(), err) catch {}; 23 diag.report(io.getStdErr().writer(), err) catch {};
24 return err; 24 return err;
25 }; 25 };
26 defer args.deinit(); 26 defer res.deinit();
27 27
28 if (args.flag("--help")) 28 if (res.args.help)
29 debug.print("--help\n", .{}); 29 debug.print("--help\n", .{});
30 if (args.option("--number")) |n| 30 if (res.args.number) |n|
31 debug.print("--number = {s}\n", .{n}); 31 debug.print("--number = {s}\n", .{n});
32 for (args.options("--string")) |s| 32 for (res.args.string) |s|
33 debug.print("--string = {s}\n", .{s}); 33 debug.print("--string = {s}\n", .{s});
34 for (args.positionals()) |pos| 34 for (res.positionals) |pos|
35 debug.print("{s}\n", .{pos}); 35 debug.print("{s}\n", .{pos});
36} 36}
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index a7ab7d8..cacda56 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -32,7 +32,7 @@ pub fn main() !void {
32 // This is optional. You can also leave the `diagnostic` field unset if you 32 // This is optional. You can also leave the `diagnostic` field unset if you
33 // don't care about the extra information `Diagnostic` provides. 33 // don't care about the extra information `Diagnostic` provides.
34 var diag = clap.Diagnostic{}; 34 var diag = clap.Diagnostic{};
35 var parser = clap.StreamingClap(u8, process.ArgIterator){ 35 var parser = clap.streaming.Clap(u8, process.ArgIterator){
36 .params = &params, 36 .params = &params,
37 .iter = &iter, 37 .iter = &iter,
38 .diagnostic = &diag, 38 .diagnostic = &diag,
diff --git a/example/usage.zig b/example/usage.zig
index 368a6b3..04fedba 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -3,17 +3,17 @@ const 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.Param(clap.Help){
6 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 6 clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable,
7 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, 7 clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable,
8 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, 8 clap.untyped.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
9 }; 9 };
10 10
11 var args = try clap.parse(clap.Help, &params, .{}); 11 var res = try clap.untyped.parse(clap.Help, &params, .{});
12 defer args.deinit(); 12 defer res.deinit();
13 13
14 // clap.usage is a function that can print a simple usage message, given a 14 // clap.usage is a function that can print a simple usage message, given a
15 // slice of Param(Help). There is also a usageEx, which can print a 15 // slice of Param(Help). There is also a usageEx, which can print a
16 // usage message for any Param, but it is more verbose to call. 16 // usage message for any Param, but it is more verbose to call.
17 if (args.flag("--help")) 17 if (res.args.help)
18 return clap.usage(std.io.getStdErr().writer(), &params); 18 return clap.usage(std.io.getStdErr().writer(), &params);
19} 19}