Compare commits
2 commits
c06ae87cbc
...
80d398f540
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80d398f540 | ||
|
|
701da7746d |
4 changed files with 67 additions and 56 deletions
|
|
@ -66,7 +66,7 @@ pub fn parseArgs(comptime T: type, allocator: Allocator) !T {
|
|||
/// Do not pass the process name as an argument.
|
||||
///
|
||||
/// Parsing order of arguments is based on the order they are declared in `T`.
|
||||
pub fn parseArgsFromSlice(comptime T: type, allocator: Allocator, args: [][]const u8) !T {
|
||||
pub fn parseArgsFromSlice(comptime T: type, allocator: Allocator, args: [][:0]const u8) !T {
|
||||
var flags = try std.ArrayList(Arg).initCapacity(allocator, 4);
|
||||
defer flags.deinit(allocator);
|
||||
|
||||
|
|
@ -383,7 +383,7 @@ fn initFromParsed(comptime T: type, allocator: Allocator, flags: []Arg) !T {
|
|||
const t = std.testing;
|
||||
|
||||
test "parse args" {
|
||||
const args = try t.allocator.alloc([]const u8, 4);
|
||||
const args = try t.allocator.alloc([:0]const u8, 4);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
const Demo = struct {
|
||||
|
|
@ -433,7 +433,7 @@ test "parse args" {
|
|||
}
|
||||
|
||||
test "missing flag" {
|
||||
const args = try t.allocator.alloc([]const u8, 1);
|
||||
const args = try t.allocator.alloc([:0]const u8, 1);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "1234";
|
||||
|
|
@ -461,7 +461,7 @@ test "missing flag" {
|
|||
}
|
||||
|
||||
test "missing toggle" {
|
||||
const args = try t.allocator.alloc([]const u8, 1);
|
||||
const args = try t.allocator.alloc([:0]const u8, 1);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "1234";
|
||||
|
|
@ -489,7 +489,7 @@ test "missing toggle" {
|
|||
}
|
||||
|
||||
test "missing positional because no args" {
|
||||
const args = try t.allocator.alloc([]const u8, 0);
|
||||
const args = try t.allocator.alloc([:0]const u8, 0);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
const Demo = struct {
|
||||
|
|
@ -511,7 +511,7 @@ test "missing positional because no args" {
|
|||
}
|
||||
|
||||
test "missing positional because empty arg" {
|
||||
const args = try t.allocator.alloc([]const u8, 1);
|
||||
const args = try t.allocator.alloc([:0]const u8, 1);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "";
|
||||
|
|
@ -535,7 +535,7 @@ test "missing positional because empty arg" {
|
|||
}
|
||||
|
||||
test "positional has default value so we get a free pass" {
|
||||
const args = try t.allocator.alloc([]const u8, 1);
|
||||
const args = try t.allocator.alloc([:0]const u8, 1);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "--toggle";
|
||||
|
|
@ -562,7 +562,7 @@ test "positional has default value so we get a free pass" {
|
|||
}
|
||||
|
||||
test "parse fn (positional)" {
|
||||
const args = try t.allocator.alloc([]const u8, 3);
|
||||
const args = try t.allocator.alloc([:0]const u8, 3);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "1234";
|
||||
|
|
@ -612,7 +612,7 @@ test "parse fn (positional)" {
|
|||
}
|
||||
|
||||
test "parse fn (flag)" {
|
||||
const args = try t.allocator.alloc([]const u8, 3);
|
||||
const args = try t.allocator.alloc([:0]const u8, 3);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "--number=1234";
|
||||
|
|
@ -671,7 +671,7 @@ test "parse fn (flag)" {
|
|||
}
|
||||
|
||||
test "parse failure because no args" {
|
||||
const args = try t.allocator.alloc([]const u8, 0);
|
||||
const args = try t.allocator.alloc([:0]const u8, 0);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
const Demo = struct {
|
||||
|
|
@ -688,7 +688,7 @@ test "parse failure because no args" {
|
|||
}
|
||||
|
||||
test "remainder has value" {
|
||||
const args = try t.allocator.alloc([]const u8, 3);
|
||||
const args = try t.allocator.alloc([:0]const u8, 3);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "--flag=value";
|
||||
|
|
@ -721,7 +721,7 @@ test "remainder has value" {
|
|||
}
|
||||
|
||||
test "sub command from remainder" {
|
||||
const args = try t.allocator.alloc([]const u8, 3);
|
||||
const args = try t.allocator.alloc([:0]const u8, 3);
|
||||
defer t.allocator.free(args);
|
||||
|
||||
args[0] = "--flag=value";
|
||||
|
|
@ -785,7 +785,13 @@ test "sub command from remainder" {
|
|||
}
|
||||
};
|
||||
|
||||
var innerResult = try parseArgsFromSlice(DemoInner, t.allocator, outerResult.remainder.value.items);
|
||||
const outerItems = outerResult.remainder.value.items;
|
||||
|
||||
var innerResult = try parseArgsFromSlice(
|
||||
DemoInner,
|
||||
t.allocator,
|
||||
@ptrCast(outerItems),
|
||||
);
|
||||
defer innerResult.deinit();
|
||||
|
||||
try t.expectEqualStrings("value", innerResult.flag.value);
|
||||
|
|
|
|||
|
|
@ -8,19 +8,19 @@ const Extra = arg_lib.Extra;
|
|||
|
||||
const niceTypeName = @import("../util/utils.zig").niceTypeName;
|
||||
|
||||
pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.AnyWriter) !void {
|
||||
pub fn printHelp(comptime T: type, comptime name: []const u8, writer: *std.Io.Writer) !void {
|
||||
const info = @typeInfo(T);
|
||||
|
||||
switch (info) {
|
||||
.Struct => {},
|
||||
.@"struct" => {},
|
||||
else => {
|
||||
log.warn("We only support printing the help of `Struct`s, not {s}", .{@tagName(info)});
|
||||
log.warn("We only support printing the help of structs, not {s}", .{@tagName(info)});
|
||||
return error.NotImplemented;
|
||||
},
|
||||
}
|
||||
|
||||
var hasFlags = false;
|
||||
inline for (info.Struct.fields) |field| {
|
||||
inline for (info.@"struct".fields) |field| {
|
||||
if (std.mem.eql(u8, field.name, "allocator")) {
|
||||
comptime continue;
|
||||
}
|
||||
|
|
@ -32,13 +32,13 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
}
|
||||
|
||||
var hasPositionals = false;
|
||||
inline for (info.Struct.fields) |field| {
|
||||
inline for (info.@"struct".fields) |field| {
|
||||
if (std.mem.eql(u8, field.name, "allocator")) {
|
||||
comptime continue;
|
||||
}
|
||||
|
||||
const valueOpaque = field.default_value orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @alignCast(@ptrCast(valueOpaque));
|
||||
const valueOpaque = field.default_value_ptr orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @ptrCast(@alignCast(valueOpaque));
|
||||
const value: Extra = @field(valueMarker, "extra");
|
||||
|
||||
switch (value) {
|
||||
|
|
@ -51,13 +51,13 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
}
|
||||
|
||||
var hasRemainder = false;
|
||||
inline for (info.Struct.fields) |field| {
|
||||
inline for (info.@"struct".fields) |field| {
|
||||
if (std.mem.eql(u8, field.name, "allocator")) {
|
||||
comptime continue;
|
||||
}
|
||||
|
||||
const valueOpaque = field.default_value orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @alignCast(@ptrCast(valueOpaque));
|
||||
const valueOpaque = field.default_value_ptr orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @ptrCast(@alignCast(valueOpaque));
|
||||
const value: Extra = @field(valueMarker, "extra");
|
||||
|
||||
switch (value) {
|
||||
|
|
@ -76,13 +76,13 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
}
|
||||
|
||||
if (hasPositionals) {
|
||||
inline for (info.Struct.fields) |field| {
|
||||
inline for (info.@"struct".fields) |field| {
|
||||
if (std.mem.eql(u8, field.name, "allocator")) {
|
||||
comptime continue;
|
||||
}
|
||||
|
||||
const valueOpaque = field.default_value orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @alignCast(@ptrCast(valueOpaque));
|
||||
const valueOpaque = field.default_value_ptr orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @ptrCast(@alignCast(valueOpaque));
|
||||
const value: Extra = @field(valueMarker, "extra");
|
||||
|
||||
switch (value) {
|
||||
|
|
@ -101,13 +101,13 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
try writer.print("\n", .{});
|
||||
try writer.print("Legend: <required> [optional]\n\n", .{});
|
||||
|
||||
inline for (info.Struct.fields) |field| {
|
||||
inline for (info.@"struct".fields) |field| {
|
||||
if (std.mem.eql(u8, field.name, "allocator")) {
|
||||
comptime continue;
|
||||
}
|
||||
|
||||
const valueOpaque = field.default_value orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @alignCast(@ptrCast(valueOpaque));
|
||||
const valueOpaque = field.default_value_ptr orelse @panic("Missing default value for field " ++ field.name);
|
||||
const valueMarker: *const field.type = @ptrCast(@alignCast(valueOpaque));
|
||||
const valueType = niceTypeName(@TypeOf(valueMarker.*.value));
|
||||
|
||||
const isOptional = std.mem.startsWith(u8, valueType, "?");
|
||||
|
|
@ -122,7 +122,7 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
valueType,
|
||||
});
|
||||
|
||||
if (pos.typeHint) |typeHint| {
|
||||
if (pos.type_hint) |typeHint| {
|
||||
try writer.print(" ({s})", .{typeHint});
|
||||
}
|
||||
|
||||
|
|
@ -137,24 +137,24 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
flag.name,
|
||||
});
|
||||
|
||||
if (flag.takesValue) {
|
||||
if (flag.takes_value) {
|
||||
try writer.print("=<value>", .{});
|
||||
}
|
||||
|
||||
if (flag.short) |short| {
|
||||
try writer.print(" (-{s}", .{short});
|
||||
|
||||
if (flag.takesValue) {
|
||||
if (flag.takes_value) {
|
||||
try writer.print("=<value>", .{});
|
||||
}
|
||||
|
||||
try writer.print(")", .{});
|
||||
}
|
||||
|
||||
if (flag.takesValue) {
|
||||
if (flag.takes_value) {
|
||||
try writer.print(": {s}", .{valueType});
|
||||
|
||||
if (flag.typeHint) |typeHint| {
|
||||
if (flag.type_hint) |typeHint| {
|
||||
try writer.print(" ({s})", .{typeHint});
|
||||
}
|
||||
}
|
||||
|
|
@ -175,25 +175,22 @@ pub fn printHelp(comptime T: type, comptime name: []const u8, writer: std.io.Any
|
|||
|
||||
const t = std.testing;
|
||||
test "empty help" {
|
||||
var buf = std.ArrayList(u8).init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
const Demo = struct {};
|
||||
|
||||
try printHelp(Demo, "demo", buf.writer().any());
|
||||
var buf = std.Io.Writer.Allocating.init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
try printHelp(Demo, "demo", &buf.writer);
|
||||
|
||||
try t.expectEqualStrings(
|
||||
\\Usage: demo
|
||||
\\Legend: <required> [optional]
|
||||
\\
|
||||
\\
|
||||
, buf.items);
|
||||
, buf.written());
|
||||
}
|
||||
|
||||
test "basic help" {
|
||||
var buf = std.ArrayList(u8).init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
const Demo = struct {
|
||||
verbose: Marker(bool) = .{
|
||||
.value = undefined,
|
||||
|
|
@ -217,7 +214,14 @@ test "basic help" {
|
|||
},
|
||||
};
|
||||
|
||||
try printHelp(Demo, "demo", buf.writer().any());
|
||||
var buf = std.Io.Writer.Allocating.init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
try printHelp(
|
||||
Demo,
|
||||
"demo",
|
||||
&buf.writer,
|
||||
);
|
||||
|
||||
try t.expectEqualStrings(
|
||||
\\Usage: demo [flags] <positional> [...]
|
||||
|
|
@ -226,28 +230,32 @@ test "basic help" {
|
|||
\\* --verbose (-v)
|
||||
\\* <positional>: string
|
||||
\\
|
||||
, buf.items);
|
||||
, buf.written());
|
||||
}
|
||||
|
||||
test "about and type hint" {
|
||||
var buf = std.ArrayList(u8).init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
const Demo = struct {
|
||||
verbose: Marker(bool) = .{
|
||||
.value = undefined,
|
||||
.extra = .{ .Flag = .{
|
||||
.name = "verbose",
|
||||
.short = "v",
|
||||
.takesValue = true,
|
||||
.takes_value = true,
|
||||
|
||||
.about = "makes the output verbose",
|
||||
.typeHint = "yes/no",
|
||||
.type_hint = "yes/no",
|
||||
} },
|
||||
},
|
||||
};
|
||||
|
||||
try printHelp(Demo, "demo", buf.writer().any());
|
||||
var buf = std.Io.Writer.Allocating.init(t.allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
try printHelp(
|
||||
Demo,
|
||||
"demo",
|
||||
&buf.writer,
|
||||
);
|
||||
|
||||
try t.expectEqualStrings(
|
||||
\\Usage: demo [flags]
|
||||
|
|
@ -255,5 +263,5 @@ test "about and type hint" {
|
|||
\\
|
||||
\\* --verbose=<value> (-v=<value>): bool (yes/no) <required> | makes the output verbose
|
||||
\\
|
||||
, buf.items);
|
||||
, buf.written());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ comptime {
|
|||
|
||||
if (builtin.is_test) {
|
||||
std.mem.doNotOptimizeAway(args);
|
||||
std.mem.doNotOptimizeAway(args.help);
|
||||
std.mem.doNotOptimizeAway(args.parsers);
|
||||
|
||||
std.mem.doNotOptimizeAway(log);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,10 +7,6 @@ pub fn niceTypeName(comptime T: type) []const u8 {
|
|||
|
||||
const name = @typeName(T);
|
||||
|
||||
if (std.mem.startsWith(u8, name, "array_list.Aligned")) {
|
||||
return "array";
|
||||
}
|
||||
|
||||
if (std.mem.lastIndexOf(u8, name, ".")) |idx| {
|
||||
return name[idx + 1 ..];
|
||||
}
|
||||
|
|
@ -27,7 +23,6 @@ test "nice type names" {
|
|||
};
|
||||
|
||||
try t.expectEqualStrings("string", niceTypeName([]const u8));
|
||||
try t.expectEqualStrings("array", niceTypeName(std.ArrayList(u8)));
|
||||
try t.expectEqualStrings("Enum", niceTypeName(Enum));
|
||||
|
||||
try t.expectEqualStrings("u8", niceTypeName(u8));
|
||||
|
|
|
|||
Loading…
Reference in a new issue