summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Jimmi HC2019-06-21 19:15:32 +0200
committerGravatar Jimmi HC2019-06-21 19:15:32 +0200
commit56e7be2835311888ef43f403e5d6bc2118c953fe (patch)
treea700e966b79ec441f8936c667bd325e37c4a61dc /README.md
parentupdated to newest version of zig (diff)
downloadzig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.tar.gz
zig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.tar.xz
zig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.zip
Embed examples in README during build
fixes #11
Diffstat (limited to 'README.md')
-rw-r--r--README.md237
1 files changed, 163 insertions, 74 deletions
diff --git a/README.md b/README.md
index 8081d84..c959009 100644
--- a/README.md
+++ b/README.md
@@ -18,27 +18,64 @@ A simple and easy to use command line argument parser library for Zig.
18The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 18The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
19`args.Iterator` to provide it with arguments lazily. 19`args.Iterator` to provide it with arguments lazily.
20 20
21```rust 21```zig
22const params = []clap.Param(u8){ 22const std = @import("std");
23 clap.Param(u8).flag('h', clap.Names.both("help")), 23const clap = @import("clap");
24 clap.Param(u8).option('n', clap.Names.both("number")), 24
25 clap.Param(u8).positional('f'), 25const debug = std.debug;
26}; 26
27 27pub fn main() !void {
28var iter = clap.args.OsIterator.init(allocator); 28 var direct_allocator = std.heap.DirectAllocator.init();
29defer iter.deinit(); 29 const allocator = &direct_allocator.allocator;
30const exe = try iter.next(); 30 defer direct_allocator.deinit();
31 31
32var parser = clap.StreamingClap(u8, clap.args.OsIterator).init(params, &iter); 32 // First we specify what parameters our program can take.
33 33 const params = [_]clap.Param(u8){
34while (try parser.next()) |arg| { 34 clap.Param(u8){
35 switch (arg.param.id) { 35 .id = 'h',
36 'h' => debug.warn("Help!\n"), 36 .names = clap.Names{ .short = 'h', .long = "help" },
37 'n' => debug.warn("--number = {}\n", arg.value.?), 37 },
38 'f' => debug.warn("{}\n", arg.value.?), 38 clap.Param(u8){
39 else => unreachable, 39 .id = 'n',
40 .names = clap.Names{ .short = 'n', .long = "number" },
41 .takes_value = true,
42 },
43 clap.Param(u8){
44 .id = 'f',
45 .takes_value = true,
46 },
47 };
48
49 // We then initialize an argument iterator. We will use the OsIterator as it nicely
50 // wraps iterating over arguments the most efficient way on each os.
51 var iter = clap.args.OsIterator.init(allocator);
52 defer iter.deinit();
53
54 // Consume the exe arg.
55 const exe = try iter.next();
56
57 // Finally we initialize our streaming parser.
58 var parser = clap.StreamingClap(u8, clap.args.OsIterator){
59 .params = params,
60 .iter = &iter,
61 };
62
63 // Because we use a streaming parser, we have to consume each argument parsed individually.
64 while (try parser.next()) |arg| {
65 // arg.param will point to the parameter which matched the argument.
66 switch (arg.param.id) {
67 'h' => debug.warn("Help!\n"),
68 'n' => debug.warn("--number = {}\n", arg.value.?),
69
70 // arg.value == null, if arg.param.takes_value == false.
71 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
72 // or "-a 10".
73 'f' => debug.warn("{}\n", arg.value.?),
74 else => unreachable,
75 }
40 } 76 }
41} 77}
78
42``` 79```
43 80
44### `ComptimeClap` 81### `ComptimeClap`
@@ -46,57 +83,100 @@ while (try parser.next()) |arg| {
46The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes 83The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes
47them available through three functions (`flag`, `option`, `positionals`). 84them available through three functions (`flag`, `option`, `positionals`).
48 85
49```rust 86```zig
50const params = comptime []clap.Param(void){ 87const std = @import("std");
51 clap.Param(void).flag({}, clap.Names.both("help")), 88const clap = @import("clap");
52 clap.Param(void).option({}, clap.Names.both("number")), 89
53 clap.Param(void).positional({}), 90const debug = std.debug;
54}; 91
55 92pub fn main() !void {
56var iter = clap.args.OsIterator.init(allocator); 93 const stdout_file = try std.io.getStdOut();
57defer iter.deinit(); 94 var stdout_out_stream = stdout_file.outStream();
58const exe = try iter.next(); 95 const stdout = &stdout_out_stream.stream;
59 96
60var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); 97 var direct_allocator = std.heap.DirectAllocator.init();
61defer args.deinit(); 98 const allocator = &direct_allocator.allocator;
62 99 defer direct_allocator.deinit();
63if (args.flag("--help")) 100
64 debug.warn("Help!\n"); 101 // First we specify what parameters our program can take.
65if (args.option("--number")) |n| 102 const params = [_]clap.Param([]const u8){
66 debug.warn("--number = {}\n", n); 103 clap.Param([]const u8){
67for (args.positionals()) |pos| 104 .id = "Display this help and exit.",
68 debug.warn("{}\n", pos); 105 .names = clap.Names{ .short = 'h', .long = "help" },
106 },
107 clap.Param([]const u8){
108 .id = "An option parameter, which takes a value.",
109 .names = clap.Names{ .short = 'n', .long = "number" },
110 .takes_value = true,
111 },
112 clap.Param([]const u8){
113 .id = "",
114 .takes_value = true,
115 },
116 };
117
118 // We then initialize an argument iterator. We will use the OsIterator as it nicely
119 // wraps iterating over arguments the most efficient way on each os.
120 var iter = clap.args.OsIterator.init(allocator);
121 defer iter.deinit();
122
123 // Consume the exe arg.
124 const exe = try iter.next();
125
126 // Finally we can parse the arguments
127 var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter);
128 defer args.deinit();
129
130 if (args.flag("--help"))
131 debug.warn("--help\n");
132 if (args.option("--number")) |n|
133 debug.warn("--number = {}\n", n);
134 for (args.positionals()) |pos|
135 debug.warn("{}\n", pos);
136}
137
69``` 138```
70 139
71The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) 140The data structure returned from this parser has lookup speed on par with array access (`arr[i]`)
72and validates that the strings you pass to `option` and `flag` are actually parameters that the 141and validates that the strings you pass to `option` and `flag` are actually parameters that the
73program can take: 142program can take:
74 143
75```rust 144```zig
76const params = comptime []clap.Param(void){ 145const std = @import("std");
77 clap.Param(void).flag({}, clap.Names.both("help")), 146const clap = @import("clap");
78}; 147
148pub fn main() !void {
149 const params = [_]clap.Param(void){
150 clap.Param(void){
151 .names = clap.Names{ .short = 'h', .long = "help" }
152 },
153 };
154
155 var direct_allocator = std.heap.DirectAllocator.init();
156 const allocator = &direct_allocator.allocator;
157 defer direct_allocator.deinit();
79 158
80var iter = clap.args.OsIterator.init(allocator); 159 var iter = clap.args.OsIterator.init(allocator);
81defer iter.deinit(); 160 defer iter.deinit();
82const exe = try iter.next(); 161 const exe = try iter.next();
83 162
84var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); 163 var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter);
85defer args.deinit(); 164 defer args.deinit();
165
166 _ = args.flag("--helps");
167}
86 168
87if (args.flag("--helps"))
88 debug.warn("Help!\n");
89``` 169```
90 170
91``` 171```
92zig-clap/src/comptime.zig:103:17: error: --helps is not a parameter. 172zig-clap/src/comptime.zig:116:17: error: --helps is not a parameter.
93 @compileError(name ++ " is not a parameter."); 173 @compileError(name ++ " is not a parameter.");
94 ^ 174 ^
95zig-clap/src/comptime.zig:71:45: note: called from here 175zig-clap/src/comptime.zig:84:45: note: called from here
96 const param = comptime findParam(name); 176 const param = comptime findParam(name);
97 ^ 177 ^
98zig-clap/example/comptime-clap.zig:41:18: note: called from here 178zig-clap/example/comptime-clap-error.zig:22:18: note: called from here
99 if (args.flag("--helps")) 179 _ = args.flag("--helps");
100 ^ 180 ^
101``` 181```
102 182
@@ -107,29 +187,38 @@ Ofc, this limits you to parameters that are comptime known.
107The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the 187The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the
108program can take. 188program can take.
109 189
110```rust 190```zig
111const stderr_file = try std.io.getStdErr(); 191const std = @import("std");
112var stderr_out_stream = stderr_file.outStream(); 192const clap = @import("clap");
113const stderr = &stderr_out_stream.stream; 193
114 194pub fn main() !void {
115try clap.help( 195 const stderr_file = try std.io.getStdErr();
116 stderr, 196 var stderr_out_stream = stderr_file.outStream();
117 []clap.Param([]const u8){ 197 const stderr = &stderr_out_stream.stream;
118 clap.Param([]const u8).flag( 198
119 "Display this help and exit.", 199 // clap.help is a function that can print a simple help message, given a
120 clap.Names.both("help"), 200 // slice of Param([]const u8). There is also a helpEx, which can print a
121 ), 201 // help message for any Param, but it is more verbose to call.
122 clap.Param([]const u8).flag( 202 try clap.help(
123 "Output version information and exit.", 203 stderr,
124 clap.Names.both("version"), 204 [_]clap.Param([]const u8){
125 ), 205 clap.Param([]const u8){
126 }, 206 .id = "Display this help and exit.",
127); 207 .names = clap.Names{ .short = 'h', .long = "help" }
208 },
209 clap.Param([]const u8){
210 .id = "Output version information and exit.",
211 .names = clap.Names{ .short = 'v', .long = "version" }
212 },
213 },
214 );
215}
216
128``` 217```
129 218
130``` 219```
131 -h, --help Display this help and exit. 220 -h, --help Display this help and exit.
132 -v, --version Output version information and exit. 221 -v, --version Output version information and exit.
133``` 222```
134 223
135The `help` function is the simplest to call. It only takes an `OutStream` and a slice of 224The `help` function is the simplest to call. It only takes an `OutStream` and a slice of