From 56e7be2835311888ef43f403e5d6bc2118c953fe Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Fri, 21 Jun 2019 19:15:32 +0200 Subject: Embed examples in README during build fixes #11 --- src/comptime.zig | 19 +++++++++++++------ src/streaming.zig | 30 ++++++++++++++---------------- 2 files changed, 27 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/comptime.zig b/src/comptime.zig index b585598..d872b82 100644 --- a/src/comptime.zig +++ b/src/comptime.zig @@ -4,6 +4,7 @@ const std = @import("std"); const testing = std.testing; const heap = std.heap; const mem = std.mem; +const debug = std.debug; pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { var flags: usize = 0; @@ -56,11 +57,17 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) if (param.names.long == null and param.names.short == null) { try pos.append(arg.value.?); } else if (param.takes_value) { - // We slice before access to avoid false positive access out of bound - // compile error. - res.options[0..][param.id] = arg.value.?; + // If we don't have any optional parameters, then this code should + // never be reached. + debug.assert(res.options.len != 0); + + // Hack: Utilize Zigs lazy analyzis to avoid a compiler error + if (res.options.len != 0) + res.options[param.id] = arg.value.?; } else { - res.flags[0..][param.id] = true; + debug.assert(res.flags.len != 0); + if (res.flags.len != 0) + res.flags[param.id] = true; } } @@ -118,13 +125,13 @@ test "clap.comptime.ComptimeClap" { .names = clap.Names{ .short = 'a', .long = "aa", - } + }, }, clap.Param(void){ .names = clap.Names{ .short = 'b', .long = "bb", - } + }, }, clap.Param(void){ .names = clap.Names{ diff --git a/src/streaming.zig b/src/streaming.zig index 9da120c..d23471c 100644 --- a/src/streaming.zig +++ b/src/streaming.zig @@ -174,7 +174,7 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r var iter = args.SliceIterator{ .args = args_strings }; var c = StreamingClap(u8, args.SliceIterator){ .params = params, - .iter = &iter + .iter = &iter, }; for (results) |res| { @@ -217,9 +217,9 @@ test "clap.streaming.StreamingClap: short params" { testNoErr( params, [_][]const u8{ - "-a", "-b", "-ab", "-ba", - "-c", "0", "-c=0", "-ac", - "0", "-ac=0", + "-a", "-b", "-ab", "-ba", + "-c", "0", "-c=0", "-ac", + "0", "-ac=0", }, [_]Arg(u8){ Arg(u8){ .param = a }, @@ -262,8 +262,8 @@ test "clap.streaming.StreamingClap: long params" { testNoErr( params, [_][]const u8{ - "--aa", "--bb", - "--cc", "0", + "--aa", "--bb", + "--cc", "0", "--cc=0", }, [_]Arg(u8){ @@ -276,12 +276,10 @@ test "clap.streaming.StreamingClap: long params" { } test "clap.streaming.StreamingClap: positional params" { - const params = [_]clap.Param(u8){ - clap.Param(u8){ - .id = 0, - .takes_value = true, - }, - }; + const params = [_]clap.Param(u8){clap.Param(u8){ + .id = 0, + .takes_value = true, + }}; testNoErr( params, @@ -331,10 +329,10 @@ test "clap.streaming.StreamingClap: all params" { testNoErr( params, [_][]const u8{ - "-a", "-b", "-ab", "-ba", - "-c", "0", "-c=0", "-ac", - "0", "-ac=0", "--aa", "--bb", - "--cc", "0", "--cc=0", "something", + "-a", "-b", "-ab", "-ba", + "-c", "0", "-c=0", "-ac", + "0", "-ac=0", "--aa", "--bb", + "--cc", "0", "--cc=0", "something", }, [_]Arg(u8){ Arg(u8){ .param = aa }, -- cgit v1.2.3