From 4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Sat, 8 May 2021 18:08:52 +0200 Subject: Modernize codebase * Better naming for variables * Follow naming style of enums * Use `writer()` instead of `outStream()` * Change many initializers to be a one liner * Don't explicitly initialize fields to their default value --- README.md | 38 ++++---- build.zig | 4 +- clap.zig | 211 +++++++++++++++------------------------------ clap/comptime.zig | 20 ++--- clap/streaming.zig | 184 +++++++++++++++++---------------------- example/help.zig | 7 +- example/simple-error.zig | 4 +- example/simple-ex.zig | 3 +- example/simple.zig | 3 +- example/streaming-clap.zig | 18 ++-- example/usage.zig | 6 +- 11 files changed, 194 insertions(+), 304 deletions(-) diff --git a/README.md b/README.md index ad9f026..398a088 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { // First we specify what parameters our program can take. @@ -46,7 +47,7 @@ pub fn main() !void { var diag = clap.Diagnostic{}; var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }; defer args.deinit(); @@ -68,15 +69,15 @@ that the strings you pass to `option`, `options` and `flag` are actually paramet program can take: ```zig -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { const params = comptime [_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit.") catch unreachable, }; - var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); + var args = try clap.parse(clap.Help, ¶ms, .{}); defer args.deinit(); _ = args.flag("--helps"); @@ -110,25 +111,23 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { const allocator = std.heap.page_allocator; // First we specify what parameters our program can take. const params = [_]clap.Param(u8){ - clap.Param(u8){ + .{ .id = 'h', - .names = clap.Names{ .short = 'h', .long = "help" }, + .names = .{ .short = 'h', .long = "help" }, }, - clap.Param(u8){ + .{ .id = 'n', - .names = clap.Names{ .short = 'n', .long = "number" }, - .takes_value = .One, - }, - clap.Param(u8){ - .id = 'f', - .takes_value = .One, + .names = .{ .short = 'n', .long = "number" }, + .takes_value = .one, }, + .{ .id = 'f', .takes_value = .one }, }; // We then initialize an argument iterator. We will use the OsIterator as it nicely @@ -149,7 +148,7 @@ pub fn main() !void { // Because we use a streaming parser, we have to consume each argument parsed individually. while (parser.next() catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }) |arg| { // arg.param will point to the parameter which matched the argument. @@ -177,18 +176,15 @@ The `help`, `helpEx` and `helpFull` are functions for printing a simple list of program can take. ```zig -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr_file = std.io.getStdErr(); - var stderr_out_stream = stderr_file.outStream(); - // clap.help is a function that can print a simple help message, given a // slice of Param(Help). There is also a helpEx, which can print a // help message for any Param, but it is more verbose to call. try clap.help( - stderr_out_stream, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, @@ -218,17 +214,15 @@ The `usage`, `usageEx` and `usageFull` are functions for printing a small abbrev of the help message. ```zig -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr = std.io.getStdErr().outStream(); - // clap.usage is a function that can print a simple usage message, given a // slice of Param(Help). There is also a usageEx, which can print a // usage message for any Param, but it is more verbose to call. try clap.usage( - stderr, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, diff --git a/build.zig b/build.zig index ef67a5f..bef10be 100644 --- a/build.zig +++ b/build.zig @@ -1,8 +1,8 @@ const builtin = @import("builtin"); const std = @import("std"); -const Mode = builtin.Mode; const Builder = std.build.Builder; +const Mode = builtin.Mode; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); @@ -58,7 +58,7 @@ fn readMeStep(b: *Builder) *std.build.Step { fn make(step: *std.build.Step) anyerror!void { @setEvalBranchQuota(10000); const file = try std.fs.cwd().createFile("README.md", .{}); - const stream = &file.outStream(); + const stream = file.writer(); try stream.print(@embedFile("example/README.md.template"), .{ @embedFile("example/simple.zig"), @embedFile("example/simple-error.zig"), diff --git a/clap.zig b/clap.zig index b31cd1d..49d0670 100644 --- a/clap.zig +++ b/clap.zig @@ -26,9 +26,9 @@ pub const Names = struct { /// Whether a param takes no value (a flag), one value, or can be specified multiple times. pub const Values = enum { - None, - One, - Many, + none, + one, + many, }; /// Represents a parameter for the command line. @@ -56,7 +56,7 @@ pub fn Param(comptime Id: type) type { return struct { id: Id = Id{}, names: Names = Names{}, - takes_value: Values = .None, + takes_value: Values = .none, }; } @@ -110,8 +110,8 @@ fn parseParamRest(line: []const u8) Param(Help) { const len = mem.indexOfScalar(u8, line, '>') orelse break :blk; const takes_many = mem.startsWith(u8, line[len + 1 ..], "..."); const help_start = len + 1 + @as(usize, 3) * @boolToInt(takes_many); - return Param(Help){ - .takes_value = if (takes_many) .Many else .One, + return .{ + .takes_value = if (takes_many) .many else .one, .id = .{ .msg = mem.trim(u8, line[help_start..], " \t"), .value = line[1..len], @@ -119,7 +119,7 @@ fn parseParamRest(line: []const u8) Param(Help) { }; } - return Param(Help){ .id = .{ .msg = mem.trim(u8, line, " \t") } }; + return .{ .id = .{ .msg = mem.trim(u8, line, " \t") } }; } fn expectParam(expect: Param(Help), actual: Param(Help)) void { @@ -136,114 +136,60 @@ fn expectParam(expect: Param(Help), actual: Param(Help)) void { test "parseParam" { expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "value", - }, - .names = Names{ - .short = 's', - .long = "long", - }, - .takes_value = .One, + .id = .{ .msg = "Help text", .value = "value" }, + .names = .{ .short = 's', .long = "long" }, + .takes_value = .one, }, try parseParam("-s, --long Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "value", - }, - .names = Names{ - .short = 's', - .long = "long", - }, - .takes_value = .Many, + .id = .{ .msg = "Help text", .value = "value" }, + .names = .{ .short = 's', .long = "long" }, + .takes_value = .many, }, try parseParam("-s, --long ... Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "value", - }, - .names = Names{ - .short = null, - .long = "long", - }, - .takes_value = .One, + .id = .{ .msg = "Help text", .value = "value" }, + .names = .{ .long = "long" }, + .takes_value = .one, }, try parseParam("--long Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "value", - }, - .names = Names{ - .short = 's', - .long = null, - }, - .takes_value = .One, + .id = .{ .msg = "Help text", .value = "value" }, + .names = .{ .short = 's' }, + .takes_value = .one, }, try parseParam("-s Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "", - }, - .names = Names{ - .short = 's', - .long = "long", - }, - .takes_value = .None, + .id = .{ .msg = "Help text" }, + .names = .{ .short = 's', .long = "long" }, }, try parseParam("-s, --long Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "", - }, - .names = Names{ - .short = 's', - .long = null, - }, - .takes_value = .None, + .id = .{ .msg = "Help text" }, + .names = .{ .short = 's' }, }, try parseParam("-s Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "", - }, - .names = Names{ - .short = null, - .long = "long", - }, - .takes_value = .None, + .id = .{ .msg = "Help text" }, + .names = .{ .long = "long" }, }, try parseParam("--long Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "A | B", - }, - .names = Names{ - .short = null, - .long = "long", - }, - .takes_value = .One, + .id = .{ .msg = "Help text", .value = "A | B" }, + .names = .{ .long = "long" }, + .takes_value = .one, }, try parseParam("--long Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "A", - }, - .names = Names{ - .short = null, - .long = null, - }, - .takes_value = .One, + .id = .{ .msg = "Help text", .value = "A" }, + .names = .{}, + .takes_value = .one, }, try parseParam(" Help text")); + expectParam(Param(Help){ - .id = Help{ - .msg = "Help text", - .value = "A", - }, - .names = Names{ - .short = null, - .long = null, - }, - .takes_value = .Many, + .id = .{ .msg = "Help text", .value = "A" }, + .names = .{}, + .takes_value = .many, }, try parseParam("... Help text")); testing.expectError(error.TrailingComma, parseParam("--long, Help")); @@ -284,7 +230,7 @@ pub const Diagnostic = struct { fn testDiag(diag: Diagnostic, err: anyerror, expected: []const u8) void { var buf: [1024]u8 = undefined; var slice_stream = io.fixedBufferStream(&buf); - diag.report(slice_stream.outStream(), err) catch unreachable; + diag.report(slice_stream.writer(), err) catch unreachable; testing.expectEqualStrings(expected, slice_stream.getWritten()); } @@ -392,10 +338,10 @@ pub fn helpFull( const max_spacing = blk: { var res: usize = 0; for (params) |param| { - var counting_stream = io.countingOutStream(io.null_out_stream); - try printParam(counting_stream.outStream(), Id, param, Error, context, valueText); - if (res < counting_stream.bytes_written) - res = @intCast(usize, counting_stream.bytes_written); + var cs = io.countingOutStream(io.null_out_stream); + try printParam(cs.writer(), Id, param, Error, context, valueText); + if (res < cs.bytes_written) + res = @intCast(usize, cs.bytes_written); } break :blk res; @@ -405,10 +351,10 @@ pub fn helpFull( if (param.names.short == null and param.names.long == null) continue; - var counting_stream = io.countingOutStream(stream); + var cs = io.countingOutStream(stream); try stream.print("\t", .{}); - try printParam(counting_stream.outStream(), Id, param, Error, context, valueText); - try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, counting_stream.bytes_written)); + try printParam(cs.writer(), Id, param, Error, context, valueText); + try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); try stream.print("\t{}\n", .{try helpText(context, param)}); } } @@ -437,9 +383,9 @@ fn printParam( } switch (param.takes_value) { - .None => {}, - .One => try stream.print(" <{}>", .{valueText(context, param)}), - .Many => try stream.print(" <{}>...", .{valueText(context, param)}), + .none => {}, + .one => try stream.print(" <{}>", .{valueText(context, param)}), + .many => try stream.print(" <{}>...", .{valueText(context, param)}), } } @@ -503,21 +449,16 @@ test "clap.help" { @setEvalBranchQuota(10000); try help( - slice_stream.outStream(), + slice_stream.writer(), comptime &[_]Param(Help){ - parseParam("-a Short flag. ") catch unreachable, + parseParam("-a Short flag.") catch unreachable, parseParam("-b Short option.") catch unreachable, - parseParam("--aa Long flag. ") catch unreachable, - parseParam("--bb Long option. ") catch unreachable, - parseParam("-c, --cc Both flag. ") catch unreachable, - parseParam("-d, --dd Both option. ") catch unreachable, - parseParam("-d, --dd ... Both repeated option. ") catch unreachable, - Param(Help){ - .id = Help{ - .msg = "Positional. This should not appear in the help message.", - }, - .takes_value = .One, - }, + parseParam("--aa Long flag.") catch unreachable, + parseParam("--bb Long option.") catch unreachable, + parseParam("-c, --cc Both flag.") catch unreachable, + parseParam("-d, --dd Both option.") catch unreachable, + parseParam("-d, --dd ... Both repeated option.") catch unreachable, + parseParam("

Positional. This should not appear in the help message.") catch unreachable, }, ); @@ -547,10 +488,10 @@ pub fn usageFull( valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8, ) !void { var cos = io.countingOutStream(stream); - const cs = cos.outStream(); + const cs = cos.writer(); for (params) |param| { const name = param.names.short orelse continue; - if (param.takes_value != .None) + if (param.takes_value != .none) continue; if (cos.bytes_written == 0) @@ -562,7 +503,7 @@ pub fn usageFull( var positional: ?Param(Id) = null; for (params) |param| { - if (param.takes_value == .None and param.names.short != null) + if (param.takes_value == .none and param.names.short != null) continue; const prefix = if (param.names.short) |_| "-" else "--"; @@ -578,9 +519,9 @@ pub fn usageFull( try cs.print("[{}{}", .{ prefix, name }); switch (param.takes_value) { - .None => {}, - .One => try cs.print(" <{}>", .{try valueText(context, param)}), - .Many => try cs.print(" <{}>...", .{try valueText(context, param)}), + .none => {}, + .one => try cs.print(" <{}>", .{try valueText(context, param)}), + .many => try cs.print(" <{}>...", .{try valueText(context, param)}), } try cs.writeByte(']'); @@ -627,7 +568,7 @@ pub fn usage(stream: anytype, params: []const Param(Help)) !void { fn testUsage(expected: []const u8, params: []const Param(Help)) !void { var buf: [1024]u8 = undefined; var fbs = io.fixedBufferStream(&buf); - try usage(fbs.outStream(), params); + try usage(fbs.writer(), params); testing.expectEqualStrings(expected, fbs.getWritten()); } @@ -650,12 +591,7 @@ test "usage" { parseParam("--b ") catch unreachable, }); try testUsage("", comptime &[_]Param(Help){ - Param(Help){ - .id = Help{ - .value = "file", - }, - .takes_value = .One, - }, + parseParam("") catch unreachable, }); try testUsage("[-ab] [-c ] [-d ] [--e] [--f] [--g ] [--h ] [-i ...] ", comptime &[_]Param(Help){ parseParam("-a") catch unreachable, @@ -667,11 +603,6 @@ test "usage" { parseParam("--g ") catch unreachable, parseParam("--h ") catch unreachable, parseParam("-i ...") catch unreachable, - Param(Help){ - .id = Help{ - .value = "file", - }, - .takes_value = .One, - }, + parseParam("") catch unreachable, }); } diff --git a/clap/comptime.zig b/clap/comptime.zig index 9bec38e..122ff16 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig @@ -19,9 +19,9 @@ pub fn ComptimeClap( var index: usize = 0; if (param.names.long != null or param.names.short != null) { const ptr = switch (param.takes_value) { - .None => &flags, - .One => &single_options, - .Many => &multi_options, + .none => &flags, + .one => &single_options, + .many => &multi_options, }; index = ptr.*; ptr.* += 1; @@ -67,11 +67,11 @@ pub fn ComptimeClap( const param = arg.param; if (param.names.long == null and param.names.short == null) { try pos.append(arg.value.?); - } else if (param.takes_value == .One) { + } else if (param.takes_value == .one) { debug.assert(res.single_options.len != 0); if (res.single_options.len != 0) res.single_options[param.id] = arg.value.?; - } else if (param.takes_value == .Many) { + } else if (param.takes_value == .many) { debug.assert(multis.len != 0); if (multis.len != 0) try multis[param.id].append(arg.value.?); @@ -97,7 +97,7 @@ pub fn ComptimeClap( pub fn flag(parser: @This(), comptime name: []const u8) bool { const param = comptime findParam(name); - if (param.takes_value != .None) + if (param.takes_value != .none) @compileError(name ++ " is an option and not a flag."); return parser.flags[param.id]; @@ -105,18 +105,18 @@ pub fn ComptimeClap( pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { const param = comptime findParam(name); - if (param.takes_value == .None) + if (param.takes_value == .none) @compileError(name ++ " is a flag and not an option."); - if (param.takes_value == .Many) + if (param.takes_value == .many) @compileError(name ++ " takes many options, not one."); return parser.single_options[param.id]; } pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 { const param = comptime findParam(name); - if (param.takes_value == .None) + if (param.takes_value == .none) @compileError(name ++ " is a flag and not an option."); - if (param.takes_value == .One) + if (param.takes_value == .one) @compileError(name ++ " takes one option, not multiple."); return parser.multi_options[param.id]; diff --git a/clap/streaming.zig b/clap/streaming.zig index 8030a67..a2a0ca8 100644 --- a/clap/streaming.zig +++ b/clap/streaming.zig @@ -69,7 +69,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { if (!mem.eql(u8, name, match)) continue; - if (param.takes_value == .None) { + if (param.takes_value == .none) { if (maybe_value != null) return parser.err(arg, .{ .long = name }, error.DoesntTakeValue); @@ -122,7 +122,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { // Before we return, we have to set the new state of the clap defer { - if (arg.len <= next_index or param.takes_value != .None) { + if (arg.len <= next_index or param.takes_value != .none) { parser.state = .normal; } else { parser.state = .{ @@ -135,7 +135,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { } const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false; - if (param.takes_value == .None) { + if (param.takes_value == .none) { if (next_is_eql) return parser.err(arg, .{ .short = short }, error.DoesntTakeValue); return Arg(Id){ .param = param }; @@ -235,9 +235,9 @@ fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, exp }; while (c.next() catch |err| { var buf: [1024]u8 = undefined; - var slice_stream = io.fixedBufferStream(&buf); - diag.report(slice_stream.outStream(), err) catch unreachable; - testing.expectEqualStrings(expected, slice_stream.getWritten()); + var fbs = io.fixedBufferStream(&buf); + diag.report(fbs.writer(), err) catch unreachable; + testing.expectEqualStrings(expected, fbs.getWritten()); return; }) |_| {} @@ -246,23 +246,17 @@ fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, exp test "short params" { const params = [_]clap.Param(u8){ - clap.Param(u8){ - .id = 0, - .names = clap.Names{ .short = 'a' }, - }, - clap.Param(u8){ - .id = 1, - .names = clap.Names{ .short = 'b' }, - }, - clap.Param(u8){ + .{ .id = 0, .names = .{ .short = 'a' } }, + .{ .id = 1, .names = .{ .short = 'b' } }, + .{ .id = 2, - .names = clap.Names{ .short = 'c' }, - .takes_value = .One, + .names = .{ .short = 'c' }, + .takes_value = .one, }, - clap.Param(u8){ + .{ .id = 3, - .names = clap.Names{ .short = 'd' }, - .takes_value = .Many, + .names = .{ .short = 'd' }, + .takes_value = .many, }, }; @@ -279,42 +273,36 @@ test "short params" { "0", "-ac=0", "-d=0", }, &[_]Arg(u8){ - Arg(u8){ .param = a }, - Arg(u8){ .param = b }, - Arg(u8){ .param = a }, - Arg(u8){ .param = b }, - Arg(u8){ .param = b }, - Arg(u8){ .param = a }, - Arg(u8){ .param = c, .value = "0" }, - Arg(u8){ .param = c, .value = "0" }, - Arg(u8){ .param = a }, - Arg(u8){ .param = c, .value = "0" }, - Arg(u8){ .param = a }, - Arg(u8){ .param = c, .value = "0" }, - Arg(u8){ .param = d, .value = "0" }, + .{ .param = a }, + .{ .param = b }, + .{ .param = a }, + .{ .param = b }, + .{ .param = b }, + .{ .param = a }, + .{ .param = c, .value = "0" }, + .{ .param = c, .value = "0" }, + .{ .param = a }, + .{ .param = c, .value = "0" }, + .{ .param = a }, + .{ .param = c, .value = "0" }, + .{ .param = d, .value = "0" }, }, ); } test "long params" { const params = [_]clap.Param(u8){ - clap.Param(u8){ - .id = 0, - .names = clap.Names{ .long = "aa" }, - }, - clap.Param(u8){ - .id = 1, - .names = clap.Names{ .long = "bb" }, - }, - clap.Param(u8){ + .{ .id = 0, .names = .{ .long = "aa" } }, + .{ .id = 1, .names = .{ .long = "bb" } }, + .{ .id = 2, - .names = clap.Names{ .long = "cc" }, - .takes_value = .One, + .names = .{ .long = "cc" }, + .takes_value = .one, }, - clap.Param(u8){ + .{ .id = 3, - .names = clap.Names{ .long = "dd" }, - .takes_value = .Many, + .names = .{ .long = "dd" }, + .takes_value = .many, }, }; @@ -331,59 +319,47 @@ test "long params" { "--cc=0", "--dd=0", }, &[_]Arg(u8){ - Arg(u8){ .param = aa }, - Arg(u8){ .param = bb }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = dd, .value = "0" }, + .{ .param = aa }, + .{ .param = bb }, + .{ .param = cc, .value = "0" }, + .{ .param = cc, .value = "0" }, + .{ .param = dd, .value = "0" }, }, ); } test "positional params" { - const params = [_]clap.Param(u8){clap.Param(u8){ + const params = [_]clap.Param(u8){.{ .id = 0, - .takes_value = .One, + .takes_value = .one, }}; testNoErr( ¶ms, &[_][]const u8{ "aa", "bb" }, &[_]Arg(u8){ - Arg(u8){ .param = ¶ms[0], .value = "aa" }, - Arg(u8){ .param = ¶ms[0], .value = "bb" }, + .{ .param = ¶ms[0], .value = "aa" }, + .{ .param = ¶ms[0], .value = "bb" }, }, ); } test "all params" { const params = [_]clap.Param(u8){ - clap.Param(u8){ + .{ .id = 0, - .names = clap.Names{ - .short = 'a', - .long = "aa", - }, + .names = .{ .short = 'a', .long = "aa" }, }, - clap.Param(u8){ + .{ .id = 1, - .names = clap.Names{ - .short = 'b', - .long = "bb", - }, + .names = .{ .short = 'b', .long = "bb" }, }, - clap.Param(u8){ + .{ .id = 2, - .names = clap.Names{ - .short = 'c', - .long = "cc", - }, - .takes_value = .One, - }, - clap.Param(u8){ - .id = 3, - .takes_value = .One, + .names = .{ .short = 'c', .long = "cc" }, + .takes_value = .one, }, + .{ .id = 3, .takes_value = .one }, }; const aa = ¶ms[0]; @@ -401,46 +377,40 @@ test "all params" { "-", "--", "--cc=0", "-a", }, &[_]Arg(u8){ - Arg(u8){ .param = aa }, - Arg(u8){ .param = bb }, - Arg(u8){ .param = aa }, - Arg(u8){ .param = bb }, - Arg(u8){ .param = bb }, - Arg(u8){ .param = aa }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = aa }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = aa }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = aa }, - Arg(u8){ .param = bb }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = cc, .value = "0" }, - Arg(u8){ .param = positional, .value = "something" }, - Arg(u8){ .param = positional, .value = "-" }, - Arg(u8){ .param = positional, .value = "--cc=0" }, - Arg(u8){ .param = positional, .value = "-a" }, + .{ .param = aa }, + .{ .param = bb }, + .{ .param = aa }, + .{ .param = bb }, + .{ .param = bb }, + .{ .param = aa }, + .{ .param = cc, .value = "0" }, + .{ .param = cc, .value = "0" }, + .{ .param = aa }, + .{ .param = cc, .value = "0" }, + .{ .param = aa }, + .{ .param = cc, .value = "0" }, + .{ .param = aa }, + .{ .param = bb }, + .{ .param = cc, .value = "0" }, + .{ .param = cc, .value = "0" }, + .{ .param = positional, .value = "something" }, + .{ .param = positional, .value = "-" }, + .{ .param = positional, .value = "--cc=0" }, + .{ .param = positional, .value = "-a" }, }, ); } test "errors" { const params = [_]clap.Param(u8){ - clap.Param(u8){ + .{ .id = 0, - .names = clap.Names{ - .short = 'a', - .long = "aa", - }, + .names = .{ .short = 'a', .long = "aa" }, }, - clap.Param(u8){ + .{ .id = 1, - .names = clap.Names{ - .short = 'c', - .long = "cc", - }, - .takes_value = .One, + .names = .{ .short = 'c', .long = "cc" }, + .takes_value = .one, }, }; testErr(¶ms, &[_][]const u8{"q"}, "Invalid argument 'q'\n"); diff --git a/example/help.zig b/example/help.zig index 2775177..3cf9e42 100644 --- a/example/help.zig +++ b/example/help.zig @@ -1,15 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr_file = std.io.getStdErr(); - var stderr_out_stream = stderr_file.outStream(); - // clap.help is a function that can print a simple help message, given a // slice of Param(Help). There is also a helpEx, which can print a // help message for any Param, but it is more verbose to call. try clap.help( - stderr_out_stream, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, diff --git a/example/simple-error.zig b/example/simple-error.zig index 3c62f0e..c04a9c6 100644 --- a/example/simple-error.zig +++ b/example/simple-error.zig @@ -1,12 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { const params = comptime [_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit.") catch unreachable, }; - var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); + var args = try clap.parse(clap.Help, ¶ms, .{}); defer args.deinit(); _ = args.flag("--helps"); diff --git a/example/simple-ex.zig b/example/simple-ex.zig index f504d63..f08751b 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -2,6 +2,7 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { const allocator = std.heap.page_allocator; @@ -29,7 +30,7 @@ pub fn main() !void { .diagnostic = &diag, }) catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }; defer args.deinit(); diff --git a/example/simple.zig b/example/simple.zig index 392dca3..69473fa 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -2,6 +2,7 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { // First we specify what parameters our program can take. @@ -19,7 +20,7 @@ pub fn main() !void { var diag = clap.Diagnostic{}; var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }; defer args.deinit(); diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index f8d873d..41efd1f 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -2,25 +2,23 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { const allocator = std.heap.page_allocator; // First we specify what parameters our program can take. const params = [_]clap.Param(u8){ - clap.Param(u8){ + .{ .id = 'h', - .names = clap.Names{ .short = 'h', .long = "help" }, + .names = .{ .short = 'h', .long = "help" }, }, - clap.Param(u8){ + .{ .id = 'n', - .names = clap.Names{ .short = 'n', .long = "number" }, - .takes_value = .One, - }, - clap.Param(u8){ - .id = 'f', - .takes_value = .One, + .names = .{ .short = 'n', .long = "number" }, + .takes_value = .one, }, + .{ .id = 'f', .takes_value = .one }, }; // We then initialize an argument iterator. We will use the OsIterator as it nicely @@ -41,7 +39,7 @@ pub fn main() !void { // Because we use a streaming parser, we have to consume each argument parsed individually. while (parser.next() catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }) |arg| { // arg.param will point to the parameter which matched the argument. diff --git a/example/usage.zig b/example/usage.zig index 25e1a34..e044f1d 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -1,14 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr = std.io.getStdErr().outStream(); - // clap.usage is a function that can print a simple usage message, given a // slice of Param(Help). There is also a usageEx, which can print a // usage message for any Param, but it is more verbose to call. try clap.usage( - stderr, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, -- cgit v1.2.3