diff options
Diffstat (limited to 'example')
| -rw-r--r-- | example/README.md.template | 4 | ||||
| -rw-r--r-- | example/comptime-clap.zig | 10 | ||||
| -rw-r--r-- | example/help.zig | 7 | ||||
| -rw-r--r-- | example/simple-error.zig | 2 | ||||
| -rw-r--r-- | example/simple.zig | 8 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 10 | ||||
| -rw-r--r-- | example/usage.zig | 6 |
7 files changed, 24 insertions, 23 deletions
diff --git a/example/README.md.template b/example/README.md.template index 5e75d5d..2afbe86 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -2,6 +2,10 @@ | |||
| 2 | 2 | ||
| 3 | A simple and easy to use command line argument parser library for Zig. | 3 | A simple and easy to use command line argument parser library for Zig. |
| 4 | 4 | ||
| 5 | Looking for a version that works with `zig master`? The `zig-master` branch has | ||
| 6 | you covered. It is maintained by people who live at head (not me) and is merged | ||
| 7 | into master on every `zig` release. | ||
| 8 | |||
| 5 | ## Features | 9 | ## Features |
| 6 | 10 | ||
| 7 | * Short arguments `-a` | 11 | * Short arguments `-a` |
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index 4d0ace3..f5c0221 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig | |||
| @@ -4,7 +4,7 @@ const clap = @import("clap"); | |||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | 5 | ||
| 6 | pub fn main() !void { | 6 | pub fn main() !void { |
| 7 | const allocator = std.heap.direct_allocator; | 7 | const allocator = std.heap.page_allocator; |
| 8 | 8 | ||
| 9 | // First we specify what parameters our program can take. | 9 | // First we specify what parameters our program can take. |
| 10 | // We can use `parseParam` to parse a string to a `Param(Help)` | 10 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| @@ -22,13 +22,13 @@ pub fn main() !void { | |||
| 22 | defer iter.deinit(); | 22 | defer iter.deinit(); |
| 23 | 23 | ||
| 24 | // Parse the arguments | 24 | // Parse the arguments |
| 25 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); | 25 | var args = try clap.ComptimeClap(clap.Help, ¶ms).parse(allocator, clap.args.OsIterator, &iter); |
| 26 | defer args.deinit(); | 26 | defer args.deinit(); |
| 27 | 27 | ||
| 28 | if (args.flag("--help")) | 28 | if (args.flag("--help")) |
| 29 | debug.warn("--help\n"); | 29 | debug.warn("--help\n", .{}); |
| 30 | if (args.option("--number")) |n| | 30 | if (args.option("--number")) |n| |
| 31 | debug.warn("--number = {}\n", n); | 31 | debug.warn("--number = {}\n", .{ n }); |
| 32 | for (args.positionals()) |pos| | 32 | for (args.positionals()) |pos| |
| 33 | debug.warn("{}\n", pos); | 33 | debug.warn("{}\n", .{ pos }); |
| 34 | } | 34 | } |
diff --git a/example/help.zig b/example/help.zig index de8a55a..2775177 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -2,16 +2,15 @@ const std = @import("std"); | |||
| 2 | const clap = @import("clap"); | 2 | const clap = @import("clap"); |
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const stderr_file = try std.io.getStdErr(); | 5 | const stderr_file = std.io.getStdErr(); |
| 6 | var stderr_out_stream = stderr_file.outStream(); | 6 | var stderr_out_stream = stderr_file.outStream(); |
| 7 | const stderr = &stderr_out_stream.stream; | ||
| 8 | 7 | ||
| 9 | // clap.help is a function that can print a simple help message, given a | 8 | // clap.help is a function that can print a simple help message, given a |
| 10 | // slice of Param(Help). There is also a helpEx, which can print a | 9 | // slice of Param(Help). There is also a helpEx, which can print a |
| 11 | // help message for any Param, but it is more verbose to call. | 10 | // help message for any Param, but it is more verbose to call. |
| 12 | try clap.help( | 11 | try clap.help( |
| 13 | stderr, | 12 | stderr_out_stream, |
| 14 | comptime [_]clap.Param(clap.Help){ | 13 | comptime &[_]clap.Param(clap.Help){ |
| 15 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 14 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 16 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 15 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 17 | }, | 16 | }, |
diff --git a/example/simple-error.zig b/example/simple-error.zig index fc72a03..2c403fc 100644 --- a/example/simple-error.zig +++ b/example/simple-error.zig | |||
| @@ -8,7 +8,7 @@ pub fn main() !void { | |||
| 8 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 8 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | 11 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator); |
| 12 | defer args.deinit(); | 12 | defer args.deinit(); |
| 13 | 13 | ||
| 14 | _ = args.flag("--helps"); | 14 | _ = args.flag("--helps"); |
diff --git a/example/simple.zig b/example/simple.zig index f791447..d546223 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -14,13 +14,13 @@ pub fn main() !void { | |||
| 14 | }, | 14 | }, |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | 17 | var args = try clap.parse(clap.Help, ¶ms, std.heap.page_allocator); |
| 18 | defer args.deinit(); | 18 | defer args.deinit(); |
| 19 | 19 | ||
| 20 | if (args.flag("--help")) | 20 | if (args.flag("--help")) |
| 21 | debug.warn("--help\n"); | 21 | debug.warn("--help\n", .{}); |
| 22 | if (args.option("--number")) |n| | 22 | if (args.option("--number")) |n| |
| 23 | debug.warn("--number = {}\n", n); | 23 | debug.warn("--number = {}\n", .{ n }); |
| 24 | for (args.positionals()) |pos| | 24 | for (args.positionals()) |pos| |
| 25 | debug.warn("{}\n", pos); | 25 | debug.warn("{}\n", .{ pos }); |
| 26 | } | 26 | } |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 0361d46..4cc43d1 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -4,7 +4,7 @@ const clap = @import("clap"); | |||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | 5 | ||
| 6 | pub fn main() !void { | 6 | pub fn main() !void { |
| 7 | const allocator = std.heap.direct_allocator; | 7 | const allocator = std.heap.page_allocator; |
| 8 | 8 | ||
| 9 | // First we specify what parameters our program can take. | 9 | // First we specify what parameters our program can take. |
| 10 | const params = [_]clap.Param(u8){ | 10 | const params = [_]clap.Param(u8){ |
| @@ -30,7 +30,7 @@ pub fn main() !void { | |||
| 30 | 30 | ||
| 31 | // Initialize our streaming parser. | 31 | // Initialize our streaming parser. |
| 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 33 | .params = params, | 33 | .params = ¶ms, |
| 34 | .iter = &iter, | 34 | .iter = &iter, |
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| @@ -38,13 +38,13 @@ pub fn main() !void { | |||
| 38 | while (try parser.next()) |arg| { | 38 | while (try parser.next()) |arg| { |
| 39 | // arg.param will point to the parameter which matched the argument. | 39 | // arg.param will point to the parameter which matched the argument. |
| 40 | switch (arg.param.id) { | 40 | switch (arg.param.id) { |
| 41 | 'h' => debug.warn("Help!\n"), | 41 | 'h' => debug.warn("Help!\n", .{}), |
| 42 | 'n' => debug.warn("--number = {}\n", arg.value.?), | 42 | 'n' => debug.warn("--number = {}\n", .{ arg.value.? }), |
| 43 | 43 | ||
| 44 | // arg.value == null, if arg.param.takes_value == false. | 44 | // arg.value == null, if arg.param.takes_value == false. |
| 45 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" | 45 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" |
| 46 | // or "-a 10". | 46 | // or "-a 10". |
| 47 | 'f' => debug.warn("{}\n", arg.value.?), | 47 | 'f' => debug.warn("{}\n", .{ arg.value.? }), |
| 48 | else => unreachable, | 48 | else => unreachable, |
| 49 | } | 49 | } |
| 50 | } | 50 | } |
diff --git a/example/usage.zig b/example/usage.zig index 734d741..25e1a34 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -2,16 +2,14 @@ const std = @import("std"); | |||
| 2 | const clap = @import("clap"); | 2 | const clap = @import("clap"); |
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const stderr_file = try std.io.getStdErr(); | 5 | const stderr = std.io.getStdErr().outStream(); |
| 6 | var stderr_out_stream = stderr_file.outStream(); | ||
| 7 | const stderr = &stderr_out_stream.stream; | ||
| 8 | 6 | ||
| 9 | // clap.usage is a function that can print a simple usage message, given a | 7 | // clap.usage is a function that can print a simple usage message, given a |
| 10 | // slice of Param(Help). There is also a usageEx, which can print a | 8 | // slice of Param(Help). There is also a usageEx, which can print a |
| 11 | // usage message for any Param, but it is more verbose to call. | 9 | // usage message for any Param, but it is more verbose to call. |
| 12 | try clap.usage( | 10 | try clap.usage( |
| 13 | stderr, | 11 | stderr, |
| 14 | comptime [_]clap.Param(clap.Help){ | 12 | comptime &[_]clap.Param(clap.Help){ |
| 15 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 13 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 16 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 14 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 17 | clap.parseParam(" --value <N> Output version information and exit.") catch unreachable, | 15 | clap.parseParam(" --value <N> Output version information and exit.") catch unreachable, |