summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2024-10-22 17:46:47 +0200
committerGravatar Jimmi Holst Christensen2024-10-22 17:48:34 +0200
commite73b56aa4bcb7e53144ef96ee978f2a19b32669d (patch)
tree0ab5e3d426e25d2ad9d2e0cd0015fc010d9ea182 /example
parentfeat: Add `terminating_positional` to `clap.ParseOptions` (diff)
downloadzig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.gz
zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.xz
zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.zip
refactor: Always access using full namespace
This is my new preferred style of programming Zig :)
Diffstat (limited to '')
-rw-r--r--example/help.zig8
-rw-r--r--example/simple-ex.zig22
-rw-r--r--example/simple.zig19
-rw-r--r--example/streaming-clap.zig22
-rw-r--r--example/usage.zig6
5 files changed, 33 insertions, 44 deletions
diff --git a/example/help.zig b/example/help.zig
index 2f063c5..b80ee35 100644
--- a/example/help.zig
+++ b/example/help.zig
@@ -1,6 +1,3 @@
1const clap = @import("clap");
2const std = @import("std");
3
4pub fn main() !void { 1pub fn main() !void {
5 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 2 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
@@ -17,9 +14,12 @@ pub fn main() !void {
17 defer res.deinit(); 14 defer res.deinit();
18 15
19 // `clap.help` is a function that can print a simple help message. It can print any `Param` 16 // `clap.help` is a function that can print a simple help message. It can print any `Param`
20 // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). 17 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
21 // The last argument contains options as to how `help` should print those parameters. Using 18 // The last argument contains options as to how `help` should print those parameters. Using
22 // `.{}` means the default options. 19 // `.{}` means the default options.
23 if (res.args.help != 0) 20 if (res.args.help != 0)
24 return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{}); 21 return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
25} 22}
23
24const clap = @import("clap");
25const std = @import("std");
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index 2943f4e..4ca9791 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -1,10 +1,3 @@
1const clap = @import("clap");
2const std = @import("std");
3
4const debug = std.debug;
5const io = std.io;
6const process = std.process;
7
8pub fn main() !void { 1pub fn main() !void {
9 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 2 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
@@ -38,19 +31,22 @@ pub fn main() !void {
38 // allowed. 31 // allowed.
39 .assignment_separators = "=:", 32 .assignment_separators = "=:",
40 }) catch |err| { 33 }) catch |err| {
41 diag.report(io.getStdErr().writer(), err) catch {}; 34 diag.report(std.io.getStdErr().writer(), err) catch {};
42 return err; 35 return err;
43 }; 36 };
44 defer res.deinit(); 37 defer res.deinit();
45 38
46 if (res.args.help != 0) 39 if (res.args.help != 0)
47 debug.print("--help\n", .{}); 40 std.debug.print("--help\n", .{});
48 if (res.args.number) |n| 41 if (res.args.number) |n|
49 debug.print("--number = {}\n", .{n}); 42 std.debug.print("--number = {}\n", .{n});
50 if (res.args.answer) |a| 43 if (res.args.answer) |a|
51 debug.print("--answer = {s}\n", .{@tagName(a)}); 44 std.debug.print("--answer = {s}\n", .{@tagName(a)});
52 for (res.args.string) |s| 45 for (res.args.string) |s|
53 debug.print("--string = {s}\n", .{s}); 46 std.debug.print("--string = {s}\n", .{s});
54 for (res.positionals) |pos| 47 for (res.positionals) |pos|
55 debug.print("{s}\n", .{pos}); 48 std.debug.print("{s}\n", .{pos});
56} 49}
50
51const clap = @import("clap");
52const std = @import("std");
diff --git a/example/simple.zig b/example/simple.zig
index a7207c7..7f1bfc0 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -1,9 +1,3 @@
1const clap = @import("clap");
2const std = @import("std");
3
4const debug = std.debug;
5const io = std.io;
6
7pub fn main() !void { 1pub fn main() !void {
8 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 2 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
@@ -27,17 +21,20 @@ pub fn main() !void {
27 .allocator = gpa.allocator(), 21 .allocator = gpa.allocator(),
28 }) catch |err| { 22 }) catch |err| {
29 // Report useful error and exit 23 // Report useful error and exit
30 diag.report(io.getStdErr().writer(), err) catch {}; 24 diag.report(std.io.getStdErr().writer(), err) catch {};
31 return err; 25 return err;
32 }; 26 };
33 defer res.deinit(); 27 defer res.deinit();
34 28
35 if (res.args.help != 0) 29 if (res.args.help != 0)
36 debug.print("--help\n", .{}); 30 std.debug.print("--help\n", .{});
37 if (res.args.number) |n| 31 if (res.args.number) |n|
38 debug.print("--number = {}\n", .{n}); 32 std.debug.print("--number = {}\n", .{n});
39 for (res.args.string) |s| 33 for (res.args.string) |s|
40 debug.print("--string = {s}\n", .{s}); 34 std.debug.print("--string = {s}\n", .{s});
41 for (res.positionals) |pos| 35 for (res.positionals) |pos|
42 debug.print("{s}\n", .{pos}); 36 std.debug.print("{s}\n", .{pos});
43} 37}
38
39const clap = @import("clap");
40const std = @import("std");
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index 9d99859..2a20bcb 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -1,10 +1,3 @@
1const clap = @import("clap");
2const std = @import("std");
3
4const debug = std.debug;
5const io = std.io;
6const process = std.process;
7
8pub fn main() !void { 1pub fn main() !void {
9 const allocator = std.heap.page_allocator; 2 const allocator = std.heap.page_allocator;
10 3
@@ -22,7 +15,7 @@ pub fn main() !void {
22 .{ .id = 'f', .takes_value = .one }, 15 .{ .id = 'f', .takes_value = .one },
23 }; 16 };
24 17
25 var iter = try process.ArgIterator.initWithAllocator(allocator); 18 var iter = try std.process.ArgIterator.initWithAllocator(allocator);
26 defer iter.deinit(); 19 defer iter.deinit();
27 20
28 // Skip exe argument 21 // Skip exe argument
@@ -32,7 +25,7 @@ pub fn main() !void {
32 // This is optional. You can also leave the `diagnostic` field unset if you 25 // This is optional. You can also leave the `diagnostic` field unset if you
33 // don't care about the extra information `Diagnostic` provides. 26 // don't care about the extra information `Diagnostic` provides.
34 var diag = clap.Diagnostic{}; 27 var diag = clap.Diagnostic{};
35 var parser = clap.streaming.Clap(u8, process.ArgIterator){ 28 var parser = clap.streaming.Clap(u8, std.process.ArgIterator){
36 .params = &params, 29 .params = &params,
37 .iter = &iter, 30 .iter = &iter,
38 .diagnostic = &diag, 31 .diagnostic = &diag,
@@ -41,19 +34,22 @@ pub fn main() !void {
41 // Because we use a streaming parser, we have to consume each argument parsed individually. 34 // Because we use a streaming parser, we have to consume each argument parsed individually.
42 while (parser.next() catch |err| { 35 while (parser.next() catch |err| {
43 // Report useful error and exit 36 // Report useful error and exit
44 diag.report(io.getStdErr().writer(), err) catch {}; 37 diag.report(std.io.getStdErr().writer(), err) catch {};
45 return err; 38 return err;
46 }) |arg| { 39 }) |arg| {
47 // arg.param will point to the parameter which matched the argument. 40 // arg.param will point to the parameter which matched the argument.
48 switch (arg.param.id) { 41 switch (arg.param.id) {
49 'h' => debug.print("Help!\n", .{}), 42 'h' => std.debug.print("Help!\n", .{}),
50 'n' => debug.print("--number = {s}\n", .{arg.value.?}), 43 'n' => std.debug.print("--number = {s}\n", .{arg.value.?}),
51 44
52 // arg.value == null, if arg.param.takes_value == .none. 45 // arg.value == null, if arg.param.takes_value == .none.
53 // Otherwise, arg.value is the value passed with the argument, such as "-a=10" 46 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
54 // or "-a 10". 47 // or "-a 10".
55 'f' => debug.print("{s}\n", .{arg.value.?}), 48 'f' => std.debug.print("{s}\n", .{arg.value.?}),
56 else => unreachable, 49 else => unreachable,
57 } 50 }
58 } 51 }
59} 52}
53
54const clap = @import("clap");
55const std = @import("std");
diff --git a/example/usage.zig b/example/usage.zig
index a773dd2..59ac84a 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -1,6 +1,3 @@
1const clap = @import("clap");
2const std = @import("std");
3
4pub fn main() !void { 1pub fn main() !void {
5 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 2 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
@@ -22,3 +19,6 @@ pub fn main() !void {
22 if (res.args.help != 0) 19 if (res.args.help != 0)
23 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params); 20 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params);
24} 21}
22
23const clap = @import("clap");
24const std = @import("std");