diff options
| author | 2021-05-26 20:46:23 +0200 | |
|---|---|---|
| committer | 2021-05-26 20:46:23 +0200 | |
| commit | bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02 (patch) | |
| tree | a545acd4583d4d344d1e235c445b9b9b5060514b /example | |
| parent | Merge branch 'master' into zig-master (diff) | |
| parent | Modernize codebase (diff) | |
| download | zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.tar.gz zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.tar.xz zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.zip | |
Merge branch 'master' into zig-master
Diffstat (limited to 'example')
| -rw-r--r-- | example/help.zig | 7 | ||||
| -rw-r--r-- | example/simple-error.zig | 4 | ||||
| -rw-r--r-- | example/simple-ex.zig | 19 | ||||
| -rw-r--r-- | example/simple.zig | 14 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 35 | ||||
| -rw-r--r-- | example/usage.zig | 6 |
6 files changed, 44 insertions, 41 deletions
diff --git a/example/help.zig b/example/help.zig index c210ff9..3cf9e42 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -1,15 +1,12 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const stderr_file = std.io.getStdErr(); | ||
| 6 | var stderr_out_stream = stderr_file.writer(); | ||
| 7 | |||
| 8 | // clap.help is a function that can print a simple help message, given a | 5 | // clap.help is a function that can print a simple help message, given a |
| 9 | // slice of Param(Help). There is also a helpEx, which can print a | 6 | // slice of Param(Help). There is also a helpEx, which can print a |
| 10 | // help message for any Param, but it is more verbose to call. | 7 | // help message for any Param, but it is more verbose to call. |
| 11 | try clap.help( | 8 | try clap.help( |
| 12 | stderr_out_stream, | 9 | std.io.getStdErr().writer(), |
| 13 | comptime &[_]clap.Param(clap.Help){ | 10 | comptime &[_]clap.Param(clap.Help){ |
| 14 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 11 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 15 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 12 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
diff --git a/example/simple-error.zig b/example/simple-error.zig index 3c62f0e..c04a9c6 100644 --- a/example/simple-error.zig +++ b/example/simple-error.zig | |||
| @@ -1,12 +1,12 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const params = comptime [_]clap.Param(clap.Help){ | 5 | const params = comptime [_]clap.Param(clap.Help){ |
| 6 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 6 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| 9 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); | 9 | var args = try clap.parse(clap.Help, ¶ms, .{}); |
| 10 | defer args.deinit(); | 10 | defer args.deinit(); |
| 11 | 11 | ||
| 12 | _ = args.flag("--helps"); | 12 | _ = args.flag("--helps"); |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index b890860..88598aa 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -1,7 +1,8 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | const io = std.io; | ||
| 5 | 6 | ||
| 6 | pub fn main() !void { | 7 | pub fn main() !void { |
| 7 | const allocator = std.heap.page_allocator; | 8 | const allocator = std.heap.page_allocator; |
| @@ -21,13 +22,19 @@ pub fn main() !void { | |||
| 21 | defer iter.deinit(); | 22 | defer iter.deinit(); |
| 22 | 23 | ||
| 23 | // Initalize our diagnostics, which can be used for reporting useful errors. | 24 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 24 | // This is optional. You can also just pass `null` to `parser.next` if you | 25 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 25 | // don't care about the extra information `Diagnostics` provides. | 26 | // care about the extra information `Diagnostics` provides. |
| 26 | var diag: clap.Diagnostic = undefined; | 27 | var diag = clap.Diagnostic{}; |
| 27 | 28 | var args = clap.parseEx(clap.Help, ¶ms, &iter, .{ | |
| 28 | var args = clap.parseEx(clap.Help, ¶ms, allocator, &iter, &diag) catch |err| { | 29 | .allocator = allocator, |
| 30 | .diagnostic = &diag, | ||
| 31 | }) catch |err| { | ||
| 29 | // Report useful error and exit | 32 | // Report useful error and exit |
| 33 | <<<<<<< HEAD | ||
| 30 | diag.report(std.io.getStdErr().writer(), err) catch {}; | 34 | diag.report(std.io.getStdErr().writer(), err) catch {}; |
| 35 | ======= | ||
| 36 | diag.report(io.getStdErr().writer(), err) catch {}; | ||
| 37 | >>>>>>> master | ||
| 31 | return err; | 38 | return err; |
| 32 | }; | 39 | }; |
| 33 | defer args.deinit(); | 40 | defer args.deinit(); |
diff --git a/example/simple.zig b/example/simple.zig index 559bba6..69473fa 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -1,7 +1,8 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | const io = std.io; | ||
| 5 | 6 | ||
| 6 | pub fn main() !void { | 7 | pub fn main() !void { |
| 7 | // First we specify what parameters our program can take. | 8 | // First we specify what parameters our program can take. |
| @@ -14,13 +15,12 @@ pub fn main() !void { | |||
| 14 | }; | 15 | }; |
| 15 | 16 | ||
| 16 | // Initalize our diagnostics, which can be used for reporting useful errors. | 17 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 17 | // This is optional. You can also just pass `null` to `parser.next` if you | 18 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 18 | // don't care about the extra information `Diagnostics` provides. | 19 | // care about the extra information `Diagnostics` provides. |
| 19 | var diag: clap.Diagnostic = undefined; | 20 | var diag = clap.Diagnostic{}; |
| 20 | 21 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | |
| 21 | var args = clap.parse(clap.Help, ¶ms, std.heap.page_allocator, &diag) catch |err| { | ||
| 22 | // Report useful error and exit | 22 | // Report useful error and exit |
| 23 | diag.report(std.io.getStdErr().writer(), err) catch {}; | 23 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 24 | return err; | 24 | return err; |
| 25 | }; | 25 | }; |
| 26 | defer args.deinit(); | 26 | defer args.deinit(); |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 5468fd5..32d44c4 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -1,26 +1,24 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | const io = std.io; | ||
| 5 | 6 | ||
| 6 | pub fn main() !void { | 7 | pub fn main() !void { |
| 7 | const allocator = std.heap.page_allocator; | 8 | const allocator = std.heap.page_allocator; |
| 8 | 9 | ||
| 9 | // First we specify what parameters our program can take. | 10 | // First we specify what parameters our program can take. |
| 10 | const params = [_]clap.Param(u8){ | 11 | const params = [_]clap.Param(u8){ |
| 11 | clap.Param(u8){ | 12 | .{ |
| 12 | .id = 'h', | 13 | .id = 'h', |
| 13 | .names = clap.Names{ .short = 'h', .long = "help" }, | 14 | .names = .{ .short = 'h', .long = "help" }, |
| 14 | }, | 15 | }, |
| 15 | clap.Param(u8){ | 16 | .{ |
| 16 | .id = 'n', | 17 | .id = 'n', |
| 17 | .names = clap.Names{ .short = 'n', .long = "number" }, | 18 | .names = .{ .short = 'n', .long = "number" }, |
| 18 | .takes_value = .One, | 19 | .takes_value = .one, |
| 19 | }, | ||
| 20 | clap.Param(u8){ | ||
| 21 | .id = 'f', | ||
| 22 | .takes_value = .One, | ||
| 23 | }, | 20 | }, |
| 21 | .{ .id = 'f', .takes_value = .one }, | ||
| 24 | }; | 22 | }; |
| 25 | 23 | ||
| 26 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 24 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| @@ -28,21 +26,24 @@ pub fn main() !void { | |||
| 28 | var iter = try clap.args.OsIterator.init(allocator); | 26 | var iter = try clap.args.OsIterator.init(allocator); |
| 29 | defer iter.deinit(); | 27 | defer iter.deinit(); |
| 30 | 28 | ||
| 31 | // Initialize our streaming parser. | 29 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 30 | // This is optional. You can also leave the `diagnostic` field unset if you | ||
| 31 | // don't care about the extra information `Diagnostic` provides. | ||
| 32 | var diag = clap.Diagnostic{}; | ||
| 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 33 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 33 | .params = ¶ms, | 34 | .params = ¶ms, |
| 34 | .iter = &iter, | 35 | .iter = &iter, |
| 36 | .diagnostic = &diag, | ||
| 35 | }; | 37 | }; |
| 36 | 38 | ||
| 37 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 38 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 39 | // don't care about the extra information `Diagnostics` provides. | ||
| 40 | var diag: clap.Diagnostic = undefined; | ||
| 41 | |||
| 42 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 39 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 43 | while (parser.next(&diag) catch |err| { | 40 | while (parser.next() catch |err| { |
| 44 | // Report useful error and exit | 41 | // Report useful error and exit |
| 42 | <<<<<<< HEAD | ||
| 45 | diag.report(std.io.getStdErr().writer(), err) catch {}; | 43 | diag.report(std.io.getStdErr().writer(), err) catch {}; |
| 44 | ======= | ||
| 45 | diag.report(io.getStdErr().writer(), err) catch {}; | ||
| 46 | >>>>>>> master | ||
| 46 | return err; | 47 | return err; |
| 47 | }) |arg| { | 48 | }) |arg| { |
| 48 | // arg.param will point to the parameter which matched the argument. | 49 | // arg.param will point to the parameter which matched the argument. |
diff --git a/example/usage.zig b/example/usage.zig index dc88c48..e044f1d 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -1,14 +1,12 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | const stderr = std.io.getStdErr().writer(); | ||
| 6 | |||
| 7 | // clap.usage is a function that can print a simple usage message, given a | 5 | // clap.usage is a function that can print a simple usage message, given a |
| 8 | // slice of Param(Help). There is also a usageEx, which can print a | 6 | // slice of Param(Help). There is also a usageEx, which can print a |
| 9 | // usage message for any Param, but it is more verbose to call. | 7 | // usage message for any Param, but it is more verbose to call. |
| 10 | try clap.usage( | 8 | try clap.usage( |
| 11 | stderr, | 9 | std.io.getStdErr().writer(), |
| 12 | comptime &[_]clap.Param(clap.Help){ | 10 | comptime &[_]clap.Param(clap.Help){ |
| 13 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 11 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 14 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 12 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |