From 928510dc510aac49f7dbc1f1c8e9f4683ab35d84 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Thu, 15 Nov 2018 18:44:07 +0100 Subject: Changed Names.prefix to Names.both --- README.md | 14 +++++++------- build.zig | 4 ++-- example/comptime-clap.zig | 4 ++-- example/streaming-clap.zig | 4 ++-- index.zig | 1 + src/index.zig | 30 ++++++++++++++---------------- 6 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 index.zig diff --git a/README.md b/README.md index 2bc86a6..92de659 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ The `StreamingClap` is base of all the other parsers. It's a streaming parser th ```rust const params = []clap.Param(u8){ - clap.Param(void).flag('h', false, clap.Names.prefix("help")), - clap.Param(void).option('n', true, clap.Names.prefix("number")), + clap.Param(void).flag('h', false, clap.Names.both("help")), + clap.Param(void).option('n', true, clap.Names.both("number")), clap.Param(void).positional('f'), }; @@ -50,8 +50,8 @@ them available through three functions (`flag`, `option`, `positionals`). ```rust const params = comptime []clap.Param(void){ - clap.Param(void).flag({}, false, clap.Names.prefix("help")), - clap.Param(void).option({}, true, clap.Names.prefix("number")), + clap.Param(void).flag({}, false, clap.Names.both("help")), + clap.Param(void).option({}, true, clap.Names.both("number")), clap.Param(void).positional({}), }; @@ -78,7 +78,7 @@ program can take: ```rust const params = comptime []clap.Param(void){ - clap.Param(void).init({}, false, clap.Names.prefix("help")), + clap.Param(void).init({}, false, clap.Names.both("help")), }; var os_iter = clap.args.OsIterator.init(allocator); @@ -123,11 +123,11 @@ try clap.help( []clap.Param([]const u8){ clap.Param([]const u8).flag( "Display this help and exit.", - clap.Names.prefix("help"), + clap.Names.both("help"), ), clap.Param([]const u8).flag( "Output version information and exit.", - clap.Names.prefix("version"), + clap.Names.both("version"), ), }, ); diff --git a/build.zig b/build.zig index 06c012b..3ceba14 100644 --- a/build.zig +++ b/build.zig @@ -13,7 +13,7 @@ pub fn build(b: *Builder) void { "streaming-clap", }) |example_name| { const example = b.addExecutable(example_name, "example/" ++ example_name ++ ".zig"); - example.addPackagePath("clap", "src/index.zig"); + example.addPackagePath("clap", "index.zig"); example.setBuildMode(mode); example_step.dependOn(&example.step); } @@ -22,7 +22,7 @@ pub fn build(b: *Builder) void { inline for ([]Mode{ Mode.Debug, Mode.ReleaseFast, Mode.ReleaseSafe, Mode.ReleaseSmall }) |test_mode| { const mode_str = comptime modeToString(test_mode); - const tests = b.addTest("src/index.zig"); + const tests = b.addTest("index.zig"); tests.setBuildMode(test_mode); tests.setNamePrefix(mode_str ++ " "); diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index b275dc7..00c2a97 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig @@ -16,11 +16,11 @@ pub fn main() !void { const params = comptime []clap.Param([]const u8){ clap.Param([]const u8).flag( "Display this help and exit.", - clap.Names.prefix("help") + clap.Names.both("help"), ), clap.Param([]const u8).option( "An option parameter, which takes a value.", - clap.Names.prefix("number"), + clap.Names.both("number"), ), clap.Param([]const u8).positional(""), }; diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 57ebe71..77d05e2 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -10,8 +10,8 @@ pub fn main() !void { // First we specify what parameters our program can take. const params = []clap.Param(u8){ - clap.Param(u8).flag('h', clap.Names.prefix("help")), - clap.Param(u8).option('n', clap.Names.prefix("number")), + clap.Param(u8).flag('h', clap.Names.both("help")), + clap.Param(u8).option('n', clap.Names.both("number")), clap.Param(u8).positional('f'), }; diff --git a/index.zig b/index.zig new file mode 100644 index 0000000..b867913 --- /dev/null +++ b/index.zig @@ -0,0 +1 @@ +pub use @import("src/index.zig"); diff --git a/src/index.zig b/src/index.zig index e85470c..40ad849 100644 --- a/src/index.zig +++ b/src/index.zig @@ -41,10 +41,10 @@ pub const Names = struct { }; } - /// Initializes a name with a prefix. + /// Initializes a name that is long and short, from the same string. /// ::short is set to ::name[0], and ::long is set to ::name. /// This function asserts that ::name.len != 0 - pub fn prefix(name: []const u8) Names { + pub fn both(name: []const u8) Names { debug.assert(name.len != 0); return Names{ @@ -119,14 +119,13 @@ pub fn helpFull( params: []const Param(Id), comptime Error: type, context: var, - help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, - value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, + help_text: fn (@typeOf(context), Param(Id)) Error![]const u8, + value_text: fn (@typeOf(context), Param(Id)) Error![]const u8, ) !void { const max_spacing = blk: { - var null_stream = io.NullOutStream.init(); var res: usize = 0; for (params) |param| { - var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); + var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(io.null_out_stream); try printParam(&counting_stream.stream, Id, param, Error, context, value_text); if (res < counting_stream.bytes_written) res = counting_stream.bytes_written; @@ -153,7 +152,7 @@ fn printParam( param: Param(Id), comptime Error: type, context: var, - value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, + value_text: fn (@typeOf(context), Param(Id)) Error![]const u8, ) @typeOf(stream.*).Error!void { if (param.names.short) |s| { try stream.print("-{c}", s); @@ -179,12 +178,12 @@ pub fn helpEx( stream: var, comptime Id: type, params: []const Param(Id), - help_text: fn(Param(Id)) []const u8, - value_text: fn(Param(Id)) []const u8, + help_text: fn (Param(Id)) []const u8, + value_text: fn (Param(Id)) []const u8, ) !void { const Context = struct { - help_text: fn(Param(Id)) []const u8, - value_text: fn(Param(Id)) []const u8, + help_text: fn (Param(Id)) []const u8, + value_text: fn (Param(Id)) []const u8, pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { return c.help_text(p); @@ -223,7 +222,6 @@ fn getValueSimple(param: Param([]const u8)) []const u8 { return "VALUE"; } - test "clap.help" { var buf: [1024]u8 = undefined; var slice_stream = io.SliceOutStream.init(buf[0..]); @@ -248,19 +246,19 @@ test "clap.help" { ), Param([]const u8).flag( "Both flag.", - Names.prefix("cc"), + Names.both("cc"), ), Param([]const u8).option( "Both option.", - Names.prefix("dd"), + Names.both("dd"), ), Param([]const u8).positional( - "Positional. This should not appear in the help message." + "Positional. This should not appear in the help message.", ), }, ); - const expected = + const expected = "" ++ "\t-a \tShort flag.\n" ++ "\t-b=VALUE \tShort option.\n" ++ "\t --aa \tLong flag.\n" ++ -- cgit v1.2.3 From f4eb2e0937614b0c1c4dd99387146c3bd0d4649e Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 16 Nov 2018 09:56:29 +0100 Subject: Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92de659..c46ef11 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(void).flag('h', false, clap.Names.both("help")), - clap.Param(void).option('n', true, clap.Names.both("number")), - clap.Param(void).positional('f'), + clap.Param(u8).flag('h', false, clap.Names.both("help")), + clap.Param(u8).option('n', true, clap.Names.both("number")), + clap.Param(u8).positional('f'), }; var os_iter = clap.args.OsIterator.init(allocator); -- cgit v1.2.3 From d0005fab619a0f8cc92b166f2d40764ab0d3043e Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Tue, 4 Dec 2018 14:48:46 +0100 Subject: Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c46ef11..0d88e53 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ The `StreamingClap` is base of all the other parsers. It's a streaming parser th ```rust const params = []clap.Param(u8){ - clap.Param(u8).flag('h', false, clap.Names.both("help")), - clap.Param(u8).option('n', true, clap.Names.both("number")), + clap.Param(u8).flag('h', clap.Names.both("help")), + clap.Param(u8).option('n', clap.Names.both("number")), clap.Param(u8).positional('f'), }; @@ -50,8 +50,8 @@ them available through three functions (`flag`, `option`, `positionals`). ```rust const params = comptime []clap.Param(void){ - clap.Param(void).flag({}, false, clap.Names.both("help")), - clap.Param(void).option({}, true, clap.Names.both("number")), + clap.Param(void).flag({}, clap.Names.both("help")), + clap.Param(void).option({}, clap.Names.both("number")), clap.Param(void).positional({}), }; @@ -78,7 +78,7 @@ program can take: ```rust const params = comptime []clap.Param(void){ - clap.Param(void).init({}, false, clap.Names.both("help")), + clap.Param(void).flag({}, clap.Names.both("help")), }; var os_iter = clap.args.OsIterator.init(allocator); -- cgit v1.2.3 From 7800f4e2247cbf3eaf5306a7e41ff17130885328 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Sun, 16 Dec 2018 13:23:40 +0100 Subject: Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d88e53..b64ab2b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ A simple and easy to use command line argument parser library for Zig. ### `StreamingClap` -The `StreamingClap` is base of all the other parsers. It's a streaming parser that uses an +The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an `args.Iterator` to provide it with arguments lazily. ```rust @@ -106,7 +106,7 @@ zig-clap/example/comptime-clap.zig:41:18: note: called from here ^ ``` -Ofc, this limits you to use only parameters that are comptime known. +Ofc, this limits you to parameters that are comptime known. ### `help` -- cgit v1.2.3