summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/main.yml4
-rw-r--r--README.md38
-rw-r--r--clap/streaming.zig6
-rw-r--r--example/README.md.template2
-rw-r--r--example/help.zig17
-rw-r--r--example/usage.zig19
6 files changed, 51 insertions, 35 deletions
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 40fe612..08d791f 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -7,7 +7,7 @@ jobs:
7 test: 7 test:
8 runs-on: ubuntu-latest 8 runs-on: ubuntu-latest
9 steps: 9 steps:
10 - uses: actions/checkout@v2.3.4 10 - uses: actions/checkout@v2.4.0
11 with: 11 with:
12 submodules: recursive 12 submodules: recursive
13 - uses: goto-bus-stop/setup-zig@v1.3.0 13 - uses: goto-bus-stop/setup-zig@v1.3.0
@@ -17,7 +17,7 @@ jobs:
17 lint: 17 lint:
18 runs-on: ubuntu-latest 18 runs-on: ubuntu-latest
19 steps: 19 steps:
20 - uses: actions/checkout@v2.3.4 20 - uses: actions/checkout@v2.4.0
21 - uses: goto-bus-stop/setup-zig@v1.3.0 21 - uses: goto-bus-stop/setup-zig@v1.3.0
22 with: 22 with:
23 version: master 23 version: master
diff --git a/README.md b/README.md
index 197eb4c..d6ad7a0 100644
--- a/README.md
+++ b/README.md
@@ -180,21 +180,25 @@ const clap = @import("clap");
180const std = @import("std"); 180const std = @import("std");
181 181
182pub fn main() !void { 182pub fn main() !void {
183 const params = comptime [_]clap.Param(clap.Help){
184 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
185 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
186 };
187
188 var args = try clap.parse(clap.Help, &params, .{});
189 defer args.deinit();
190
183 // clap.help is a function that can print a simple help message, given a 191 // clap.help is a function that can print a simple help message, given a
184 // slice of Param(Help). There is also a helpEx, which can print a 192 // slice of Param(Help). There is also a helpEx, which can print a
185 // help message for any Param, but it is more verbose to call. 193 // help message for any Param, but it is more verbose to call.
186 try clap.help( 194 if (args.flag("--help"))
187 std.io.getStdErr().writer(), 195 return clap.help(std.io.getStdErr().writer(), &params);
188 comptime &.{
189 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
190 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
191 },
192 );
193} 196}
194 197
195``` 198```
196 199
197``` 200```
201$ zig-out/bin/help --help
198 -h, --help Display this help and exit. 202 -h, --help Display this help and exit.
199 -v, --version Output version information and exit. 203 -v, --version Output version information and exit.
200``` 204```
@@ -218,22 +222,26 @@ const clap = @import("clap");
218const std = @import("std"); 222const std = @import("std");
219 223
220pub fn main() !void { 224pub fn main() !void {
225 const params = comptime [_]clap.Param(clap.Help){
226 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
227 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable,
228 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
229 };
230
231 var args = try clap.parse(clap.Help, &params, .{});
232 defer args.deinit();
233
221 // clap.usage is a function that can print a simple usage message, given a 234 // clap.usage is a function that can print a simple usage message, given a
222 // slice of Param(Help). There is also a usageEx, which can print a 235 // slice of Param(Help). There is also a usageEx, which can print a
223 // usage message for any Param, but it is more verbose to call. 236 // usage message for any Param, but it is more verbose to call.
224 try clap.usage( 237 if (args.flag("--help"))
225 std.io.getStdErr().writer(), 238 return clap.usage(std.io.getStdErr().writer(), &params);
226 comptime &.{
227 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
228 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable,
229 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
230 },
231 );
232} 239}
233 240
234``` 241```
235 242
236``` 243```
244$ zig-out/bin/usage --help
237[-hv] [--value <N>] 245[-hv] [--value <N>]
238``` 246```
239 247
diff --git a/clap/streaming.zig b/clap/streaming.zig
index 885c581..3f24aaa 100644
--- a/clap/streaming.zig
+++ b/clap/streaming.zig
@@ -46,7 +46,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
46 pub fn next(parser: *@This()) !?Arg(Id) { 46 pub fn next(parser: *@This()) !?Arg(Id) {
47 switch (parser.state) { 47 switch (parser.state) {
48 .normal => return try parser.normal(), 48 .normal => return try parser.normal(),
49 .chaining => |state| return try parser.chainging(state), 49 .chaining => |state| return try parser.chaining(state),
50 .rest_are_positional => { 50 .rest_are_positional => {
51 const param = parser.positionalParam() orelse unreachable; 51 const param = parser.positionalParam() orelse unreachable;
52 const value = (try parser.iter.next()) orelse return null; 52 const value = (try parser.iter.next()) orelse return null;
@@ -89,7 +89,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
89 89
90 return parser.err(arg, .{ .long = name }, error.InvalidArgument); 90 return parser.err(arg, .{ .long = name }, error.InvalidArgument);
91 }, 91 },
92 .short => return try parser.chainging(.{ 92 .short => return try parser.chaining(.{
93 .arg = arg, 93 .arg = arg,
94 .index = 0, 94 .index = 0,
95 }), 95 }),
@@ -110,7 +110,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
110 } 110 }
111 } 111 }
112 112
113 fn chainging(parser: *@This(), state: State.Chaining) !?Arg(Id) { 113 fn chaining(parser: *@This(), state: State.Chaining) !?Arg(Id) {
114 const arg = state.arg; 114 const arg = state.arg;
115 const index = state.index; 115 const index = state.index;
116 const next_index = index + 1; 116 const next_index = index + 1;
diff --git a/example/README.md.template b/example/README.md.template
index 74a2f1d..1848d03 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -74,6 +74,7 @@ program can take.
74``` 74```
75 75
76``` 76```
77$ zig-out/bin/help --help
77 -h, --help Display this help and exit. 78 -h, --help Display this help and exit.
78 -v, --version Output version information and exit. 79 -v, --version Output version information and exit.
79``` 80```
@@ -97,6 +98,7 @@ of the help message.
97``` 98```
98 99
99``` 100```
101$ zig-out/bin/usage --help
100[-hv] [--value <N>] 102[-hv] [--value <N>]
101``` 103```
102 104
diff --git a/example/help.zig b/example/help.zig
index d90373a..de3b707 100644
--- a/example/help.zig
+++ b/example/help.zig
@@ -2,14 +2,17 @@ const clap = @import("clap");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub fn main() !void {
5 const params = comptime [_]clap.Param(clap.Help){
6 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
7 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
8 };
9
10 var args = try clap.parse(clap.Help, &params, .{});
11 defer args.deinit();
12
5 // clap.help is a function that can print a simple help message, given a 13 // clap.help is a function that can print a simple help message, given a
6 // slice of Param(Help). There is also a helpEx, which can print a 14 // slice of Param(Help). There is also a helpEx, which can print a
7 // help message for any Param, but it is more verbose to call. 15 // help message for any Param, but it is more verbose to call.
8 try clap.help( 16 if (args.flag("--help"))
9 std.io.getStdErr().writer(), 17 return clap.help(std.io.getStdErr().writer(), &params);
10 comptime &.{
11 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
12 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
13 },
14 );
15} 18}
diff --git a/example/usage.zig b/example/usage.zig
index 90fa310..368a6b3 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -2,15 +2,18 @@ const clap = @import("clap");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub fn main() !void {
5 const params = comptime [_]clap.Param(clap.Help){
6 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
7 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable,
8 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
9 };
10
11 var args = try clap.parse(clap.Help, &params, .{});
12 defer args.deinit();
13
5 // clap.usage is a function that can print a simple usage message, given a 14 // clap.usage is a function that can print a simple usage message, given a
6 // slice of Param(Help). There is also a usageEx, which can print a 15 // slice of Param(Help). There is also a usageEx, which can print a
7 // usage message for any Param, but it is more verbose to call. 16 // usage message for any Param, but it is more verbose to call.
8 try clap.usage( 17 if (args.flag("--help"))
9 std.io.getStdErr().writer(), 18 return clap.usage(std.io.getStdErr().writer(), &params);
10 comptime &.{
11 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
12 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable,
13 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
14 },
15 );
16} 19}