summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2018-11-15 12:53:46 +0100
committerGravatar Jimmi Holst Christensen2018-11-15 12:53:46 +0100
commit1a219fc680faa79f1236912b14fd58754313df87 (patch)
tree94ee1aee0dd89f0a40d1ab04156dd3dcb7965115 /example
parentZig fmt (diff)
downloadzig-clap-1a219fc680faa79f1236912b14fd58754313df87.tar.gz
zig-clap-1a219fc680faa79f1236912b14fd58754313df87.tar.xz
zig-clap-1a219fc680faa79f1236912b14fd58754313df87.zip
Added help function
Diffstat (limited to 'example')
-rw-r--r--example/comptime-clap.zig35
-rw-r--r--example/streaming-clap.zig10
2 files changed, 19 insertions, 26 deletions
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig
index 3b7b42b..b275dc7 100644
--- a/example/comptime-clap.zig
+++ b/example/comptime-clap.zig
@@ -4,25 +4,25 @@ const clap = @import("clap");
4const debug = std.debug; 4const debug = std.debug;
5 5
6pub fn main() !void { 6pub fn main() !void {
7 const stdout_file = try std.io.getStdOut();
8 var stdout_out_stream = stdout_file.outStream();
9 const stdout = &stdout_out_stream.stream;
10
7 var direct_allocator = std.heap.DirectAllocator.init(); 11 var direct_allocator = std.heap.DirectAllocator.init();
8 const allocator = &direct_allocator.allocator; 12 const allocator = &direct_allocator.allocator;
9 defer direct_allocator.deinit(); 13 defer direct_allocator.deinit();
10 14
11 // First we specify what parameters our program can take. 15 // First we specify what parameters our program can take.
12 const params = comptime []clap.Param(void){ 16 const params = comptime []clap.Param([]const u8){
13 // Param.init takes 3 arguments. 17 clap.Param([]const u8).flag(
14 // * An "id", which can be any type specified by the argument to Param. The 18 "Display this help and exit.",
15 // ComptimeClap expects clap.Param(void) only. 19 clap.Names.prefix("help")
16 // * A bool which determins wether the parameter takes a value. 20 ),
17 // * A "Names" struct, which determins what names the parameter will have on the 21 clap.Param([]const u8).option(
18 // commandline. Names.prefix inits a "Names" struct that has the "short" name 22 "An option parameter, which takes a value.",
19 // set to the first letter, and the "long" name set to the full name. 23 clap.Names.prefix("number"),
20 clap.Param(void).flag({}, clap.Names.prefix("help")), 24 ),
21 clap.Param(void).option({}, clap.Names.prefix("number")), 25 clap.Param([]const u8).positional(""),
22
23 // Names.positional returns a "Names" struct where neither the "short" or "long"
24 // name is set.
25 clap.Param(void).positional({}),
26 }; 26 };
27 27
28 // We then initialize an argument iterator. We will use the OsIterator as it nicely 28 // We then initialize an argument iterator. We will use the OsIterator as it nicely
@@ -35,11 +35,14 @@ pub fn main() !void {
35 const exe = try iter.next(); 35 const exe = try iter.next();
36 36
37 // Finally we can parse the arguments 37 // Finally we can parse the arguments
38 var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator.Error, iter); 38 var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator.Error, iter);
39 defer args.deinit(); 39 defer args.deinit();
40 40
41 // clap.help is a function that can print a simple help message, given a
42 // slice of Param([]const u8). There is also a helpEx, which can print a
43 // help message for any Param, but it is more verbose to call.
41 if (args.flag("--help")) 44 if (args.flag("--help"))
42 debug.warn("Help!\n"); 45 return try clap.help(stdout, params);
43 if (args.option("--number")) |n| 46 if (args.option("--number")) |n|
44 debug.warn("--number = {}\n", n); 47 debug.warn("--number = {}\n", n);
45 for (args.positionals()) |pos| 48 for (args.positionals()) |pos|
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index 013c1d4..57ebe71 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -10,18 +10,8 @@ pub fn main() !void {
10 10
11 // First we specify what parameters our program can take. 11 // First we specify what parameters our program can take.
12 const params = []clap.Param(u8){ 12 const params = []clap.Param(u8){
13 // Param.init takes 3 arguments.
14 // * An "id", which can be any type specified by the argument to Param. Here, we
15 // use a "u8" as the "id" type.
16 // * A bool which determins wether the parameter takes a value.
17 // * A "Names" struct, which determins what names the parameter will have on the
18 // commandline. Names.prefix inits a "Names" struct that has the "short" name
19 // set to the first letter, and the "long" name set to the full name.
20 clap.Param(u8).flag('h', clap.Names.prefix("help")), 13 clap.Param(u8).flag('h', clap.Names.prefix("help")),
21 clap.Param(u8).option('n', clap.Names.prefix("number")), 14 clap.Param(u8).option('n', clap.Names.prefix("number")),
22
23 // Names.positional returns a "Names" struct where neither the "short" or "long"
24 // name is set.
25 clap.Param(u8).positional('f'), 15 clap.Param(u8).positional('f'),
26 }; 16 };
27 17