diff options
| author | 2018-11-15 18:44:07 +0100 | |
|---|---|---|
| committer | 2018-11-15 18:44:07 +0100 | |
| commit | 928510dc510aac49f7dbc1f1c8e9f4683ab35d84 (patch) | |
| tree | e33178c707d943e05d885b2d8383c9d8da1b1d77 | |
| parent | Renamed helpEx to helpFull and added a new helpEx that wraps helpFull (diff) | |
| download | zig-clap-928510dc510aac49f7dbc1f1c8e9f4683ab35d84.tar.gz zig-clap-928510dc510aac49f7dbc1f1c8e9f4683ab35d84.tar.xz zig-clap-928510dc510aac49f7dbc1f1c8e9f4683ab35d84.zip | |
Changed Names.prefix to Names.both
Diffstat (limited to '')
| -rw-r--r-- | README.md | 14 | ||||
| -rw-r--r-- | build.zig | 4 | ||||
| -rw-r--r-- | example/comptime-clap.zig | 4 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 4 | ||||
| -rw-r--r-- | index.zig | 1 | ||||
| -rw-r--r-- | src/index.zig | 30 |
6 files changed, 28 insertions, 29 deletions
| @@ -20,8 +20,8 @@ The `StreamingClap` is base of all the other parsers. It's a streaming parser th | |||
| 20 | 20 | ||
| 21 | ```rust | 21 | ```rust |
| 22 | const params = []clap.Param(u8){ | 22 | const params = []clap.Param(u8){ |
| 23 | clap.Param(void).flag('h', false, clap.Names.prefix("help")), | 23 | clap.Param(void).flag('h', false, clap.Names.both("help")), |
| 24 | clap.Param(void).option('n', true, clap.Names.prefix("number")), | 24 | clap.Param(void).option('n', true, clap.Names.both("number")), |
| 25 | clap.Param(void).positional('f'), | 25 | clap.Param(void).positional('f'), |
| 26 | }; | 26 | }; |
| 27 | 27 | ||
| @@ -50,8 +50,8 @@ them available through three functions (`flag`, `option`, `positionals`). | |||
| 50 | 50 | ||
| 51 | ```rust | 51 | ```rust |
| 52 | const params = comptime []clap.Param(void){ | 52 | const params = comptime []clap.Param(void){ |
| 53 | clap.Param(void).flag({}, false, clap.Names.prefix("help")), | 53 | clap.Param(void).flag({}, false, clap.Names.both("help")), |
| 54 | clap.Param(void).option({}, true, clap.Names.prefix("number")), | 54 | clap.Param(void).option({}, true, clap.Names.both("number")), |
| 55 | clap.Param(void).positional({}), | 55 | clap.Param(void).positional({}), |
| 56 | }; | 56 | }; |
| 57 | 57 | ||
| @@ -78,7 +78,7 @@ program can take: | |||
| 78 | 78 | ||
| 79 | ```rust | 79 | ```rust |
| 80 | const params = comptime []clap.Param(void){ | 80 | const params = comptime []clap.Param(void){ |
| 81 | clap.Param(void).init({}, false, clap.Names.prefix("help")), | 81 | clap.Param(void).init({}, false, clap.Names.both("help")), |
| 82 | }; | 82 | }; |
| 83 | 83 | ||
| 84 | var os_iter = clap.args.OsIterator.init(allocator); | 84 | var os_iter = clap.args.OsIterator.init(allocator); |
| @@ -123,11 +123,11 @@ try clap.help( | |||
| 123 | []clap.Param([]const u8){ | 123 | []clap.Param([]const u8){ |
| 124 | clap.Param([]const u8).flag( | 124 | clap.Param([]const u8).flag( |
| 125 | "Display this help and exit.", | 125 | "Display this help and exit.", |
| 126 | clap.Names.prefix("help"), | 126 | clap.Names.both("help"), |
| 127 | ), | 127 | ), |
| 128 | clap.Param([]const u8).flag( | 128 | clap.Param([]const u8).flag( |
| 129 | "Output version information and exit.", | 129 | "Output version information and exit.", |
| 130 | clap.Names.prefix("version"), | 130 | clap.Names.both("version"), |
| 131 | ), | 131 | ), |
| 132 | }, | 132 | }, |
| 133 | ); | 133 | ); |
| @@ -13,7 +13,7 @@ pub fn build(b: *Builder) void { | |||
| 13 | "streaming-clap", | 13 | "streaming-clap", |
| 14 | }) |example_name| { | 14 | }) |example_name| { |
| 15 | const example = b.addExecutable(example_name, "example/" ++ example_name ++ ".zig"); | 15 | const example = b.addExecutable(example_name, "example/" ++ example_name ++ ".zig"); |
| 16 | example.addPackagePath("clap", "src/index.zig"); | 16 | example.addPackagePath("clap", "index.zig"); |
| 17 | example.setBuildMode(mode); | 17 | example.setBuildMode(mode); |
| 18 | example_step.dependOn(&example.step); | 18 | example_step.dependOn(&example.step); |
| 19 | } | 19 | } |
| @@ -22,7 +22,7 @@ pub fn build(b: *Builder) void { | |||
| 22 | inline for ([]Mode{ Mode.Debug, Mode.ReleaseFast, Mode.ReleaseSafe, Mode.ReleaseSmall }) |test_mode| { | 22 | inline for ([]Mode{ Mode.Debug, Mode.ReleaseFast, Mode.ReleaseSafe, Mode.ReleaseSmall }) |test_mode| { |
| 23 | const mode_str = comptime modeToString(test_mode); | 23 | const mode_str = comptime modeToString(test_mode); |
| 24 | 24 | ||
| 25 | const tests = b.addTest("src/index.zig"); | 25 | const tests = b.addTest("index.zig"); |
| 26 | tests.setBuildMode(test_mode); | 26 | tests.setBuildMode(test_mode); |
| 27 | tests.setNamePrefix(mode_str ++ " "); | 27 | tests.setNamePrefix(mode_str ++ " "); |
| 28 | 28 | ||
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 { | |||
| 16 | const params = comptime []clap.Param([]const u8){ | 16 | const params = comptime []clap.Param([]const u8){ |
| 17 | clap.Param([]const u8).flag( | 17 | clap.Param([]const u8).flag( |
| 18 | "Display this help and exit.", | 18 | "Display this help and exit.", |
| 19 | clap.Names.prefix("help") | 19 | clap.Names.both("help"), |
| 20 | ), | 20 | ), |
| 21 | clap.Param([]const u8).option( | 21 | clap.Param([]const u8).option( |
| 22 | "An option parameter, which takes a value.", | 22 | "An option parameter, which takes a value.", |
| 23 | clap.Names.prefix("number"), | 23 | clap.Names.both("number"), |
| 24 | ), | 24 | ), |
| 25 | clap.Param([]const u8).positional(""), | 25 | clap.Param([]const u8).positional(""), |
| 26 | }; | 26 | }; |
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 { | |||
| 10 | 10 | ||
| 11 | // First we specify what parameters our program can take. | 11 | // First we specify what parameters our program can take. |
| 12 | const params = []clap.Param(u8){ | 12 | const params = []clap.Param(u8){ |
| 13 | clap.Param(u8).flag('h', clap.Names.prefix("help")), | 13 | clap.Param(u8).flag('h', clap.Names.both("help")), |
| 14 | clap.Param(u8).option('n', clap.Names.prefix("number")), | 14 | clap.Param(u8).option('n', clap.Names.both("number")), |
| 15 | clap.Param(u8).positional('f'), | 15 | clap.Param(u8).positional('f'), |
| 16 | }; | 16 | }; |
| 17 | 17 | ||
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 { | |||
| 41 | }; | 41 | }; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | /// Initializes a name with a prefix. | 44 | /// Initializes a name that is long and short, from the same string. |
| 45 | /// ::short is set to ::name[0], and ::long is set to ::name. | 45 | /// ::short is set to ::name[0], and ::long is set to ::name. |
| 46 | /// This function asserts that ::name.len != 0 | 46 | /// This function asserts that ::name.len != 0 |
| 47 | pub fn prefix(name: []const u8) Names { | 47 | pub fn both(name: []const u8) Names { |
| 48 | debug.assert(name.len != 0); | 48 | debug.assert(name.len != 0); |
| 49 | 49 | ||
| 50 | return Names{ | 50 | return Names{ |
| @@ -119,14 +119,13 @@ pub fn helpFull( | |||
| 119 | params: []const Param(Id), | 119 | params: []const Param(Id), |
| 120 | comptime Error: type, | 120 | comptime Error: type, |
| 121 | context: var, | 121 | context: var, |
| 122 | help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | 122 | help_text: fn (@typeOf(context), Param(Id)) Error![]const u8, |
| 123 | value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | 123 | value_text: fn (@typeOf(context), Param(Id)) Error![]const u8, |
| 124 | ) !void { | 124 | ) !void { |
| 125 | const max_spacing = blk: { | 125 | const max_spacing = blk: { |
| 126 | var null_stream = io.NullOutStream.init(); | ||
| 127 | var res: usize = 0; | 126 | var res: usize = 0; |
| 128 | for (params) |param| { | 127 | for (params) |param| { |
| 129 | var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); | 128 | var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(io.null_out_stream); |
| 130 | try printParam(&counting_stream.stream, Id, param, Error, context, value_text); | 129 | try printParam(&counting_stream.stream, Id, param, Error, context, value_text); |
| 131 | if (res < counting_stream.bytes_written) | 130 | if (res < counting_stream.bytes_written) |
| 132 | res = counting_stream.bytes_written; | 131 | res = counting_stream.bytes_written; |
| @@ -153,7 +152,7 @@ fn printParam( | |||
| 153 | param: Param(Id), | 152 | param: Param(Id), |
| 154 | comptime Error: type, | 153 | comptime Error: type, |
| 155 | context: var, | 154 | context: var, |
| 156 | value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | 155 | value_text: fn (@typeOf(context), Param(Id)) Error![]const u8, |
| 157 | ) @typeOf(stream.*).Error!void { | 156 | ) @typeOf(stream.*).Error!void { |
| 158 | if (param.names.short) |s| { | 157 | if (param.names.short) |s| { |
| 159 | try stream.print("-{c}", s); | 158 | try stream.print("-{c}", s); |
| @@ -179,12 +178,12 @@ pub fn helpEx( | |||
| 179 | stream: var, | 178 | stream: var, |
| 180 | comptime Id: type, | 179 | comptime Id: type, |
| 181 | params: []const Param(Id), | 180 | params: []const Param(Id), |
| 182 | help_text: fn(Param(Id)) []const u8, | 181 | help_text: fn (Param(Id)) []const u8, |
| 183 | value_text: fn(Param(Id)) []const u8, | 182 | value_text: fn (Param(Id)) []const u8, |
| 184 | ) !void { | 183 | ) !void { |
| 185 | const Context = struct { | 184 | const Context = struct { |
| 186 | help_text: fn(Param(Id)) []const u8, | 185 | help_text: fn (Param(Id)) []const u8, |
| 187 | value_text: fn(Param(Id)) []const u8, | 186 | value_text: fn (Param(Id)) []const u8, |
| 188 | 187 | ||
| 189 | pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { | 188 | pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { |
| 190 | return c.help_text(p); | 189 | return c.help_text(p); |
| @@ -223,7 +222,6 @@ fn getValueSimple(param: Param([]const u8)) []const u8 { | |||
| 223 | return "VALUE"; | 222 | return "VALUE"; |
| 224 | } | 223 | } |
| 225 | 224 | ||
| 226 | |||
| 227 | test "clap.help" { | 225 | test "clap.help" { |
| 228 | var buf: [1024]u8 = undefined; | 226 | var buf: [1024]u8 = undefined; |
| 229 | var slice_stream = io.SliceOutStream.init(buf[0..]); | 227 | var slice_stream = io.SliceOutStream.init(buf[0..]); |
| @@ -248,19 +246,19 @@ test "clap.help" { | |||
| 248 | ), | 246 | ), |
| 249 | Param([]const u8).flag( | 247 | Param([]const u8).flag( |
| 250 | "Both flag.", | 248 | "Both flag.", |
| 251 | Names.prefix("cc"), | 249 | Names.both("cc"), |
| 252 | ), | 250 | ), |
| 253 | Param([]const u8).option( | 251 | Param([]const u8).option( |
| 254 | "Both option.", | 252 | "Both option.", |
| 255 | Names.prefix("dd"), | 253 | Names.both("dd"), |
| 256 | ), | 254 | ), |
| 257 | Param([]const u8).positional( | 255 | Param([]const u8).positional( |
| 258 | "Positional. This should not appear in the help message." | 256 | "Positional. This should not appear in the help message.", |
| 259 | ), | 257 | ), |
| 260 | }, | 258 | }, |
| 261 | ); | 259 | ); |
| 262 | 260 | ||
| 263 | const expected = | 261 | const expected = "" ++ |
| 264 | "\t-a \tShort flag.\n" ++ | 262 | "\t-a \tShort flag.\n" ++ |
| 265 | "\t-b=VALUE \tShort option.\n" ++ | 263 | "\t-b=VALUE \tShort option.\n" ++ |
| 266 | "\t --aa \tLong flag.\n" ++ | 264 | "\t --aa \tLong flag.\n" ++ |