2020-03-04 22:36:06 +01:00
|
|
|
const std = @import("std");
|
|
|
|
|
2020-03-05 14:31:35 +01:00
|
|
|
/// Parses arguments for the given specification and our current process.
|
|
|
|
/// - `Spec` is the configuration of the arguments.
|
|
|
|
/// - `allocator` is the allocator that is used to allocate all required memory
|
2021-05-12 10:24:50 +02:00
|
|
|
/// - `error_handling` defines how parser errors will be handled.
|
2023-05-28 11:46:46 +02:00
|
|
|
pub fn parseForCurrentProcess(comptime Spec: type, allocator: std.mem.Allocator, comptime error_handling: ErrorHandling) !ParseArgsResult(Spec, null) {
|
2022-02-02 00:16:20 +01:00
|
|
|
// Use argsWithAllocator for portability.
|
|
|
|
// All data allocated by the ArgIterator is freed at the end of the function.
|
|
|
|
// Data returned to the user is always duplicated using the allocator.
|
|
|
|
var args = try std.process.argsWithAllocator(allocator);
|
|
|
|
defer args.deinit();
|
2020-03-05 14:31:35 +01:00
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
const executable_name = args.next() orelse {
|
2021-05-12 10:24:50 +02:00
|
|
|
try error_handling.process(error.NoExecutableName, Error{
|
|
|
|
.option = "",
|
|
|
|
.kind = .missing_executable_name,
|
|
|
|
});
|
|
|
|
|
|
|
|
// we do not assume any more arguments appear here anyways...
|
2020-03-05 14:31:35 +01:00
|
|
|
return error.NoExecutableName;
|
2021-12-23 09:17:24 +01:00
|
|
|
};
|
2020-03-05 14:31:35 +01:00
|
|
|
|
2021-08-27 23:47:45 +02:00
|
|
|
var result = try parseInternal(Spec, null, &args, allocator, error_handling);
|
2020-03-05 14:31:35 +01:00
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
result.executable_name = try allocator.dupeZ(u8, executable_name);
|
2020-03-05 14:31:35 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:03:13 +02:00
|
|
|
/// Parses arguments for the given specification and our current process.
|
2020-03-05 14:31:35 +01:00
|
|
|
/// - `Spec` is the configuration of the arguments.
|
2021-08-27 17:03:13 +02:00
|
|
|
/// - `allocator` is the allocator that is used to allocate all required memory
|
|
|
|
/// - `error_handling` defines how parser errors will be handled.
|
2023-05-28 11:46:46 +02:00
|
|
|
pub fn parseWithVerbForCurrentProcess(comptime Spec: type, comptime Verb: type, allocator: std.mem.Allocator, comptime error_handling: ErrorHandling) !ParseArgsResult(Spec, Verb) {
|
2022-02-02 00:16:20 +01:00
|
|
|
// Use argsWithAllocator for portability.
|
|
|
|
// All data allocated by the ArgIterator is freed at the end of the function.
|
|
|
|
// Data returned to the user is always duplicated using the allocator.
|
|
|
|
var args = try std.process.argsWithAllocator(allocator);
|
|
|
|
defer args.deinit();
|
2021-08-27 17:03:13 +02:00
|
|
|
|
2022-02-05 18:06:32 +01:00
|
|
|
const executable_name = args.next() orelse {
|
2021-08-27 17:03:13 +02:00
|
|
|
try error_handling.process(error.NoExecutableName, Error{
|
|
|
|
.option = "",
|
|
|
|
.kind = .missing_executable_name,
|
|
|
|
});
|
|
|
|
|
|
|
|
// we do not assume any more arguments appear here anyways...
|
|
|
|
return error.NoExecutableName;
|
2021-12-23 09:17:24 +01:00
|
|
|
};
|
2021-08-27 17:03:13 +02:00
|
|
|
|
2021-08-27 23:47:45 +02:00
|
|
|
var result = try parseInternal(Spec, Verb, &args, allocator, error_handling);
|
2021-08-27 17:03:13 +02:00
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
result.executable_name = try allocator.dupeZ(u8, executable_name);
|
2021-08-27 17:03:13 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses arguments for the given specification.
|
|
|
|
/// - `Generic` is the configuration of the arguments.
|
|
|
|
/// - `args_iterator` is a pointer to an std.process.ArgIterator that will yield the command line arguments.
|
2020-03-05 14:31:35 +01:00
|
|
|
/// - `allocator` is the allocator that is used to allocate all required memory
|
2021-05-12 10:24:50 +02:00
|
|
|
/// - `error_handling` defines how parser errors will be handled.
|
2020-03-05 14:31:35 +01:00
|
|
|
///
|
2020-05-17 03:46:42 +02:00
|
|
|
/// Note that `.executable_name` in the result will not be set!
|
2023-05-28 11:46:46 +02:00
|
|
|
pub fn parse(comptime Generic: type, args_iterator: anytype, allocator: std.mem.Allocator, comptime error_handling: ErrorHandling) !ParseArgsResult(Generic, null) {
|
2021-08-27 17:03:13 +02:00
|
|
|
return parseInternal(Generic, null, args_iterator, allocator, error_handling);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses arguments for the given specification using a `Verb` method.
|
|
|
|
/// This means that the first positional argument is interpreted as a verb, that can
|
|
|
|
/// be considered a sub-command that provides more specific options.
|
|
|
|
/// - `Generic` is the configuration of the arguments.
|
|
|
|
/// - `Verb` is the configuration of the verbs.
|
|
|
|
/// - `args_iterator` is a pointer to an std.process.ArgIterator that will yield the command line arguments.
|
|
|
|
/// - `allocator` is the allocator that is used to allocate all required memory
|
|
|
|
/// - `error_handling` defines how parser errors will be handled.
|
|
|
|
///
|
|
|
|
/// Note that `.executable_name` in the result will not be set!
|
2023-05-28 11:46:46 +02:00
|
|
|
pub fn parseWithVerb(comptime Generic: type, comptime Verb: type, args_iterator: anytype, allocator: std.mem.Allocator, comptime error_handling: ErrorHandling) !ParseArgsResult(Generic, Verb) {
|
2021-08-27 17:03:13 +02:00
|
|
|
return parseInternal(Generic, Verb, args_iterator, allocator, error_handling);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as parse, but with anytype argument for testability
|
2023-05-28 11:46:46 +02:00
|
|
|
fn parseInternal(comptime Generic: type, comptime MaybeVerb: ?type, args_iterator: anytype, allocator: std.mem.Allocator, comptime error_handling: ErrorHandling) !ParseArgsResult(Generic, MaybeVerb) {
|
2021-08-27 17:03:13 +02:00
|
|
|
var result = ParseArgsResult(Generic, MaybeVerb){
|
2020-03-04 22:36:06 +01:00
|
|
|
.arena = std.heap.ArenaAllocator.init(allocator),
|
2021-08-27 17:03:13 +02:00
|
|
|
.options = Generic{},
|
|
|
|
.verb = if (MaybeVerb != null) null else {}, // no verb by default
|
2020-03-05 14:31:35 +01:00
|
|
|
.positionals = undefined,
|
2020-05-17 03:46:42 +02:00
|
|
|
.executable_name = null,
|
2020-03-04 22:36:06 +01:00
|
|
|
};
|
|
|
|
errdefer result.arena.deinit();
|
2021-12-05 19:16:50 +01:00
|
|
|
var result_arena_allocator = result.arena.allocator();
|
2020-03-04 22:36:06 +01:00
|
|
|
|
2021-08-11 13:10:50 +02:00
|
|
|
var arglist = std.ArrayList([:0]const u8).init(allocator);
|
2022-12-01 21:05:12 +01:00
|
|
|
defer arglist.deinit();
|
2020-03-04 22:36:06 +01:00
|
|
|
|
2021-05-12 10:24:50 +02:00
|
|
|
var last_error: ?anyerror = null;
|
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
while (args_iterator.next()) |item| {
|
2020-03-04 22:36:06 +01:00
|
|
|
if (std.mem.startsWith(u8, item, "--")) {
|
|
|
|
if (std.mem.eql(u8, item, "--")) {
|
|
|
|
// double hyphen is considered 'everything from here now is positional'
|
2022-03-11 17:19:44 +01:00
|
|
|
result.raw_start_index = arglist.items.len;
|
2020-03-04 22:36:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-03-04 22:58:12 +01:00
|
|
|
|
|
|
|
const Pair = struct {
|
|
|
|
name: []const u8,
|
|
|
|
value: ?[]const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const pair = if (std.mem.indexOf(u8, item, "=")) |index|
|
|
|
|
Pair{
|
|
|
|
.name = item[2..index],
|
|
|
|
.value = item[index + 1 ..],
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Pair{
|
|
|
|
.name = item[2..],
|
|
|
|
.value = null,
|
|
|
|
};
|
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
var found = false;
|
2021-08-27 17:03:13 +02:00
|
|
|
inline for (std.meta.fields(Generic)) |fld| {
|
2020-03-04 22:58:12 +01:00
|
|
|
if (std.mem.eql(u8, pair.name, fld.name)) {
|
2021-12-05 19:16:50 +01:00
|
|
|
try parseOption(Generic, result_arena_allocator, &result.options, args_iterator, error_handling, &last_error, fld.name, pair.value);
|
2020-03-04 22:36:06 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:03:13 +02:00
|
|
|
if (MaybeVerb) |Verb| {
|
|
|
|
if (result.verb) |*verb| {
|
|
|
|
if (!found) {
|
|
|
|
const Tag = std.meta.Tag(Verb);
|
|
|
|
inline for (std.meta.fields(Verb)) |verb_info| {
|
|
|
|
if (verb.* == @field(Tag, verb_info.name)) {
|
2022-12-21 11:20:53 +01:00
|
|
|
if (comptime canHaveFieldsAndIsNotZeroSized(verb_info.type)) {
|
|
|
|
inline for (std.meta.fields(verb_info.type)) |fld| {
|
2022-08-14 21:32:41 +02:00
|
|
|
if (std.mem.eql(u8, pair.name, fld.name)) {
|
|
|
|
try parseOption(
|
2022-12-21 11:20:53 +01:00
|
|
|
verb_info.type,
|
2022-08-14 21:32:41 +02:00
|
|
|
result_arena_allocator,
|
|
|
|
&@field(verb.*, verb_info.name),
|
|
|
|
args_iterator,
|
|
|
|
error_handling,
|
|
|
|
&last_error,
|
|
|
|
fld.name,
|
|
|
|
pair.value,
|
|
|
|
);
|
|
|
|
found = true;
|
|
|
|
}
|
2021-08-27 17:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
if (!found) {
|
2021-05-12 10:24:50 +02:00
|
|
|
last_error = error.EncounteredUnknownArgument;
|
|
|
|
try error_handling.process(error.EncounteredUnknownArgument, Error{
|
|
|
|
.option = pair.name,
|
|
|
|
.kind = .unknown,
|
|
|
|
});
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
} else if (std.mem.startsWith(u8, item, "-")) {
|
|
|
|
if (std.mem.eql(u8, item, "-")) {
|
|
|
|
// single hyphen is considered a positional argument
|
2022-02-02 00:16:20 +01:00
|
|
|
try arglist.append(try result_arena_allocator.dupeZ(u8, item));
|
2020-03-04 22:36:06 +01:00
|
|
|
} else {
|
2021-08-27 20:16:19 +02:00
|
|
|
var any_shorthands = false;
|
2023-02-23 04:21:22 +01:00
|
|
|
for (item[1..], 0..) |char, index| {
|
2021-08-27 20:16:19 +02:00
|
|
|
var option_name = [2]u8{ '-', char };
|
|
|
|
var found = false;
|
|
|
|
if (@hasDecl(Generic, "shorthands")) {
|
|
|
|
any_shorthands = true;
|
2021-08-27 17:03:13 +02:00
|
|
|
inline for (std.meta.fields(@TypeOf(Generic.shorthands))) |fld| {
|
2020-03-04 22:36:06 +01:00
|
|
|
if (fld.name.len != 1)
|
|
|
|
@compileError("All shorthand fields must be exactly one character long!");
|
|
|
|
if (fld.name[0] == char) {
|
2021-08-27 17:03:13 +02:00
|
|
|
const real_name = @field(Generic.shorthands, fld.name);
|
2021-01-04 11:37:36 +01:00
|
|
|
const real_fld_type = @TypeOf(@field(result.options, real_name));
|
2020-03-04 22:36:06 +01:00
|
|
|
|
|
|
|
// -2 because we stripped of the "-" at the beginning
|
2021-01-04 11:37:36 +01:00
|
|
|
if (requiresArg(real_fld_type) and index != item.len - 2) {
|
2021-05-12 10:24:50 +02:00
|
|
|
last_error = error.EncounteredUnexpectedArgument;
|
|
|
|
try error_handling.process(error.EncounteredUnexpectedArgument, Error{
|
|
|
|
.option = &option_name,
|
|
|
|
.kind = .invalid_placement,
|
|
|
|
});
|
|
|
|
} else {
|
2021-12-05 19:16:50 +01:00
|
|
|
try parseOption(Generic, result_arena_allocator, &result.options, args_iterator, error_handling, &last_error, real_name, null);
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MaybeVerb) |Verb| {
|
|
|
|
if (result.verb) |*verb| {
|
|
|
|
if (!found) {
|
|
|
|
const Tag = std.meta.Tag(Verb);
|
|
|
|
inline for (std.meta.fields(Verb)) |verb_info| {
|
2022-12-21 11:20:53 +01:00
|
|
|
const VerbType = verb_info.type;
|
2022-08-14 21:32:41 +02:00
|
|
|
if (comptime canHaveFieldsAndIsNotZeroSized(VerbType)) {
|
|
|
|
if (verb.* == @field(Tag, verb_info.name)) {
|
|
|
|
const target_value = &@field(verb.*, verb_info.name);
|
|
|
|
if (@hasDecl(VerbType, "shorthands")) {
|
|
|
|
any_shorthands = true;
|
|
|
|
inline for (std.meta.fields(@TypeOf(VerbType.shorthands))) |fld| {
|
|
|
|
if (fld.name.len != 1)
|
|
|
|
@compileError("All shorthand fields must be exactly one character long!");
|
|
|
|
if (fld.name[0] == char) {
|
|
|
|
const real_name = @field(VerbType.shorthands, fld.name);
|
|
|
|
const real_fld_type = @TypeOf(@field(target_value.*, real_name));
|
|
|
|
|
|
|
|
// -2 because we stripped of the "-" at the beginning
|
|
|
|
if (requiresArg(real_fld_type) and index != item.len - 2) {
|
|
|
|
last_error = error.EncounteredUnexpectedArgument;
|
|
|
|
try error_handling.process(error.EncounteredUnexpectedArgument, Error{
|
|
|
|
.option = &option_name,
|
|
|
|
.kind = .invalid_placement,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
try parseOption(VerbType, result_arena_allocator, target_value, args_iterator, error_handling, &last_error, real_name, null);
|
|
|
|
}
|
|
|
|
last_error = null; // we need to reset that error here, as it was set previously
|
|
|
|
found = true;
|
2021-08-27 20:16:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-27 20:16:19 +02:00
|
|
|
if (!found) {
|
|
|
|
last_error = error.EncounteredUnknownArgument;
|
|
|
|
try error_handling.process(error.EncounteredUnknownArgument, Error{
|
|
|
|
.option = &option_name,
|
|
|
|
.kind = .unknown,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!any_shorthands) {
|
2021-05-12 10:24:50 +02:00
|
|
|
try error_handling.process(error.EncounteredUnsupportedArgument, Error{
|
|
|
|
.option = item,
|
|
|
|
.kind = .unsupported,
|
|
|
|
});
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-27 17:03:13 +02:00
|
|
|
if (MaybeVerb) |Verb| {
|
2021-08-27 19:03:26 +02:00
|
|
|
if (result.verb == null) {
|
2021-08-27 17:03:13 +02:00
|
|
|
inline for (std.meta.fields(Verb)) |fld| {
|
|
|
|
if (std.mem.eql(u8, item, fld.name)) {
|
|
|
|
// found active verb, default-initialize it
|
2022-12-21 11:20:53 +01:00
|
|
|
result.verb = @unionInit(Verb, fld.name, fld.type{});
|
2021-08-27 17:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.verb == null) {
|
|
|
|
try error_handling.process(error.EncounteredUnknownVerb, Error{
|
|
|
|
.option = "verb",
|
|
|
|
.kind = .unsupported,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
try arglist.append(try result_arena_allocator.dupeZ(u8, item));
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:24:50 +02:00
|
|
|
if (last_error != null)
|
|
|
|
return error.InvalidArguments;
|
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
// This will consume the rest of the arguments as positional ones.
|
|
|
|
// Only executes when the above loop is broken.
|
2022-02-02 00:16:20 +01:00
|
|
|
while (args_iterator.next()) |item| {
|
|
|
|
try arglist.append(try result_arena_allocator.dupeZ(u8, item));
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 21:05:12 +01:00
|
|
|
result.positionals = try arglist.toOwnedSlice();
|
2020-03-04 22:36:06 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:32:41 +02:00
|
|
|
fn canHaveFieldsAndIsNotZeroSized(comptime T: type) bool {
|
|
|
|
return switch (@typeInfo(T)) {
|
2024-08-30 02:37:56 +02:00
|
|
|
.@"struct", .@"union", .@"enum", .error_set => @sizeOf(T) != 0,
|
2022-08-14 21:32:41 +02:00
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:31:35 +01:00
|
|
|
/// The return type of the argument parser.
|
2021-08-27 17:03:13 +02:00
|
|
|
pub fn ParseArgsResult(comptime Generic: type, comptime MaybeVerb: ?type) type {
|
2024-08-30 02:37:56 +02:00
|
|
|
if (@typeInfo(Generic) != .@"struct")
|
2021-08-27 17:03:13 +02:00
|
|
|
@compileError("Generic argument definition must be a struct");
|
|
|
|
|
|
|
|
if (MaybeVerb) |Verb| {
|
2022-09-17 19:51:29 +02:00
|
|
|
const ti: std.builtin.Type = @typeInfo(Verb);
|
2024-08-30 02:37:56 +02:00
|
|
|
if (ti != .@"union" or ti.@"union".tag_type == null)
|
2021-08-27 17:03:13 +02:00
|
|
|
@compileError("Verb must be a tagged union");
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
2021-07-14 09:00:53 +02:00
|
|
|
/// Exports the type of options.
|
2021-08-27 17:03:13 +02:00
|
|
|
pub const GenericOptions = Generic;
|
2021-09-15 08:58:14 +02:00
|
|
|
pub const Verbs = MaybeVerb orelse void;
|
2021-07-14 09:00:53 +02:00
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
arena: std.heap.ArenaAllocator,
|
2020-03-05 14:31:35 +01:00
|
|
|
|
|
|
|
/// The options with either default or set values.
|
2021-08-27 17:03:13 +02:00
|
|
|
options: Generic,
|
|
|
|
|
|
|
|
/// The verb that was parsed or `null` if no first positional was provided.
|
|
|
|
/// Is `void` when verb parsing is disabled
|
|
|
|
verb: if (MaybeVerb) |Verb| ?Verb else void,
|
2020-03-05 14:31:35 +01:00
|
|
|
|
|
|
|
/// The positional arguments that were passed to the process.
|
2021-08-11 13:10:50 +02:00
|
|
|
positionals: [][:0]const u8,
|
2020-03-05 14:31:35 +01:00
|
|
|
|
2022-03-11 17:19:44 +01:00
|
|
|
// The index of the first "raw arg", meaning the first arg after "--"
|
|
|
|
raw_start_index: ?usize = null,
|
|
|
|
|
2020-03-05 14:31:35 +01:00
|
|
|
/// Name of the executable file (or: zeroth argument)
|
2021-08-11 13:10:50 +02:00
|
|
|
executable_name: ?[:0]const u8,
|
2020-03-04 22:36:06 +01:00
|
|
|
|
2020-05-17 03:46:42 +02:00
|
|
|
pub fn deinit(self: Self) void {
|
2020-03-05 14:31:35 +01:00
|
|
|
self.arena.child_allocator.free(self.positionals);
|
|
|
|
|
2020-05-17 03:46:42 +02:00
|
|
|
if (self.executable_name) |n|
|
2020-03-05 14:31:35 +01:00
|
|
|
self.arena.child_allocator.free(n);
|
|
|
|
|
2020-03-04 22:36:06 +01:00
|
|
|
self.arena.deinit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:51:43 +01:00
|
|
|
/// Returns true if the given type requires an argument to be parsed.
|
2020-03-04 22:36:06 +01:00
|
|
|
fn requiresArg(comptime T: type) bool {
|
|
|
|
const H = struct {
|
|
|
|
fn doesArgTypeRequireArg(comptime Type: type) bool {
|
|
|
|
if (Type == []const u8)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return switch (@as(std.builtin.TypeId, @typeInfo(Type))) {
|
2024-08-30 02:37:56 +02:00
|
|
|
.int, .float, .@"enum" => true,
|
|
|
|
.bool => false,
|
|
|
|
.@"struct", .@"union" => true,
|
|
|
|
.pointer => true,
|
2020-03-04 22:36:06 +01:00
|
|
|
else => @compileError(@typeName(Type) ++ " is not a supported argument type!"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ti = @typeInfo(T);
|
2024-08-30 02:37:56 +02:00
|
|
|
if (ti == .optional) {
|
|
|
|
return H.doesArgTypeRequireArg(ti.optional.child);
|
2020-03-04 22:36:06 +01:00
|
|
|
} else {
|
|
|
|
return H.doesArgTypeRequireArg(T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:51:43 +01:00
|
|
|
/// Parses a boolean option.
|
|
|
|
fn parseBoolean(str: []const u8) !bool {
|
2024-04-26 17:07:10 +02:00
|
|
|
return switch (str.len) {
|
|
|
|
1 => switch (str[0]) {
|
|
|
|
'y', 'Y', 't', 'T' => true,
|
|
|
|
'n', 'N', 'f', 'F' => false,
|
|
|
|
else => error.NotABooleanValue,
|
|
|
|
},
|
|
|
|
2 => if (std.ascii.eqlIgnoreCase("no", str)) false else error.NotABooleanValue,
|
|
|
|
3 => if (std.ascii.eqlIgnoreCase("yes", str)) true else error.NotABooleanValue,
|
|
|
|
4 => if (std.ascii.eqlIgnoreCase("true", str)) true else error.NotABooleanValue,
|
|
|
|
5 => if (std.ascii.eqlIgnoreCase("false", str)) false else error.NotABooleanValue,
|
|
|
|
else => error.NotABooleanValue,
|
|
|
|
};
|
2020-03-05 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 11:50:39 +01:00
|
|
|
/// Parses an int option.
|
|
|
|
fn parseInt(comptime T: type, str: []const u8) !T {
|
|
|
|
var buf = str;
|
|
|
|
var multiplier: T = 1;
|
|
|
|
|
2021-01-23 12:04:51 +01:00
|
|
|
if (buf.len != 0) {
|
2021-01-23 11:50:39 +01:00
|
|
|
var base1024 = false;
|
2021-05-12 10:24:50 +02:00
|
|
|
if (std.ascii.toLower(buf[buf.len - 1]) == 'i') { //ki vs k for instance
|
2021-01-23 11:50:39 +01:00
|
|
|
buf.len -= 1;
|
|
|
|
base1024 = true;
|
|
|
|
}
|
2021-01-23 12:04:51 +01:00
|
|
|
if (buf.len != 0) {
|
2023-11-21 10:06:35 +01:00
|
|
|
const pow: u3 = switch (buf[buf.len - 1]) {
|
2021-05-12 10:24:50 +02:00
|
|
|
'k', 'K' => 1, //kilo
|
|
|
|
'm', 'M' => 2, //mega
|
|
|
|
'g', 'G' => 3, //giga
|
|
|
|
't', 'T' => 4, //tera
|
|
|
|
'p', 'P' => 5, //peta
|
|
|
|
else => 0,
|
2021-01-23 11:50:39 +01:00
|
|
|
};
|
2021-05-12 10:24:50 +02:00
|
|
|
|
2021-01-23 12:09:37 +01:00
|
|
|
if (pow != 0) {
|
2021-01-23 11:50:39 +01:00
|
|
|
buf.len -= 1;
|
|
|
|
|
|
|
|
if (comptime std.math.maxInt(T) < 1024)
|
|
|
|
return error.Overflow;
|
2023-11-21 10:06:35 +01:00
|
|
|
const base: T = if (base1024) 1024 else 1000;
|
2023-06-27 11:25:55 +02:00
|
|
|
multiplier = try std.math.powi(T, base, @as(T, @intCast(pow)));
|
2021-01-23 11:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-30 02:37:56 +02:00
|
|
|
const ret: T = switch (@typeInfo(T).int.signedness) {
|
2021-01-23 11:50:39 +01:00
|
|
|
.signed => try std.fmt.parseInt(T, buf, 0),
|
|
|
|
.unsigned => try std.fmt.parseUnsigned(T, buf, 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
return try std.math.mul(T, ret, multiplier);
|
|
|
|
}
|
|
|
|
|
2021-01-23 12:04:51 +01:00
|
|
|
test "parseInt" {
|
|
|
|
const tst = std.testing;
|
|
|
|
|
2021-06-25 23:24:34 +02:00
|
|
|
try tst.expectEqual(@as(i32, 50), try parseInt(i32, "50"));
|
|
|
|
try tst.expectEqual(@as(i32, 6000), try parseInt(i32, "6k"));
|
|
|
|
try tst.expectEqual(@as(u32, 2048), try parseInt(u32, "0x2KI"));
|
|
|
|
try tst.expectEqual(@as(i8, 0), try parseInt(i8, "0"));
|
|
|
|
try tst.expectEqual(@as(usize, 10_000_000_000), try parseInt(usize, "0xAg"));
|
|
|
|
try tst.expectError(error.Overflow, parseInt(i2, "1m"));
|
|
|
|
try tst.expectError(error.Overflow, parseInt(u16, "1Ti"));
|
2021-01-23 12:04:51 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:51:43 +01:00
|
|
|
/// Converts an argument value to the target type.
|
2021-12-05 19:16:50 +01:00
|
|
|
fn convertArgumentValue(comptime T: type, allocator: std.mem.Allocator, textInput: []const u8) !T {
|
2020-03-04 22:36:06 +01:00
|
|
|
switch (@typeInfo(T)) {
|
2024-08-30 02:37:56 +02:00
|
|
|
.optional => |opt| return try convertArgumentValue(opt.child, allocator, textInput),
|
|
|
|
.bool => if (textInput.len > 0)
|
2020-03-05 14:51:43 +01:00
|
|
|
return try parseBoolean(textInput)
|
|
|
|
else
|
|
|
|
return true, // boolean options are always true
|
2024-08-30 02:37:56 +02:00
|
|
|
.int => return try parseInt(T, textInput),
|
|
|
|
.float => return try std.fmt.parseFloat(T, textInput),
|
|
|
|
.@"enum" => {
|
2020-07-05 16:54:26 +02:00
|
|
|
if (@hasDecl(T, "parse")) {
|
|
|
|
return try T.parse(textInput);
|
|
|
|
} else {
|
|
|
|
return std.meta.stringToEnum(T, textInput) orelse return error.InvalidEnumeration;
|
|
|
|
}
|
|
|
|
},
|
2024-08-30 02:37:56 +02:00
|
|
|
.@"struct", .@"union" => {
|
2020-07-05 16:54:26 +02:00
|
|
|
if (@hasDecl(T, "parse")) {
|
|
|
|
return try T.parse(textInput);
|
|
|
|
} else {
|
|
|
|
@compileError(@typeName(T) ++ " has no public visible `fn parse([]const u8) !T`!");
|
|
|
|
}
|
|
|
|
},
|
2024-08-30 02:37:56 +02:00
|
|
|
.pointer => |ptr| switch (ptr.size) {
|
2021-10-11 00:24:01 +02:00
|
|
|
.Slice => {
|
|
|
|
if (ptr.child != u8) {
|
|
|
|
@compileError(@typeName(T) ++ " is not a supported pointer type, only slices of u8 are supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type contains a sentinel dupe the text input to a new buffer.
|
|
|
|
// This is equivalent to allocator.dupeZ but works with any sentinel.
|
|
|
|
if (comptime std.meta.sentinel(T)) |sentinel| {
|
|
|
|
const data = try allocator.alloc(u8, textInput.len + 1);
|
2024-01-04 22:57:33 +01:00
|
|
|
@memcpy(data[0..textInput.len], textInput);
|
2021-10-11 00:24:01 +02:00
|
|
|
data[textInput.len] = sentinel;
|
|
|
|
|
|
|
|
return data[0..textInput.len :sentinel];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the type is []const u8 so just return the text input.
|
|
|
|
return textInput;
|
|
|
|
},
|
|
|
|
else => @compileError(@typeName(T) ++ " is not a supported pointer type!"),
|
|
|
|
},
|
2020-03-04 22:36:06 +01:00
|
|
|
else => @compileError(@typeName(T) ++ " is not a supported argument type!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:51:43 +01:00
|
|
|
/// Parses an option value into the correct type.
|
2020-03-04 22:58:12 +01:00
|
|
|
fn parseOption(
|
|
|
|
comptime Spec: type,
|
2021-12-05 19:16:50 +01:00
|
|
|
arena: std.mem.Allocator,
|
2021-08-27 17:03:13 +02:00
|
|
|
target_struct: *Spec,
|
|
|
|
args: anytype,
|
2023-05-28 11:46:46 +02:00
|
|
|
comptime error_handling: ErrorHandling,
|
2021-05-12 10:24:50 +02:00
|
|
|
last_error: *?anyerror,
|
2020-03-05 14:51:43 +01:00
|
|
|
/// The name of the option that is currently parsed.
|
2020-03-04 22:58:12 +01:00
|
|
|
comptime name: []const u8,
|
2020-03-05 14:51:43 +01:00
|
|
|
/// Optional pre-defined value for options that use `--foo=bar`
|
2020-03-04 22:58:12 +01:00
|
|
|
value: ?[]const u8,
|
|
|
|
) !void {
|
2021-08-27 17:03:13 +02:00
|
|
|
const field_type = @TypeOf(@field(target_struct, name));
|
2020-03-05 14:51:43 +01:00
|
|
|
|
2022-02-02 00:16:20 +01:00
|
|
|
const final_value = if (value) |val| blk: {
|
|
|
|
// use the literal value
|
|
|
|
const res = try arena.dupeZ(u8, val);
|
|
|
|
break :blk res;
|
|
|
|
} else if (requiresArg(field_type)) blk: {
|
2021-05-12 10:24:50 +02:00
|
|
|
// fetch from parser
|
2022-02-08 11:24:40 +01:00
|
|
|
const val = args.next();
|
|
|
|
if (val == null or std.mem.eql(u8, val.?, "--")) {
|
2021-05-12 10:24:50 +02:00
|
|
|
last_error.* = error.MissingArgument;
|
|
|
|
try error_handling.process(error.MissingArgument, Error{
|
|
|
|
.option = "--" ++ name,
|
|
|
|
.kind = .missing_argument,
|
2020-03-04 22:58:12 +01:00
|
|
|
});
|
2021-05-12 10:24:50 +02:00
|
|
|
return;
|
2022-02-08 11:24:40 +01:00
|
|
|
}
|
2022-02-02 00:16:20 +01:00
|
|
|
|
2022-02-08 11:24:40 +01:00
|
|
|
const res = try arena.dupeZ(u8, val.?);
|
2022-02-02 00:16:20 +01:00
|
|
|
break :blk res;
|
|
|
|
} else blk: {
|
2021-05-12 10:24:50 +02:00
|
|
|
// argument is "empty"
|
2022-02-02 00:16:20 +01:00
|
|
|
break :blk "";
|
|
|
|
};
|
2021-05-12 10:24:50 +02:00
|
|
|
|
2021-10-11 00:24:01 +02:00
|
|
|
@field(target_struct, name) = convertArgumentValue(field_type, arena, final_value) catch |err| {
|
2021-05-12 10:24:50 +02:00
|
|
|
last_error.* = err;
|
|
|
|
try error_handling.process(err, Error{
|
|
|
|
.option = "--" ++ name,
|
|
|
|
.kind = .{ .invalid_value = final_value },
|
|
|
|
});
|
|
|
|
// we couldn't parse the value, so we return a undefined value as we have signalled an
|
|
|
|
// error and won't return this anyways.
|
2022-10-22 12:50:18 +02:00
|
|
|
return;
|
2021-05-12 10:24:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A collection of errors that were encountered while parsing arguments.
|
|
|
|
pub const ErrorCollection = struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
arena: std.heap.ArenaAllocator,
|
|
|
|
list: std.ArrayList(Error),
|
2020-03-04 22:36:06 +01:00
|
|
|
|
2021-12-05 19:16:50 +01:00
|
|
|
pub fn init(allocator: std.mem.Allocator) Self {
|
2021-05-12 10:24:50 +02:00
|
|
|
return Self{
|
|
|
|
.arena = std.heap.ArenaAllocator.init(allocator),
|
|
|
|
.list = std.ArrayList(Error).init(allocator),
|
2020-03-04 22:36:06 +01:00
|
|
|
};
|
2021-05-12 10:24:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
self.list.deinit();
|
|
|
|
self.arena.deinit();
|
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the current enumeration of errors.
|
|
|
|
pub fn errors(self: Self) []const Error {
|
|
|
|
return self.list.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends an error to the collection
|
|
|
|
fn insert(self: *Self, err: Error) !void {
|
2023-11-21 10:06:35 +01:00
|
|
|
const dupe = Error{
|
2021-12-05 19:16:50 +01:00
|
|
|
.option = try self.arena.allocator().dupe(u8, err.option),
|
2021-05-12 10:24:50 +02:00
|
|
|
.kind = switch (err.kind) {
|
|
|
|
.invalid_value => |v| Error.Kind{
|
2021-12-05 19:16:50 +01:00
|
|
|
.invalid_value = try self.arena.allocator().dupe(u8, v),
|
2021-05-12 10:24:50 +02:00
|
|
|
},
|
|
|
|
// flat copy
|
2021-08-27 17:03:13 +02:00
|
|
|
.unknown, .out_of_memory, .unsupported, .invalid_placement, .missing_argument, .missing_executable_name, .unknown_verb => err.kind,
|
2021-05-12 10:24:50 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
try self.list.append(dupe);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An argument parsing error.
|
|
|
|
pub const Error = struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
/// The option that yielded the error
|
|
|
|
option: []const u8,
|
|
|
|
|
|
|
|
/// The kind of error, might include additional information
|
|
|
|
kind: Kind,
|
|
|
|
|
|
|
|
pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
|
2021-06-25 23:24:34 +02:00
|
|
|
_ = fmt;
|
|
|
|
_ = options;
|
2021-05-12 10:24:50 +02:00
|
|
|
switch (self.kind) {
|
|
|
|
.unknown => try writer.print("The option {s} does not exist", .{self.option}),
|
|
|
|
.invalid_value => |value| try writer.print("Invalid value '{s}' for option {s}", .{ value, self.option }),
|
|
|
|
.out_of_memory => try writer.print("Out of memory while parsing option {s}", .{self.option}),
|
|
|
|
.unsupported => try writer.writeAll("Short command line options are not supported."),
|
|
|
|
.invalid_placement => try writer.writeAll("An option with argument must be the last option for short command line options."),
|
|
|
|
.missing_argument => try writer.print("Missing argument for option {s}", .{self.option}),
|
|
|
|
|
|
|
|
.missing_executable_name => try writer.writeAll("Failed to get executable name from the argument list!"),
|
2021-08-27 17:03:13 +02:00
|
|
|
.unknown_verb => try writer.print("Unknown verb '{s}'.", .{self.option}),
|
2021-05-12 10:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 03:25:16 +01:00
|
|
|
pub const Kind = union(enum) {
|
2021-05-12 10:24:50 +02:00
|
|
|
/// When the argument itself is unknown
|
|
|
|
unknown,
|
|
|
|
|
|
|
|
/// When the parsing of an argument value failed
|
|
|
|
invalid_value: []const u8,
|
|
|
|
|
|
|
|
/// When the parsing of an argument value triggered a out of memory error
|
|
|
|
out_of_memory,
|
|
|
|
|
|
|
|
/// When the argument is a short argument and no shorthands are enabled
|
|
|
|
unsupported,
|
|
|
|
|
|
|
|
/// Can only happen when a shorthand for an option requires an argument, but is followed by more shorthands.
|
|
|
|
invalid_placement,
|
|
|
|
|
|
|
|
/// An option was passed that requires an argument, but the option was passed last.
|
|
|
|
missing_argument,
|
|
|
|
|
|
|
|
/// This error has an empty option name and can only happen when parsing the argument list for a process.
|
|
|
|
missing_executable_name,
|
2021-08-27 17:03:13 +02:00
|
|
|
|
|
|
|
/// This error has the verb as an option name and will happen when a verb is provided that is not known.
|
|
|
|
unknown_verb,
|
2021-05-12 10:24:50 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The error handling method that should be used.
|
|
|
|
pub const ErrorHandling = union(enum) {
|
|
|
|
const Self = @This();
|
|
|
|
|
2021-10-11 00:24:01 +02:00
|
|
|
/// Do not print or process any errors, just
|
2021-05-12 10:24:50 +02:00
|
|
|
/// return a fitting error on the first argument mismatch.
|
|
|
|
silent,
|
|
|
|
|
|
|
|
/// Print errors to stderr and return a `error.InvalidArguments`.
|
|
|
|
print,
|
|
|
|
|
|
|
|
/// Collect errors into the error collection and return
|
|
|
|
/// `error.InvalidArguments` when any error was encountered.
|
|
|
|
collect: *ErrorCollection,
|
|
|
|
|
2023-05-28 11:46:46 +02:00
|
|
|
/// Forwards the parsing error to a functionm
|
|
|
|
forward: fn (err: Error) anyerror!void,
|
|
|
|
|
2021-05-12 10:24:50 +02:00
|
|
|
/// Processes an error with the given handling method.
|
2023-05-28 11:46:46 +02:00
|
|
|
fn process(comptime self: Self, src_error: anytype, err: Error) !void {
|
2024-08-30 02:37:56 +02:00
|
|
|
if (@typeInfo(@TypeOf(src_error)) != .error_set)
|
2021-05-12 10:24:50 +02:00
|
|
|
@compileError("src_error must be a error union!");
|
|
|
|
switch (self) {
|
|
|
|
.silent => return src_error,
|
|
|
|
.print => try std.io.getStdErr().writer().print("{}\n", .{err}),
|
|
|
|
.collect => |collection| try collection.insert(err),
|
2023-05-28 11:46:46 +02:00
|
|
|
.forward => |func| try func(err),
|
2021-05-12 10:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test {
|
|
|
|
std.testing.refAllDecls(@This());
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 10:24:50 +02:00
|
|
|
test "ErrorCollection" {
|
|
|
|
var option_buf = "option".*;
|
|
|
|
var invalid_buf = "invalid".*;
|
|
|
|
|
|
|
|
var ec = ErrorCollection.init(std.testing.allocator);
|
|
|
|
defer ec.deinit();
|
|
|
|
|
|
|
|
try ec.insert(Error{
|
|
|
|
.option = &option_buf,
|
|
|
|
.kind = .{ .invalid_value = &invalid_buf },
|
2020-03-04 22:36:06 +01:00
|
|
|
});
|
2021-05-12 10:24:50 +02:00
|
|
|
|
|
|
|
option_buf = undefined;
|
|
|
|
invalid_buf = undefined;
|
|
|
|
|
2021-06-25 23:24:34 +02:00
|
|
|
try std.testing.expectEqualStrings("option", ec.errors()[0].option);
|
|
|
|
try std.testing.expectEqualStrings("invalid", ec.errors()[0].kind.invalid_value);
|
2020-03-04 22:36:06 +01:00
|
|
|
}
|
2021-08-27 18:21:59 +02:00
|
|
|
|
|
|
|
const TestIterator = struct {
|
|
|
|
sequence: []const [:0]const u8,
|
|
|
|
index: usize = 0,
|
|
|
|
|
|
|
|
pub fn init(items: []const [:0]const u8) TestIterator {
|
|
|
|
return TestIterator{ .sequence = items };
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:17:28 +01:00
|
|
|
pub fn next(self: *@This()) ?[:0]const u8 {
|
2021-08-27 18:21:59 +02:00
|
|
|
if (self.index >= self.sequence.len)
|
|
|
|
return null;
|
2022-02-02 00:17:28 +01:00
|
|
|
const result = self.sequence[self.index];
|
2021-08-27 18:21:59 +02:00
|
|
|
self.index += 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const TestEnum = enum { default, special, slow, fast };
|
|
|
|
|
|
|
|
const TestGenericOptions = struct {
|
|
|
|
output: ?[]const u8 = null,
|
|
|
|
@"with-offset": bool = false,
|
|
|
|
@"with-hexdump": bool = false,
|
|
|
|
@"intermix-source": bool = false,
|
|
|
|
numberOfBytes: ?i32 = null,
|
|
|
|
signed_number: ?i64 = null,
|
|
|
|
unsigned_number: ?u64 = null,
|
|
|
|
mode: TestEnum = .default,
|
|
|
|
|
|
|
|
// This declares short-hand options for single hyphen
|
|
|
|
pub const shorthands = .{
|
|
|
|
.S = "intermix-source",
|
|
|
|
.b = "with-hexdump",
|
|
|
|
.O = "with-offset",
|
|
|
|
.o = "output",
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-08-27 19:03:26 +02:00
|
|
|
const TestVerb = union(enum) {
|
|
|
|
magic: MagicOptions,
|
|
|
|
booze: BoozeOptions,
|
|
|
|
|
|
|
|
const MagicOptions = struct { invoke: bool = false };
|
2021-08-27 20:16:19 +02:00
|
|
|
const BoozeOptions = struct {
|
|
|
|
cocktail: bool = false,
|
|
|
|
longdrink: bool = false,
|
|
|
|
|
|
|
|
pub const shorthands = .{
|
|
|
|
.c = "cocktail",
|
|
|
|
.l = "longdrink",
|
|
|
|
};
|
|
|
|
};
|
2021-08-27 19:03:26 +02:00
|
|
|
};
|
|
|
|
|
2021-08-27 18:21:59 +02:00
|
|
|
test "basic parsing (no verbs)" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
|
|
|
"--output",
|
|
|
|
"foobar",
|
|
|
|
"--with-offset",
|
|
|
|
"--numberOfBytes",
|
|
|
|
"-250",
|
|
|
|
"--unsigned_number",
|
|
|
|
"0xFF00FF",
|
|
|
|
"positional 1",
|
|
|
|
"--mode",
|
|
|
|
"special",
|
|
|
|
"positional 2",
|
|
|
|
});
|
2021-08-27 20:16:19 +02:00
|
|
|
var args = try parseInternal(TestGenericOptions, null, &titerator, std.testing.allocator, .print);
|
2021-08-27 18:21:59 +02:00
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?[:0]const u8, null), args.executable_name);
|
|
|
|
try std.testing.expect(void == @TypeOf(args.verb));
|
|
|
|
try std.testing.expectEqual(@as(usize, 2), args.positionals.len);
|
|
|
|
try std.testing.expectEqualStrings("positional 1", args.positionals[0]);
|
|
|
|
try std.testing.expectEqualStrings("positional 2", args.positionals[1]);
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings("foobar", args.options.output.?);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i32, -250), args.options.numberOfBytes);
|
|
|
|
try std.testing.expectEqual(@as(?u64, 0xFF00FF), args.options.unsigned_number);
|
|
|
|
try std.testing.expectEqual(TestEnum.special, args.options.mode);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i64, null), args.options.signed_number);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, args.options.@"with-offset");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"with-hexdump");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"intermix-source");
|
|
|
|
}
|
|
|
|
|
|
|
|
test "shorthand parsing (no verbs)" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
|
|
|
"-o",
|
|
|
|
"foobar",
|
|
|
|
"-O",
|
|
|
|
"--numberOfBytes",
|
|
|
|
"-250",
|
|
|
|
"--unsigned_number",
|
|
|
|
"0xFF00FF",
|
|
|
|
"positional 1",
|
|
|
|
"--mode",
|
|
|
|
"special",
|
|
|
|
"positional 2",
|
|
|
|
});
|
2021-08-27 20:16:19 +02:00
|
|
|
var args = try parseInternal(TestGenericOptions, null, &titerator, std.testing.allocator, .print);
|
2021-08-27 18:21:59 +02:00
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?[:0]const u8, null), args.executable_name);
|
|
|
|
try std.testing.expect(void == @TypeOf(args.verb));
|
|
|
|
try std.testing.expectEqual(@as(usize, 2), args.positionals.len);
|
|
|
|
try std.testing.expectEqualStrings("positional 1", args.positionals[0]);
|
|
|
|
try std.testing.expectEqualStrings("positional 2", args.positionals[1]);
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings("foobar", args.options.output.?);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i32, -250), args.options.numberOfBytes);
|
|
|
|
try std.testing.expectEqual(@as(?u64, 0xFF00FF), args.options.unsigned_number);
|
|
|
|
try std.testing.expectEqual(TestEnum.special, args.options.mode);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i64, null), args.options.signed_number);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, args.options.@"with-offset");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"with-hexdump");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"intermix-source");
|
|
|
|
}
|
2021-08-27 19:03:26 +02:00
|
|
|
|
|
|
|
test "basic parsing (with verbs)" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
2022-08-14 21:32:41 +02:00
|
|
|
"--output", // non-verb options can come before or after verb
|
2021-08-27 19:03:26 +02:00
|
|
|
"foobar",
|
2022-02-10 10:30:32 +01:00
|
|
|
"booze", // verb
|
2021-08-27 19:03:26 +02:00
|
|
|
"--with-offset",
|
|
|
|
"--numberOfBytes",
|
|
|
|
"-250",
|
|
|
|
"--unsigned_number",
|
|
|
|
"0xFF00FF",
|
|
|
|
"positional 1",
|
|
|
|
"--mode",
|
|
|
|
"special",
|
|
|
|
"positional 2",
|
|
|
|
"--cocktail",
|
|
|
|
});
|
2021-08-27 20:16:19 +02:00
|
|
|
var args = try parseInternal(TestGenericOptions, TestVerb, &titerator, std.testing.allocator, .print);
|
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?[:0]const u8, null), args.executable_name);
|
|
|
|
try std.testing.expect(?TestVerb == @TypeOf(args.verb));
|
|
|
|
try std.testing.expectEqual(@as(usize, 2), args.positionals.len);
|
|
|
|
try std.testing.expectEqualStrings("positional 1", args.positionals[0]);
|
|
|
|
try std.testing.expectEqualStrings("positional 2", args.positionals[1]);
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings("foobar", args.options.output.?);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i32, -250), args.options.numberOfBytes);
|
|
|
|
try std.testing.expectEqual(@as(?u64, 0xFF00FF), args.options.unsigned_number);
|
|
|
|
try std.testing.expectEqual(TestEnum.special, args.options.mode);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i64, null), args.options.signed_number);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, args.options.@"with-offset");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"with-hexdump");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"intermix-source");
|
|
|
|
|
|
|
|
try std.testing.expect(args.verb.? == .booze);
|
|
|
|
|
|
|
|
const booze = args.verb.?.booze;
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, booze.cocktail);
|
|
|
|
try std.testing.expectEqual(false, booze.longdrink);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "shorthand parsing (with verbs)" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
|
|
|
"booze", // verb
|
|
|
|
"-o",
|
|
|
|
"foobar",
|
|
|
|
"-O",
|
|
|
|
"--numberOfBytes",
|
|
|
|
"-250",
|
|
|
|
"--unsigned_number",
|
|
|
|
"0xFF00FF",
|
|
|
|
"positional 1",
|
|
|
|
"--mode",
|
|
|
|
"special",
|
|
|
|
"positional 2",
|
|
|
|
"-c", // --cocktail
|
|
|
|
});
|
|
|
|
var args = try parseInternal(TestGenericOptions, TestVerb, &titerator, std.testing.allocator, .print);
|
2021-08-27 19:03:26 +02:00
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?[:0]const u8, null), args.executable_name);
|
|
|
|
try std.testing.expect(?TestVerb == @TypeOf(args.verb));
|
|
|
|
try std.testing.expectEqual(@as(usize, 2), args.positionals.len);
|
|
|
|
try std.testing.expectEqualStrings("positional 1", args.positionals[0]);
|
|
|
|
try std.testing.expectEqualStrings("positional 2", args.positionals[1]);
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings("foobar", args.options.output.?);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i32, -250), args.options.numberOfBytes);
|
|
|
|
try std.testing.expectEqual(@as(?u64, 0xFF00FF), args.options.unsigned_number);
|
|
|
|
try std.testing.expectEqual(TestEnum.special, args.options.mode);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?i64, null), args.options.signed_number);
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, args.options.@"with-offset");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"with-hexdump");
|
|
|
|
try std.testing.expectEqual(false, args.options.@"intermix-source");
|
|
|
|
|
|
|
|
try std.testing.expect(args.verb.? == .booze);
|
|
|
|
|
|
|
|
const booze = args.verb.?.booze;
|
|
|
|
|
|
|
|
try std.testing.expectEqual(true, booze.cocktail);
|
|
|
|
try std.testing.expectEqual(false, booze.longdrink);
|
|
|
|
}
|
2021-10-11 00:24:01 +02:00
|
|
|
|
|
|
|
test "strings with sentinel" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
|
|
|
"--output",
|
|
|
|
"foobar",
|
|
|
|
});
|
|
|
|
var args = try parseInternal(
|
|
|
|
struct {
|
|
|
|
output: ?[:0]const u8 = null,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
&titerator,
|
|
|
|
std.testing.allocator,
|
|
|
|
.print,
|
|
|
|
);
|
|
|
|
defer args.deinit();
|
|
|
|
|
|
|
|
try std.testing.expectEqual(@as(?[:0]const u8, null), args.executable_name);
|
|
|
|
try std.testing.expect(void == @TypeOf(args.verb));
|
|
|
|
try std.testing.expectEqual(@as(usize, 0), args.positionals.len);
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings("foobar", args.options.output.?);
|
|
|
|
}
|
2022-02-08 11:24:40 +01:00
|
|
|
|
|
|
|
test "option argument --" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{
|
|
|
|
"--output",
|
|
|
|
"--",
|
|
|
|
});
|
|
|
|
|
|
|
|
try std.testing.expectError(error.MissingArgument, parseInternal(
|
|
|
|
struct {
|
|
|
|
output: ?[:0]const u8 = null,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
&titerator,
|
|
|
|
std.testing.allocator,
|
|
|
|
.silent,
|
|
|
|
));
|
2022-03-11 17:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
test "index of raw indicator --" {
|
|
|
|
var titerator = TestIterator.init(&[_][:0]const u8{ "stdin", "-", "--", "not-stdin", "-", "--" });
|
|
|
|
|
|
|
|
var args = try parseInternal(
|
|
|
|
struct {},
|
|
|
|
null,
|
|
|
|
&titerator,
|
|
|
|
std.testing.allocator,
|
|
|
|
.print,
|
|
|
|
);
|
|
|
|
defer args.deinit();
|
2022-02-08 11:24:40 +01:00
|
|
|
|
2022-03-11 17:19:44 +01:00
|
|
|
try std.testing.expectEqual(args.raw_start_index, 2);
|
|
|
|
try std.testing.expectEqual(args.positionals.len, 5);
|
2022-02-08 11:24:40 +01:00
|
|
|
}
|
2023-07-25 18:29:01 +02:00
|
|
|
|
2024-01-04 22:56:55 +01:00
|
|
|
fn reserved_argument(arg: []const u8) bool {
|
2023-07-25 18:29:01 +02:00
|
|
|
return std.mem.eql(u8, arg, "shorthands") or std.mem.eql(u8, arg, "meta");
|
|
|
|
}
|
|
|
|
|
2023-07-26 16:11:46 +02:00
|
|
|
pub fn printHelp(comptime Generic: type, name: []const u8, writer: anytype) !void {
|
2023-07-25 18:29:01 +02:00
|
|
|
if (!@hasDecl(Generic, "meta")) {
|
|
|
|
@compileError("Missing meta declaration in Generic");
|
|
|
|
}
|
|
|
|
|
2023-07-25 23:26:56 +02:00
|
|
|
const Meta = @TypeOf(Generic.meta);
|
2023-07-25 18:29:01 +02:00
|
|
|
|
2023-08-31 22:08:16 +02:00
|
|
|
try writer.print("Usage: {s}", .{name});
|
2023-07-25 23:26:56 +02:00
|
|
|
|
2023-08-31 22:14:48 +02:00
|
|
|
if (@hasField(Meta, "usage_summary")) {
|
|
|
|
try writer.print(" {s}", .{Generic.meta.usage_summary});
|
2023-07-25 18:29:01 +02:00
|
|
|
}
|
2023-07-25 21:37:55 +02:00
|
|
|
try writer.print("\n\n", .{});
|
2023-07-25 18:29:01 +02:00
|
|
|
|
2023-07-25 23:26:56 +02:00
|
|
|
if (@hasField(Meta, "full_text")) {
|
2023-07-25 18:29:01 +02:00
|
|
|
try writer.print("{s}\n\n", .{Generic.meta.full_text});
|
|
|
|
}
|
|
|
|
|
2023-07-25 23:26:56 +02:00
|
|
|
if (@hasField(Meta, "option_docs")) {
|
2023-07-25 18:29:01 +02:00
|
|
|
const fields = std.meta.fields(Generic);
|
|
|
|
|
|
|
|
try writer.print("Options:\n", .{});
|
2023-07-25 23:09:46 +02:00
|
|
|
comptime var maxOptionLength = 0;
|
2023-07-25 18:29:01 +02:00
|
|
|
inline for (fields) |field| {
|
|
|
|
if (!reserved_argument(field.name)) {
|
|
|
|
if (!@hasField(@TypeOf(Generic.meta.option_docs), field.name)) {
|
|
|
|
@compileError("option_docs not specified for field: " ++ field.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field.name.len > maxOptionLength) {
|
|
|
|
maxOptionLength = field.name.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline for (fields) |field| {
|
|
|
|
if (!reserved_argument(field.name)) {
|
|
|
|
if (@hasDecl(Generic, "shorthands")) {
|
|
|
|
var foundShorthand = false;
|
|
|
|
inline for (std.meta.fields(@TypeOf(Generic.shorthands))) |shorthand| {
|
|
|
|
const option = @field(Generic.shorthands, shorthand.name);
|
|
|
|
if (std.mem.eql(u8, option, field.name)) {
|
|
|
|
try writer.print(" -{s}, ", .{shorthand.name});
|
|
|
|
foundShorthand = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundShorthand)
|
|
|
|
try writer.print(" ", .{});
|
|
|
|
}
|
2024-03-19 01:09:35 +01:00
|
|
|
if (@hasDecl(Generic, "wrap_len")) {
|
2024-06-18 18:55:14 +02:00
|
|
|
var it = std.mem.splitScalar(u8, @field(Generic.meta.option_docs, field.name), ' ');
|
2024-03-19 01:09:35 +01:00
|
|
|
const threshold = Generic.wrap_len;
|
|
|
|
var line_len: usize = 0;
|
|
|
|
var newline = false;
|
|
|
|
var first = true;
|
|
|
|
while (it.next()) |word| {
|
|
|
|
if (first) {
|
|
|
|
const fmtString = std.fmt.comptimePrint("--{{s: <{}}} {{s}}", .{maxOptionLength});
|
|
|
|
try writer.print(fmtString, .{ field.name, word });
|
|
|
|
first = false;
|
|
|
|
} else if (newline) {
|
|
|
|
const fmtString = std.fmt.comptimePrint("\n{{s: <{}}} {{s}}", .{maxOptionLength + 10});
|
|
|
|
try writer.print(fmtString, .{ " ", word });
|
|
|
|
newline = false;
|
|
|
|
} else {
|
|
|
|
try writer.print(" {s}", .{word});
|
|
|
|
}
|
|
|
|
line_len += word.len;
|
|
|
|
if (line_len >= threshold) {
|
|
|
|
newline = true;
|
|
|
|
line_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try writer.writeByte('\n');
|
|
|
|
} else {
|
|
|
|
const fmtString = std.fmt.comptimePrint("--{{s: <{}}} {{s}}\n", .{maxOptionLength});
|
|
|
|
try writer.print(fmtString, .{ field.name, @field(Generic.meta.option_docs, field.name) });
|
|
|
|
}
|
2023-07-25 18:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-25 22:56:45 +02:00
|
|
|
|
|
|
|
test "full help" {
|
|
|
|
const Options = struct {
|
|
|
|
boolflag: bool = false,
|
|
|
|
stringflag: []const u8 = "hello",
|
|
|
|
|
|
|
|
pub const shorthands = .{
|
|
|
|
.b = "boolflag",
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const meta = .{
|
2023-07-25 23:26:56 +02:00
|
|
|
.name = "test",
|
2023-07-25 22:56:45 +02:00
|
|
|
.full_text = "testing tool",
|
2023-08-31 22:14:48 +02:00
|
|
|
.usage_summary = "[--boolflag] [--stringflag]",
|
2023-07-25 22:56:45 +02:00
|
|
|
.option_docs = .{
|
|
|
|
.boolflag = "a boolean flag",
|
|
|
|
.stringflag = "a string flag",
|
2024-01-04 22:56:55 +01:00
|
|
|
},
|
2023-07-25 22:56:45 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var test_buffer = std.ArrayList(u8).init(std.testing.allocator);
|
|
|
|
defer test_buffer.deinit();
|
|
|
|
|
2023-07-26 16:11:46 +02:00
|
|
|
try printHelp(Options, "test", test_buffer.writer());
|
2023-07-25 22:56:45 +02:00
|
|
|
|
|
|
|
const expected =
|
2024-01-04 22:56:55 +01:00
|
|
|
\\Usage: test [--boolflag] [--stringflag]
|
|
|
|
\\
|
|
|
|
\\testing tool
|
|
|
|
\\
|
|
|
|
\\Options:
|
|
|
|
\\ -b, --boolflag a boolean flag
|
|
|
|
\\ --stringflag a string flag
|
|
|
|
\\
|
2023-07-25 22:56:45 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings(expected, test_buffer.items);
|
|
|
|
}
|
|
|
|
|
2023-08-31 22:14:48 +02:00
|
|
|
test "help with no usage summary" {
|
2023-07-25 22:56:45 +02:00
|
|
|
const Options = struct {
|
|
|
|
boolflag: bool = false,
|
|
|
|
stringflag: []const u8 = "hello",
|
|
|
|
|
|
|
|
pub const shorthands = .{
|
|
|
|
.b = "boolflag",
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const meta = .{
|
|
|
|
.full_text = "testing tool",
|
|
|
|
.option_docs = .{
|
|
|
|
.boolflag = "a boolean flag",
|
|
|
|
.stringflag = "a string flag",
|
2024-01-04 22:56:55 +01:00
|
|
|
},
|
2023-07-25 22:56:45 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var test_buffer = std.ArrayList(u8).init(std.testing.allocator);
|
|
|
|
defer test_buffer.deinit();
|
|
|
|
|
2023-07-26 16:11:46 +02:00
|
|
|
try printHelp(Options, "test", test_buffer.writer());
|
2023-07-25 22:56:45 +02:00
|
|
|
|
|
|
|
const expected =
|
2024-01-04 22:56:55 +01:00
|
|
|
\\Usage: test
|
|
|
|
\\
|
|
|
|
\\testing tool
|
|
|
|
\\
|
|
|
|
\\Options:
|
|
|
|
\\ -b, --boolflag a boolean flag
|
|
|
|
\\ --stringflag a string flag
|
|
|
|
\\
|
2023-07-25 22:56:45 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings(expected, test_buffer.items);
|
|
|
|
}
|
2024-03-19 01:09:35 +01:00
|
|
|
|
|
|
|
test "help with wrapping" {
|
|
|
|
const Options = struct {
|
|
|
|
boolflag: bool = false,
|
|
|
|
stringflag: []const u8 = "hello",
|
|
|
|
|
|
|
|
pub const shorthands = .{
|
|
|
|
.b = "boolflag",
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const wrap_len = 10;
|
|
|
|
|
|
|
|
pub const meta = .{
|
|
|
|
.full_text = "testing tool",
|
|
|
|
.option_docs = .{
|
|
|
|
.boolflag = "a boolean flag with a pretty long description about booleans",
|
|
|
|
.stringflag = "a string flag with another long description about strings",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var test_buffer = std.ArrayList(u8).init(std.testing.allocator);
|
|
|
|
defer test_buffer.deinit();
|
|
|
|
|
|
|
|
try printHelp(Options, "test", test_buffer.writer());
|
|
|
|
|
|
|
|
const expected =
|
|
|
|
\\Usage: test
|
|
|
|
\\
|
|
|
|
\\testing tool
|
|
|
|
\\
|
|
|
|
\\Options:
|
|
|
|
\\ -b, --boolflag a boolean flag
|
|
|
|
\\ with a pretty
|
|
|
|
\\ long description
|
|
|
|
\\ about booleans
|
|
|
|
\\ --stringflag a string flag
|
|
|
|
\\ with another
|
|
|
|
\\ long description
|
|
|
|
\\ about strings
|
|
|
|
\\
|
|
|
|
;
|
|
|
|
|
|
|
|
try std.testing.expectEqualStrings(expected, test_buffer.items);
|
|
|
|
}
|