summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
Diffstat (limited to 'example')
-rw-r--r--example/README.md.template28
-rw-r--r--example/simple-ex.zig2
-rw-r--r--example/simple.zig4
-rw-r--r--example/streaming-clap.zig4
-rw-r--r--example/subcommands.zig16
5 files changed, 26 insertions, 28 deletions
diff --git a/example/README.md.template b/example/README.md.template
index bcd3774..bbf5cb8 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -45,8 +45,8 @@ exe.root_module.addImport("clap", clap.module("clap"));
45 45
46## API Reference 46## API Reference
47 47
48Automatically generated API Reference for the project 48Automatically generated API Reference for the project can be found at https://Hejsil.github.io/
49can be found at https://Hejsil.github.io/zig-clap. 49zig-clap.
50Note that Zig autodoc is in beta; the website may be broken or incomplete. 50Note that Zig autodoc is in beta; the website may be broken or incomplete.
51 51
52## Examples 52## Examples
@@ -59,16 +59,16 @@ The simplest way to use this library is to just call the `clap.parse` function.
59{s} 59{s}
60``` 60```
61 61
62The result will contain an `args` field and a `positionals` field. `args` will have one field 62The result will contain an `args` field and a `positionals` field. `args` will have one field for
63for each none positional parameter of your program. The name of the field will be the longest 63each non-positional parameter of your program. The name of the field will be the longest name of the
64name of the parameter. `positionals` be a tuple with one field for each positional parameter. 64parameter. `positionals` will be a tuple with one field for each positional parameter.
65 65
66The fields in `args` and `postionals` are typed. The type is based on the name of the value the 66The fields in `args` and `postionals` are typed. The type is based on the name of the value the
67parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. 67parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`.
68 68
69Note that this is only the case because `clap.parsers.default` has a field called `usize` which 69Note that this is only the case because `clap.parsers.default` has a field called `usize` which
70contains a parser that returns `usize`. You can pass in something other than 70contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default`
71`clap.parsers.default` if you want some other mapping. 71if you want some other mapping.
72 72
73```zig 73```zig
74{s} 74{s}
@@ -92,15 +92,13 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
92{s} 92{s}
93``` 93```
94 94
95Currently, this parser is the only parser that allows an array of `Param` that 95Currently, this parser is the only parser that allows an array of `Param` that is generated at runtime.
96is generated at runtime.
97 96
98### `help` 97### `help`
99 98
100The `help` prints a simple list of all parameters the program can take. It expects the 99`help` prints a simple list of all parameters the program can take. It expects the `Id` to have a
101`Id` to have a `description` method and an `value` method so that it can provide that 100`description` method and an `value` method so that it can provide that in the output. `HelpOptions`
102in the output. `HelpOptions` is passed to `help` to control how the help message is 101is passed to `help` to control how the help message is printed.
103printed.
104 102
105```zig 103```zig
106{s} 104{s}
@@ -117,8 +115,8 @@ $ zig-out/bin/help --help
117 115
118### `usage` 116### `usage`
119 117
120The `usage` prints a small abbreviated version of the help message. It expects the `Id` 118`usage` prints a small abbreviated version of the help message. It expects the `Id` to have a
121to have a `value` method so it can provide that in the output. 119`value` method so it can provide that in the output.
122 120
123```zig 121```zig
124{s} 122{s}
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index 154e486..22f657f 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -3,7 +3,7 @@ pub fn main() !void {
3 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
4 4
5 // First we specify what parameters our program can take. 5 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
7 const params = comptime clap.parseParamsComptime( 7 const params = comptime clap.parseParamsComptime(
8 \\-h, --help Display this help and exit. 8 \\-h, --help Display this help and exit.
9 \\-n, --number <INT> An option parameter, which takes a value. 9 \\-n, --number <INT> An option parameter, which takes a value.
diff --git a/example/simple.zig b/example/simple.zig
index 74157a6..2b7bf0a 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -3,7 +3,7 @@ pub fn main() !void {
3 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
4 4
5 // First we specify what parameters our program can take. 5 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
7 const params = comptime clap.parseParamsComptime( 7 const params = comptime clap.parseParamsComptime(
8 \\-h, --help Display this help and exit. 8 \\-h, --help Display this help and exit.
9 \\-n, --number <usize> An option parameter, which takes a value. 9 \\-n, --number <usize> An option parameter, which takes a value.
@@ -20,7 +20,7 @@ pub fn main() !void {
20 .diagnostic = &diag, 20 .diagnostic = &diag,
21 .allocator = gpa.allocator(), 21 .allocator = gpa.allocator(),
22 }) catch |err| { 22 }) catch |err| {
23 // Report useful error and exit 23 // Report useful error and exit.
24 diag.report(std.io.getStdErr().writer(), err) catch {}; 24 diag.report(std.io.getStdErr().writer(), err) catch {};
25 return err; 25 return err;
26 }; 26 };
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index 2a20bcb..054c401 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -18,7 +18,7 @@ pub fn main() !void {
18 var iter = try std.process.ArgIterator.initWithAllocator(allocator); 18 var iter = try std.process.ArgIterator.initWithAllocator(allocator);
19 defer iter.deinit(); 19 defer iter.deinit();
20 20
21 // Skip exe argument 21 // Skip exe argument.
22 _ = iter.next(); 22 _ = iter.next();
23 23
24 // Initialize our diagnostics, which can be used for reporting useful errors. 24 // Initialize our diagnostics, which can be used for reporting useful errors.
@@ -33,7 +33,7 @@ pub fn main() !void {
33 33
34 // Because we use a streaming parser, we have to consume each argument parsed individually. 34 // Because we use a streaming parser, we have to consume each argument parsed individually.
35 while (parser.next() catch |err| { 35 while (parser.next() catch |err| {
36 // Report useful error and exit 36 // Report useful error and exit.
37 diag.report(std.io.getStdErr().writer(), err) catch {}; 37 diag.report(std.io.getStdErr().writer(), err) catch {};
38 return err; 38 return err;
39 }) |arg| { 39 }) |arg| {
diff --git a/example/subcommands.zig b/example/subcommands.zig
index fcd4fee..8223f31 100644
--- a/example/subcommands.zig
+++ b/example/subcommands.zig
@@ -1,4 +1,4 @@
1// These are our subcommands 1// These are our subcommands.
2const SubCommands = enum { 2const SubCommands = enum {
3 help, 3 help,
4 math, 4 math,
@@ -8,7 +8,7 @@ const main_parsers = .{
8 .command = clap.parsers.enumeration(SubCommands), 8 .command = clap.parsers.enumeration(SubCommands),
9}; 9};
10 10
11// The parameters for main. Parameters for the subcommands are specified further down 11// The parameters for `main`. Parameters for the subcommands are specified further down.
12const main_params = clap.parseParamsComptime( 12const main_params = clap.parseParamsComptime(
13 \\-h, --help Display this help and exit. 13 \\-h, --help Display this help and exit.
14 \\<command> 14 \\<command>
@@ -16,7 +16,7 @@ const main_params = clap.parseParamsComptime(
16); 16);
17 17
18// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to 18// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to
19// get the return type of `clap.parse` and `clap.parseEx` 19// get the return type of `clap.parse` and `clap.parseEx`.
20const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); 20const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
21 21
22pub fn main() !void { 22pub fn main() !void {
@@ -38,7 +38,7 @@ pub fn main() !void {
38 // here because parsed positionals are, like slices and arrays, indexed starting at 0). 38 // here because parsed positionals are, like slices and arrays, indexed starting at 0).
39 // 39 //
40 // This will terminate the parsing after parsing the subcommand enum and leave `iter` 40 // This will terminate the parsing after parsing the subcommand enum and leave `iter`
41 // not fully consumed. It can then be reused to parse the arguments for subcommands 41 // not fully consumed. It can then be reused to parse the arguments for subcommands.
42 .terminating_positional = 0, 42 .terminating_positional = 0,
43 }) catch |err| { 43 }) catch |err| {
44 diag.report(std.io.getStdErr().writer(), err) catch {}; 44 diag.report(std.io.getStdErr().writer(), err) catch {};
@@ -57,11 +57,11 @@ pub fn main() !void {
57} 57}
58 58
59fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { 59fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
60 // The parent arguments are not used it, but there are cases where it might be useful, so 60 // The parent arguments are not used here, but there are cases where it might be useful, so
61 // the example shows how to pass the arguments around. 61 // this example shows how to pass the arguments around.
62 _ = main_args; 62 _ = main_args;
63 63
64 // The parameters for the subcommand 64 // The parameters for the subcommand.
65 const params = comptime clap.parseParamsComptime( 65 const params = comptime clap.parseParamsComptime(
66 \\-h, --help Display this help and exit. 66 \\-h, --help Display this help and exit.
67 \\-a, --add Add the two numbers 67 \\-a, --add Add the two numbers
@@ -71,7 +71,7 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
71 \\ 71 \\
72 ); 72 );
73 73
74 // Here we pass the partially parsed argument iterator 74 // Here we pass the partially parsed argument iterator.
75 var diag = clap.Diagnostic{}; 75 var diag = clap.Diagnostic{};
76 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{ 76 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{
77 .diagnostic = &diag, 77 .diagnostic = &diag,