From f968d56d96989d1c74664449f509d7d1b2e3010f Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 14 Nov 2018 14:25:42 +0100 Subject: Added pub flag/option/positional init funcs to Param --- src/comptime.zig | 8 ++++---- src/index.zig | 22 +++++++++++++--------- src/streaming.zig | 22 +++++++++++----------- 3 files changed, 28 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/comptime.zig b/src/comptime.zig index b11dccc..ddcd7e1 100644 --- a/src/comptime.zig +++ b/src/comptime.zig @@ -108,19 +108,19 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) test "clap.comptime.ComptimeClap" { const Clap = ComptimeClap(void, comptime []clap.Param(void){ - clap.Param(void).init({}, false, clap.Names{ + clap.Param(void).flag({}, clap.Names{ .short = 'a', .long = "aa", }), - clap.Param(void).init({}, false, clap.Names{ + clap.Param(void).flag({}, clap.Names{ .short = 'b', .long = "bb", }), - clap.Param(void).init({}, true, clap.Names{ + clap.Param(void).option({}, clap.Names{ .short = 'c', .long = "cc", }), - clap.Param(void).init({}, true, clap.Names.positional()), + clap.Param(void).positional({}), }); var buf: [1024]u8 = undefined; diff --git a/src/index.zig b/src/index.zig index dde4748..5900424 100644 --- a/src/index.zig +++ b/src/index.zig @@ -23,14 +23,6 @@ pub const Names = struct { /// '--' prefix long: ?[]const u8, - /// Initializes no names - pub fn positional() Names { - return Names{ - .short = null, - .long = null, - }; - } - /// Initializes a short name pub fn short(s: u8) Names { return Names{ @@ -87,7 +79,19 @@ pub fn Param(comptime Id: type) type { takes_value: bool, names: Names, - pub fn init(id: Id, takes_value: bool, names: Names) @This() { + pub fn flag(id: Id, names: Names) @This() { + return init(id, false, names); + } + + pub fn option(id: Id, names: Names) @This() { + return init(id, true, names); + } + + pub fn positional(id: Id) @This() { + return init(id, true, Names{ .short = null, .long = null }); + } + + fn init(id: Id, takes_value: bool, names: Names) @This() { // Assert, that if the param have no name, then it has to take // a value. debug.assert( diff --git a/src/streaming.zig b/src/streaming.zig index bfb4045..99eaecb 100644 --- a/src/streaming.zig +++ b/src/streaming.zig @@ -212,9 +212,9 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r test "clap.streaming.StreamingClap: short params" { const params = []clap.Param(u8){ - clap.Param(u8).init(0, false, clap.Names.short('a')), - clap.Param(u8).init(1, false, clap.Names.short('b')), - clap.Param(u8).init(2, true, clap.Names.short('c')), + clap.Param(u8).flag(0, clap.Names.short('a')), + clap.Param(u8).flag(1, clap.Names.short('b')), + clap.Param(u8).option(2, clap.Names.short('c')), }; const a = ¶ms[0]; @@ -247,9 +247,9 @@ test "clap.streaming.StreamingClap: short params" { test "clap.streaming.StreamingClap: long params" { const params = []clap.Param(u8){ - clap.Param(u8).init(0, false, clap.Names.long("aa")), - clap.Param(u8).init(1, false, clap.Names.long("bb")), - clap.Param(u8).init(2, true, clap.Names.long("cc")), + clap.Param(u8).flag(0, clap.Names.long("aa")), + clap.Param(u8).flag(1, clap.Names.long("bb")), + clap.Param(u8).option(2, clap.Names.long("cc")), }; const aa = ¶ms[0]; @@ -273,7 +273,7 @@ test "clap.streaming.StreamingClap: long params" { } test "clap.streaming.StreamingClap: positional params" { - const params = []clap.Param(u8){clap.Param(u8).init(0, true, clap.Names.positional())}; + const params = []clap.Param(u8){clap.Param(u8).positional(0)}; testNoErr( params, @@ -287,19 +287,19 @@ test "clap.streaming.StreamingClap: positional params" { test "clap.streaming.StreamingClap: all params" { const params = []clap.Param(u8){ - clap.Param(u8).init(0, false, clap.Names{ + clap.Param(u8).flag(0, clap.Names{ .short = 'a', .long = "aa", }), - clap.Param(u8).init(1, false, clap.Names{ + clap.Param(u8).flag(1, clap.Names{ .short = 'b', .long = "bb", }), - clap.Param(u8).init(2, true, clap.Names{ + clap.Param(u8).option(2, clap.Names{ .short = 'c', .long = "cc", }), - clap.Param(u8).init(3, true, clap.Names.positional()), + clap.Param(u8).positional(3), }; const aa = ¶ms[0]; -- cgit v1.2.3