Move things around a little
This commit is contained in:
parent
d441a6d044
commit
7d1d02f2a6
1 changed files with 19 additions and 15 deletions
|
|
@ -9,16 +9,22 @@ const Allocator = std.mem.Allocator;
|
||||||
pub const parsers = @import("./parsers/parsers.zig");
|
pub const parsers = @import("./parsers/parsers.zig");
|
||||||
|
|
||||||
const Arg = @import("./arg.zig").Arg;
|
const Arg = @import("./arg.zig").Arg;
|
||||||
|
const niceTypeName = @import("./utils.zig").niceTypeName;
|
||||||
|
|
||||||
const log = std.log.scoped(.args);
|
const log = std.log.scoped(.args);
|
||||||
|
|
||||||
/// Metadata for a field that is parseable by the argument parser.
|
/// Metadata for a field that is parseable by the argument parser.
|
||||||
pub const Extra = union(enum(u2)) {
|
pub const Extra = union(enum(u2)) {
|
||||||
Positional,
|
Positional: struct {
|
||||||
|
about: ?[]const u8 = null,
|
||||||
|
typeHint: ?[]const u8 = null,
|
||||||
|
},
|
||||||
Remainder,
|
Remainder,
|
||||||
Flag: struct {
|
Flag: struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
short: ?[]const u8 = null,
|
short: ?[]const u8 = null,
|
||||||
|
about: ?[]const u8 = null,
|
||||||
|
typeHint: ?[]const u8 = null,
|
||||||
toggle: bool = false,
|
toggle: bool = false,
|
||||||
takesValue: bool = false,
|
takesValue: bool = false,
|
||||||
},
|
},
|
||||||
|
|
@ -41,6 +47,12 @@ pub fn Marker(comptime T: type) type {
|
||||||
|
|
||||||
pub const help = @import("./help.zig");
|
pub const help = @import("./help.zig");
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
if (builtin.mode == .Debug) {
|
||||||
|
std.mem.doNotOptimizeAway(help);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <https://git.cutie.zone/lyssieth/zither/issues/1>
|
/// <https://git.cutie.zone/lyssieth/zither/issues/1>
|
||||||
pub fn parseArgs(comptime T: type, allocator: Allocator) !T {
|
pub fn parseArgs(comptime T: type, allocator: Allocator) !T {
|
||||||
const args = try std.process.argsAlloc(allocator);
|
const args = try std.process.argsAlloc(allocator);
|
||||||
|
|
@ -329,14 +341,6 @@ fn initFromParsed(comptime T: type, allocator: Allocator, flags: []Arg) !T {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn niceTypeName(comptime T: type) []const u8 {
|
|
||||||
if (T == []const u8) {
|
|
||||||
return "string";
|
|
||||||
}
|
|
||||||
|
|
||||||
return @typeName(T);
|
|
||||||
}
|
|
||||||
|
|
||||||
const t = std.testing;
|
const t = std.testing;
|
||||||
|
|
||||||
test "parse args" {
|
test "parse args" {
|
||||||
|
|
@ -367,7 +371,7 @@ test "parse args" {
|
||||||
positional: Marker([]const u8) = .{
|
positional: Marker([]const u8) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -460,7 +464,7 @@ test "missing positional because no args" {
|
||||||
positional: Marker([]const u8) = .{
|
positional: Marker([]const u8) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -485,7 +489,7 @@ test "missing positional because empty arg" {
|
||||||
positional: Marker([]const u8) = .{
|
positional: Marker([]const u8) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -517,7 +521,7 @@ test "parse fn (positional)" {
|
||||||
flag: Marker(u16) = .{
|
flag: Marker(u16) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
.parse = parsers.num(u16),
|
.parse = parsers.num(u16),
|
||||||
},
|
},
|
||||||
|
|
@ -525,7 +529,7 @@ test "parse fn (positional)" {
|
||||||
boolean: Marker(bool) = .{
|
boolean: Marker(bool) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
.parse = parsers.boolean,
|
.parse = parsers.boolean,
|
||||||
},
|
},
|
||||||
|
|
@ -533,7 +537,7 @@ test "parse fn (positional)" {
|
||||||
enumeration: Marker(DemoEnum) = .{
|
enumeration: Marker(DemoEnum) = .{
|
||||||
.value = undefined,
|
.value = undefined,
|
||||||
.extra = Extra{
|
.extra = Extra{
|
||||||
.Positional = {},
|
.Positional = .{},
|
||||||
},
|
},
|
||||||
.parse = parsers.enumLiteral(DemoEnum),
|
.parse = parsers.enumLiteral(DemoEnum),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue