summaryrefslogtreecommitdiff
path: root/example.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--example.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/example.zig b/example.zig
new file mode 100644
index 0000000..c77ef5c
--- /dev/null
+++ b/example.zig
@@ -0,0 +1,79 @@
1const std = @import("std");
2const clap = @import("clap.zig");
3
4const debug = std.debug;
5const os = std.os;
6
7const Clap = clap.Clap;
8const Command = clap.Command;
9const Argument = clap.Argument;
10
11const Options = struct {
12 print_values: bool,
13 a: i64,
14 b: u64,
15 c: u8,
16 d: []const u8,
17};
18
19// Output on windows:
20// zig-clap> .\example.exe -a 1
21// zig-clap> .\example.exe -p -a 1
22// a = 1
23// zig-clap> .\example.exe -pa 1
24// a = 1
25// zig-clap> .\example.exe -pd friend
26// d = friend
27// zig-clap> .\example.exe -pd=friend
28// d = friend
29// zig-clap> .\example.exe -p -d=friend
30// d = friend
31pub fn main() !void {
32 const parser = comptime Clap(Options).init(
33 Options {
34 .print_values = false,
35 .a = 0,
36 .b = 0,
37 .c = 0,
38 .d = "",
39 }
40 )
41 .with("program_name", "My Test Command")
42 .with("author", "Hejsil")
43 .with("version", "v1")
44 .with("about", "Prints some values to the screen... Maybe.")
45 .with("command", Command.init("command")
46 .with("arguments",
47 []Argument {
48 Argument.arg("a")
49 .with("help", "Set the a field of Option.")
50 .with("takes_value", clap.parse.int(i64, 10)),
51 Argument.arg("b")
52 .with("help", "Set the b field of Option.")
53 .with("takes_value", clap.parse.int(u64, 10)),
54 Argument.arg("c")
55 .with("help", "Set the c field of Option.")
56 .with("takes_value", clap.parse.int(u8, 10)),
57 Argument.arg("d")
58 .with("help", "Set the d field of Option.")
59 .with("takes_value", clap.parse.string),
60 Argument.field("print_values")
61 .with("help", "Print all not 0 values.")
62 .with("short", 'p')
63 .with("long", "print-values"),
64 }
65 )
66 );
67
68 const args = try os.argsAlloc(debug.global_allocator);
69 defer os.argsFree(debug.global_allocator, args);
70
71 const options = try parser.parse(args[1..]);
72
73 if (options.print_values) {
74 if (options.a != 0) debug.warn("a = {}\n", options.a);
75 if (options.b != 0) debug.warn("b = {}\n", options.b);
76 if (options.c != 0) debug.warn("c = {}\n", options.c);
77 if (options.d.len != 0) debug.warn("d = {}\n", options.d);
78 }
79}