summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2019-11-27 23:13:31 +0900
committerGravatar Jimmi Holst Christensen2019-11-27 23:13:31 +0900
commita305e818bd986c599fff17141617bc4f890276cf (patch)
treeff0a5c133c271d83de3e71ebc023ffc08bb1e3c9 /example
parentBreaking: OsIterator will now get the exe on init (diff)
downloadzig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.gz
zig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.xz
zig-clap-a305e818bd986c599fff17141617bc4f890276cf.zip
Add clap.parse as the simplest way of using the lib
Diffstat (limited to 'example')
-rw-r--r--example/README.md.template50
-rw-r--r--example/comptime-clap-error.zig19
-rw-r--r--example/simple-error.zig15
-rw-r--r--example/simple.zig26
4 files changed, 71 insertions, 39 deletions
diff --git a/example/README.md.template b/example/README.md.template
index 373a037..1fd90b0 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -15,45 +15,55 @@ A simple and easy to use command line argument parser library for Zig.
15 15
16## Examples 16## Examples
17 17
18### `StreamingClap` 18### `clap.parse`
19 19
20The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 20The simplest way to use this library is to just call the `clap.parse` function.
21`args.Iterator` to provide it with arguments lazily.
22 21
23```zig 22```zig
24{} 23{}
25``` 24```
26 25
27### `ComptimeClap` 26The data structure returned has lookup speed on par with array access (`arr[i]`) and validates
28 27that the strings you pass to `option` and `flag` are actually parameters that the program can take:
29The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes
30them available through three functions (`flag`, `option`, `positionals`).
31
32```zig
33{}
34```
35
36The data structure returned from this parser has lookup speed on par with array access (`arr[i]`)
37and validates that the strings you pass to `option` and `flag` are actually parameters that the
38program can take:
39 28
40```zig 29```zig
41{} 30{}
42``` 31```
43 32
44``` 33```
45zig-clap/src/comptime.zig:109:17: error: --helps is not a parameter. 34zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter.
46 @compileError(name ++ " is not a parameter."); 35 @compileError(name ++ " is not a parameter.");
47 ^ 36 ^
48zig-clap/src/comptime.zig:77:45: note: called from here 37zig-clap/clap/comptime.zig:77:45: note: called from here
49 const param = comptime findParam(name); 38 const param = comptime findParam(name);
50 ^ 39 ^
51zig-clap/example/comptime-clap-error.zig:18:18: note: called from here 40zig-clap/clap.zig:238:31: note: called from here
41 return a.clap.flag(name);
42 ^
43zig-clap/example/simple-error.zig:16:18: note: called from here
52 _ = args.flag("--helps"); 44 _ = args.flag("--helps");
53 ^
54``` 45```
55 46
56Ofc, this limits you to parameters that are comptime known. 47### `ComptimeClap`
48
49The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument
50iterator.
51
52```zig
53{}
54```
55
56### `StreamingClap`
57
58The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
59`args.Iterator` to provide it with arguments lazily.
60
61```zig
62{}
63```
64
65Currently, this parse is the only parser that allow an array of `Param` that
66is generated at runtime.
57 67
58### `help` 68### `help`
59 69
diff --git a/example/comptime-clap-error.zig b/example/comptime-clap-error.zig
deleted file mode 100644
index 3209b74..0000000
--- a/example/comptime-clap-error.zig
+++ /dev/null
@@ -1,19 +0,0 @@
1const std = @import("std");
2const clap = @import("clap");
3
4pub fn main() !void {
5 const allocator = std.heap.direct_allocator;
6
7 const params = [_]clap.Param(void){clap.Param(void){
8 .names = clap.Names{ .short = 'h', .long = "help" },
9 }};
10
11 var iter = clap.args.OsIterator.init(allocator);
12 defer iter.deinit();
13 const exe = try iter.next();
14
15 var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter);
16 defer args.deinit();
17
18 _ = args.flag("--helps");
19}
diff --git a/example/simple-error.zig b/example/simple-error.zig
new file mode 100644
index 0000000..fc72a03
--- /dev/null
+++ b/example/simple-error.zig
@@ -0,0 +1,15 @@
1const std = @import("std");
2const clap = @import("clap");
3
4pub fn main() !void {
5 // First we specify what parameters our program can take.
6 // We can use `parseParam` to parse a string to a `Param(Help)`
7 const params = comptime [_]clap.Param(clap.Help){
8 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
9 };
10
11 var args = try clap.parse(clap.Help, params, std.heap.direct_allocator);
12 defer args.deinit();
13
14 _ = args.flag("--helps");
15}
diff --git a/example/simple.zig b/example/simple.zig
new file mode 100644
index 0000000..f791447
--- /dev/null
+++ b/example/simple.zig
@@ -0,0 +1,26 @@
1const std = @import("std");
2const clap = @import("clap");
3
4const debug = std.debug;
5
6pub fn main() !void {
7 // First we specify what parameters our program can take.
8 // We can use `parseParam` to parse a string to a `Param(Help)`
9 const params = comptime [_]clap.Param(clap.Help){
10 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
11 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
12 clap.Param(clap.Help){
13 .takes_value = true,
14 },
15 };
16
17 var args = try clap.parse(clap.Help, params, std.heap.direct_allocator);
18 defer args.deinit();
19
20 if (args.flag("--help"))
21 debug.warn("--help\n");
22 if (args.option("--number")) |n|
23 debug.warn("--number = {}\n", n);
24 for (args.positionals()) |pos|
25 debug.warn("{}\n", pos);
26}