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 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/comptime.zig') 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{ -- cgit v1.2.3