parser: add interface method for retrieving a child interface by name
The main value of this method is that it allows runtime access to the help description of the subcommand. This could allow implementation of a help flag that takes the name of a subcommand to print help for or something. Anyway, it's probably useful.
This commit is contained in:
parent
d091de5686
commit
883218cdca
@ -12,6 +12,7 @@ pub const ParserInterface = struct {
|
|||||||
execute: *const fn (parser: *anyopaque, context: *anyopaque) anyerror!void,
|
execute: *const fn (parser: *anyopaque, context: *anyopaque) anyerror!void,
|
||||||
parse: *const fn (parser: *anyopaque, context: *anyopaque, name: []const u8, args: [][:0]u8, env: std.process.EnvMap) anyerror!void,
|
parse: *const fn (parser: *anyopaque, context: *anyopaque, name: []const u8, args: [][:0]u8, env: std.process.EnvMap) anyerror!void,
|
||||||
finish: *const fn (parser: *anyopaque, context: *anyopaque) anyerror!void,
|
finish: *const fn (parser: *anyopaque, context: *anyopaque) anyerror!void,
|
||||||
|
getChild: *const fn (parser: *anyopaque, name: []const u8) ?ParserInterface,
|
||||||
describe: *const fn () []const u8,
|
describe: *const fn () []const u8,
|
||||||
deinit: *const fn (parser: *anyopaque) void,
|
deinit: *const fn (parser: *anyopaque) void,
|
||||||
deinitTree: *const fn (parser: *anyopaque) void,
|
deinitTree: *const fn (parser: *anyopaque) void,
|
||||||
@ -29,6 +30,7 @@ pub const ParserInterface = struct {
|
|||||||
.execute = ParserType.wrap_execute,
|
.execute = ParserType.wrap_execute,
|
||||||
.parse = ParserType.wrap_parse,
|
.parse = ParserType.wrap_parse,
|
||||||
.finish = ParserType.wrap_finish,
|
.finish = ParserType.wrap_finish,
|
||||||
|
.getChild = ParserType.wrap_getChild,
|
||||||
.describe = ParserType.describe,
|
.describe = ParserType.describe,
|
||||||
.deinit = ParserType.wrap_deinit,
|
.deinit = ParserType.wrap_deinit,
|
||||||
.deinitTree = ParserType.wrap_deinitTree,
|
.deinitTree = ParserType.wrap_deinitTree,
|
||||||
@ -48,6 +50,10 @@ pub const ParserInterface = struct {
|
|||||||
return try self.methods.finish(self.parser, self.context);
|
return try self.methods.finish(self.parser, self.context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getChild(self: @This(), name: []const u8) ?ParserInterface {
|
||||||
|
return self.methods.getChild(self.parser, name);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn describe(self: @This()) []const u8 {
|
pub fn describe(self: @This()) []const u8 {
|
||||||
return self.methods.describe();
|
return self.methods.describe();
|
||||||
}
|
}
|
||||||
@ -147,6 +153,11 @@ pub fn Parser(comptime command: anytype, comptime callback: anytype) type {
|
|||||||
return try self.finish(context);
|
return try self.finish(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wrap_getChild(parser: *anyopaque, name: []const u8) ?ParserInterface {
|
||||||
|
const self = cast_interface_parser(parser);
|
||||||
|
return self.getChild(name);
|
||||||
|
}
|
||||||
|
|
||||||
fn wrap_deinit(parser: *anyopaque) void {
|
fn wrap_deinit(parser: *anyopaque) void {
|
||||||
const self = cast_interface_parser(parser);
|
const self = cast_interface_parser(parser);
|
||||||
self.deinit();
|
self.deinit();
|
||||||
@ -200,6 +211,10 @@ pub fn Parser(comptime command: anytype, comptime callback: anytype) type {
|
|||||||
self.deinit();
|
self.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getChild(self: @This(), name: []const u8) ?ParserInterface {
|
||||||
|
return self.subcommands.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn execute(self: *@This(), context: UserContext) anyerror!void {
|
pub fn execute(self: *@This(), context: UserContext) anyerror!void {
|
||||||
const args = try std.process.argsAlloc(self.allocator);
|
const args = try std.process.argsAlloc(self.allocator);
|
||||||
var env = try std.process.getEnvMap(self.allocator);
|
var env = try std.process.getEnvMap(self.allocator);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user