summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--README.md98
1 files changed, 98 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b678b87
--- /dev/null
+++ b/README.md
@@ -0,0 +1,98 @@
1# zig-clap
2A non allocating, fast and easy to use command line argument parser library for Zig.
3
4# Example
5
6```
7const std = @import("std");
8const clap = @import("clap.zig");
9
10const debug = std.debug;
11const os = std.os;
12
13const Clap = clap.Clap;
14const Command = clap.Command;
15const Argument = clap.Argument;
16
17const Options = struct {
18 print_values: bool,
19 a: i64,
20 b: u64,
21 c: u8,
22};
23
24pub fn main() !void {
25 const parser = comptime Clap(Options).Builder
26 .init(
27 Options {
28 .print_values = false,
29 .a = 0,
30 .b = 0,
31 .c = 0,
32 }
33 )
34 .programName("My Test Command")
35 .author("Hejsil")
36 .version("v1")
37 .about("Prints some values to the screen... Maybe.")
38 .command(
39 Command.Builder
40 .init("command")
41 .arguments(
42 []Argument {
43 Argument.Builder
44 .init("a")
45 .help("Set the a field of Option.")
46 .short('a')
47 .takesValue(true)
48 .build(),
49 Argument.Builder
50 .init("b")
51 .help("Set the b field of Option.")
52 .short('b')
53 .takesValue(true)
54 .build(),
55 Argument.Builder
56 .init("c")
57 .help("Set the c field of Option.")
58 .short('c')
59 .takesValue(true)
60 .build(),
61 Argument.Builder
62 .init("print_values")
63 .help("Print all not 0 values.")
64 .short('p')
65 .long("print-values")
66 .build(),
67 }
68 )
69 .build()
70 )
71 .build();
72
73 const args = try os.argsAlloc(debug.global_allocator);
74 defer os.argsFree(debug.global_allocator, args);
75
76 const options = try parser.parse(args[1..]);
77
78 if (options.print_values) {
79 if (options.a != 0) debug.warn("a = {}\n", options.a);
80 if (options.b != 0) debug.warn("b = {}\n", options.b);
81 if (options.c != 0) debug.warn("c = {}\n", options.c);
82 }
83}
84```
85
86Running example:
87```
88sample % ./sample [0]
89sample % ./sample -a 2 -b 4 -c 6 [0]
90sample % ./sample -a 2 -b 4 -c 6 -p [0]
91a = 2
92b = 4
93c = 6
94sample % ./sample -a 2 -b 4 -c 6 --print-values [0]
95a = 2
96b = 4
97c = 6
98```