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 --- README.md | 12 ++++++------ example/comptime-clap.zig | 6 +++--- example/streaming-clap.zig | 6 +++--- src/comptime.zig | 8 ++++---- src/index.zig | 22 +++++++++++++--------- src/streaming.zig | 22 +++++++++++----------- 6 files changed, 40 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 92f53c2..68970d9 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ The `StreamingClap` is base of all the other parsers. It's a streaming parser th ```rust const params = []clap.Param(u8){ - clap.Param(u8).init('h', false, clap.Names.prefix("help")), - clap.Param(u8).init('n', true, clap.Names.prefix("number")), - clap.Param(u8).init('f', true, clap.Names.positional()), + clap.Param(void).flag('h', false, clap.Names.prefix("help")), + clap.Param(void).option('n', true, clap.Names.prefix("number")), + clap.Param(void).positional('f'), }; var os_iter = clap.args.OsIterator.init(allocator); @@ -50,9 +50,9 @@ them available through three functions (`flag`, `option`, `positionals`). ```rust const params = comptime []clap.Param(void){ - clap.Param(void).init({}, false, clap.Names.prefix("help")), - clap.Param(void).init({}, true, clap.Names.prefix("number")), - clap.Param(void).init({}, true, clap.Names.positional()), + clap.Param(void).flag({}, false, clap.Names.prefix("help")), + clap.Param(void).option({}, true, clap.Names.prefix("number")), + clap.Param(void).positional({}), }; var os_iter = clap.args.OsIterator.init(allocator); diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index e44d9b1..3b7b42b 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig @@ -17,12 +17,12 @@ pub fn main() !void { // * A "Names" struct, which determins what names the parameter will have on the // commandline. Names.prefix inits a "Names" struct that has the "short" name // set to the first letter, and the "long" name set to the full name. - clap.Param(void).init({}, false, clap.Names.prefix("help")), - clap.Param(void).init({}, true, clap.Names.prefix("number")), + clap.Param(void).flag({}, clap.Names.prefix("help")), + clap.Param(void).option({}, clap.Names.prefix("number")), // Names.positional returns a "Names" struct where neither the "short" or "long" // name is set. - clap.Param(void).init({}, true, clap.Names.positional()), + clap.Param(void).positional({}), }; // We then initialize an argument iterator. We will use the OsIterator as it nicely diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 0dc2bf7..013c1d4 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -17,12 +17,12 @@ pub fn main() !void { // * A "Names" struct, which determins what names the parameter will have on the // commandline. Names.prefix inits a "Names" struct that has the "short" name // set to the first letter, and the "long" name set to the full name. - clap.Param(u8).init('h', false, clap.Names.prefix("help")), - clap.Param(u8).init('n', true, clap.Names.prefix("number")), + clap.Param(u8).flag('h', clap.Names.prefix("help")), + clap.Param(u8).option('n', clap.Names.prefix("number")), // Names.positional returns a "Names" struct where neither the "short" or "long" // name is set. - clap.Param(u8).init('f', true, clap.Names.positional()), + clap.Param(u8).positional('f'), }; // We then initialize an argument iterator. We will use the OsIterator as it nicely 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