summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md38
1 files changed, 16 insertions, 22 deletions
diff --git a/README.md b/README.md
index ad9f026..398a088 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,7 @@ const clap = @import("clap");
29const std = @import("std"); 29const std = @import("std");
30 30
31const debug = std.debug; 31const debug = std.debug;
32const io = std.io;
32 33
33pub fn main() !void { 34pub fn main() !void {
34 // First we specify what parameters our program can take. 35 // First we specify what parameters our program can take.
@@ -46,7 +47,7 @@ pub fn main() !void {
46 var diag = clap.Diagnostic{}; 47 var diag = clap.Diagnostic{};
47 var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| { 48 var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| {
48 // Report useful error and exit 49 // Report useful error and exit
49 diag.report(std.io.getStdErr().outStream(), err) catch {}; 50 diag.report(io.getStdErr().writer(), err) catch {};
50 return err; 51 return err;
51 }; 52 };
52 defer args.deinit(); 53 defer args.deinit();
@@ -68,15 +69,15 @@ that the strings you pass to `option`, `options` and `flag` are actually paramet
68program can take: 69program can take:
69 70
70```zig 71```zig
71const std = @import("std");
72const clap = @import("clap"); 72const clap = @import("clap");
73const std = @import("std");
73 74
74pub fn main() !void { 75pub fn main() !void {
75 const params = comptime [_]clap.Param(clap.Help){ 76 const params = comptime [_]clap.Param(clap.Help){
76 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 77 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
77 }; 78 };
78 79
79 var args = try clap.parse(clap.Help, &params, std.heap.direct_allocator, null); 80 var args = try clap.parse(clap.Help, &params, .{});
80 defer args.deinit(); 81 defer args.deinit();
81 82
82 _ = args.flag("--helps"); 83 _ = args.flag("--helps");
@@ -110,25 +111,23 @@ const clap = @import("clap");
110const std = @import("std"); 111const std = @import("std");
111 112
112const debug = std.debug; 113const debug = std.debug;
114const io = std.io;
113 115
114pub fn main() !void { 116pub fn main() !void {
115 const allocator = std.heap.page_allocator; 117 const allocator = std.heap.page_allocator;
116 118
117 // First we specify what parameters our program can take. 119 // First we specify what parameters our program can take.
118 const params = [_]clap.Param(u8){ 120 const params = [_]clap.Param(u8){
119 clap.Param(u8){ 121 .{
120 .id = 'h', 122 .id = 'h',
121 .names = clap.Names{ .short = 'h', .long = "help" }, 123 .names = .{ .short = 'h', .long = "help" },
122 }, 124 },
123 clap.Param(u8){ 125 .{
124 .id = 'n', 126 .id = 'n',
125 .names = clap.Names{ .short = 'n', .long = "number" }, 127 .names = .{ .short = 'n', .long = "number" },
126 .takes_value = .One, 128 .takes_value = .one,
127 },
128 clap.Param(u8){
129 .id = 'f',
130 .takes_value = .One,
131 }, 129 },
130 .{ .id = 'f', .takes_value = .one },
132 }; 131 };
133 132
134 // We then initialize an argument iterator. We will use the OsIterator as it nicely 133 // We then initialize an argument iterator. We will use the OsIterator as it nicely
@@ -149,7 +148,7 @@ pub fn main() !void {
149 // Because we use a streaming parser, we have to consume each argument parsed individually. 148 // Because we use a streaming parser, we have to consume each argument parsed individually.
150 while (parser.next() catch |err| { 149 while (parser.next() catch |err| {
151 // Report useful error and exit 150 // Report useful error and exit
152 diag.report(std.io.getStdErr().outStream(), err) catch {}; 151 diag.report(io.getStdErr().writer(), err) catch {};
153 return err; 152 return err;
154 }) |arg| { 153 }) |arg| {
155 // arg.param will point to the parameter which matched the argument. 154 // arg.param will point to the parameter which matched the argument.
@@ -177,18 +176,15 @@ The `help`, `helpEx` and `helpFull` are functions for printing a simple list of
177program can take. 176program can take.
178 177
179```zig 178```zig
180const std = @import("std");
181const clap = @import("clap"); 179const clap = @import("clap");
180const std = @import("std");
182 181
183pub fn main() !void { 182pub fn main() !void {
184 const stderr_file = std.io.getStdErr();
185 var stderr_out_stream = stderr_file.outStream();
186
187 // clap.help is a function that can print a simple help message, given a 183 // clap.help is a function that can print a simple help message, given a
188 // slice of Param(Help). There is also a helpEx, which can print a 184 // slice of Param(Help). There is also a helpEx, which can print a
189 // help message for any Param, but it is more verbose to call. 185 // help message for any Param, but it is more verbose to call.
190 try clap.help( 186 try clap.help(
191 stderr_out_stream, 187 std.io.getStdErr().writer(),
192 comptime &[_]clap.Param(clap.Help){ 188 comptime &[_]clap.Param(clap.Help){
193 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 189 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
194 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 190 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
@@ -218,17 +214,15 @@ The `usage`, `usageEx` and `usageFull` are functions for printing a small abbrev
218of the help message. 214of the help message.
219 215
220```zig 216```zig
221const std = @import("std");
222const clap = @import("clap"); 217const clap = @import("clap");
218const std = @import("std");
223 219
224pub fn main() !void { 220pub fn main() !void {
225 const stderr = std.io.getStdErr().outStream();
226
227 // clap.usage is a function that can print a simple usage message, given a 221 // clap.usage is a function that can print a simple usage message, given a
228 // slice of Param(Help). There is also a usageEx, which can print a 222 // slice of Param(Help). There is also a usageEx, which can print a
229 // usage message for any Param, but it is more verbose to call. 223 // usage message for any Param, but it is more verbose to call.
230 try clap.usage( 224 try clap.usage(
231 stderr, 225 std.io.getStdErr().writer(),
232 comptime &[_]clap.Param(clap.Help){ 226 comptime &[_]clap.Param(clap.Help){
233 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 227 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
234 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 228 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,