summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi HC2019-06-21 19:15:32 +0200
committerGravatar Jimmi HC2019-06-21 19:15:32 +0200
commit56e7be2835311888ef43f403e5d6bc2118c953fe (patch)
treea700e966b79ec441f8936c667bd325e37c4a61dc /example
parentupdated to newest version of zig (diff)
downloadzig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.tar.gz
zig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.tar.xz
zig-clap-56e7be2835311888ef43f403e5d6bc2118c953fe.zip
Embed examples in README during build
fixes #11
Diffstat (limited to 'example')
-rw-r--r--example/README.md.template77
-rw-r--r--example/comptime-clap-error.zig21
-rw-r--r--example/comptime-clap.zig7
-rw-r--r--example/help.zig25
4 files changed, 125 insertions, 5 deletions
diff --git a/example/README.md.template b/example/README.md.template
new file mode 100644
index 0000000..88914fb
--- /dev/null
+++ b/example/README.md.template
@@ -0,0 +1,77 @@
1# zig-clap
2
3A simple and easy to use command line argument parser library for Zig.
4
5## Features
6
7* Short arguments `-a`
8 * Chaining `-abc` where `a` and `b` does not take values.
9* Long arguments `--long`
10* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`)
11 * Short args also support passing values with no spacing or `=` (`-a100`)
12 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`)
13
14## Examples
15
16### `StreamingClap`
17
18The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
19`args.Iterator` to provide it with arguments lazily.
20
21```zig
22{}
23```
24
25### `ComptimeClap`
26
27The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes
28them available through three functions (`flag`, `option`, `positionals`).
29
30```zig
31{}
32```
33
34The data structure returned from this parser has lookup speed on par with array access (`arr[i]`)
35and validates that the strings you pass to `option` and `flag` are actually parameters that the
36program can take:
37
38```zig
39{}
40```
41
42```
43zig-clap/src/comptime.zig:116:17: error: --helps is not a parameter.
44 @compileError(name ++ " is not a parameter.");
45 ^
46zig-clap/src/comptime.zig:84:45: note: called from here
47 const param = comptime findParam(name);
48 ^
49zig-clap/example/comptime-clap-error.zig:22:18: note: called from here
50 _ = args.flag("--helps");
51 ^
52```
53
54Ofc, this limits you to parameters that are comptime known.
55
56### `help`
57
58The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the
59program can take.
60
61```zig
62{}
63```
64
65```
66 -h, --help Display this help and exit.
67 -v, --version Output version information and exit.
68```
69
70The `help` function is the simplest to call. It only takes an `OutStream` and a slice of
71`Param([]const u8)`. This function assumes that the id of each parameter is the help message.
72
73The `helpEx` is the generic version of `help`. It can print a help message for any
74`Param` give that the caller provides functions for getting the help and value strings.
75
76The `helpFull` is even more generic, allowing the functions that get the help and value strings
77to return errors and take a context as a parameter.
diff --git a/example/comptime-clap-error.zig b/example/comptime-clap-error.zig
new file mode 100644
index 0000000..93c1af2
--- /dev/null
+++ b/example/comptime-clap-error.zig
@@ -0,0 +1,21 @@
1const std = @import("std");
2const clap = @import("clap");
3
4pub fn main() !void {
5 const params = [_]clap.Param(void){clap.Param(void){
6 .names = clap.Names{ .short = 'h', .long = "help" },
7 }};
8
9 var direct_allocator = std.heap.DirectAllocator.init();
10 const allocator = &direct_allocator.allocator;
11 defer direct_allocator.deinit();
12
13 var iter = clap.args.OsIterator.init(allocator);
14 defer iter.deinit();
15 const exe = try iter.next();
16
17 var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter);
18 defer args.deinit();
19
20 _ = args.flag("--helps");
21}
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig
index 935381f..695fa62 100644
--- a/example/comptime-clap.zig
+++ b/example/comptime-clap.zig
@@ -13,7 +13,7 @@ pub fn main() !void {
13 defer direct_allocator.deinit(); 13 defer direct_allocator.deinit();
14 14
15 // First we specify what parameters our program can take. 15 // First we specify what parameters our program can take.
16 const params = comptime [_]clap.Param([]const u8){ 16 const params = [_]clap.Param([]const u8){
17 clap.Param([]const u8){ 17 clap.Param([]const u8){
18 .id = "Display this help and exit.", 18 .id = "Display this help and exit.",
19 .names = clap.Names{ .short = 'h', .long = "help" }, 19 .names = clap.Names{ .short = 'h', .long = "help" },
@@ -41,11 +41,8 @@ pub fn main() !void {
41 var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter); 41 var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter);
42 defer args.deinit(); 42 defer args.deinit();
43 43
44 // clap.help is a function that can print a simple help message, given a
45 // slice of Param([]const u8). There is also a helpEx, which can print a
46 // help message for any Param, but it is more verbose to call.
47 if (args.flag("--help")) 44 if (args.flag("--help"))
48 return try clap.help(stdout, params); 45 debug.warn("--help\n");
49 if (args.option("--number")) |n| 46 if (args.option("--number")) |n|
50 debug.warn("--number = {}\n", n); 47 debug.warn("--number = {}\n", n);
51 for (args.positionals()) |pos| 48 for (args.positionals()) |pos|
diff --git a/example/help.zig b/example/help.zig
new file mode 100644
index 0000000..35c0258
--- /dev/null
+++ b/example/help.zig
@@ -0,0 +1,25 @@
1const std = @import("std");
2const clap = @import("clap");
3
4pub fn main() !void {
5 const stderr_file = try std.io.getStdErr();
6 var stderr_out_stream = stderr_file.outStream();
7 const stderr = &stderr_out_stream.stream;
8
9 // clap.help is a function that can print a simple help message, given a
10 // slice of Param([]const u8). There is also a helpEx, which can print a
11 // help message for any Param, but it is more verbose to call.
12 try clap.help(
13 stderr,
14 [_]clap.Param([]const u8){
15 clap.Param([]const u8){
16 .id = "Display this help and exit.",
17 .names = clap.Names{ .short = 'h', .long = "help" },
18 },
19 clap.Param([]const u8){
20 .id = "Output version information and exit.",
21 .names = clap.Names{ .short = 'v', .long = "version" },
22 },
23 },
24 );
25}