command: add a few helper functions

There's going to be more where this came from. Since this is our main
API, directly exposing all of the built-in functionality at this layer
is desirable.
This commit is contained in:
torque 2023-04-03 01:38:00 -07:00
parent 6ffc1c1a4c
commit 2c09113a37
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
2 changed files with 33 additions and 20 deletions

View File

@ -73,14 +73,6 @@ fn BuilderGenerics(comptime UserContext: type) type {
} }
pub fn CommandBuilder(comptime UserContext: type) type { pub fn CommandBuilder(comptime UserContext: type) type {
const HelpFlagOption = OptionConfig(.{
.UserContext = UserContext,
.OutputType = bool,
.param_type = .Nominal,
.value_count = .flag,
.multi = false,
});
return struct { return struct {
param_spec: ncmeta.TupleBuilder = .{}, param_spec: ncmeta.TupleBuilder = .{},
// this is a strange hack, but it's easily the path of least resistance // this is a strange hack, but it's easily the path of least resistance
@ -114,16 +106,37 @@ pub fn CommandBuilder(comptime UserContext: type) type {
self.help_flag = tags; self.help_flag = tags;
} }
pub fn mock_help_parameter(comptime self: @This()) ?HelpFlagOption { const string_generics = BuilderGenerics(UserContext){ .OutputType = []const u8 };
if (self.help_flag.short_tag == null and self.help_flag.long_tag == null) return null;
return HelpFlagOption{ pub fn string_option(
.name = "_internal_help_flag", comptime self: *@This(),
.short_tag = self.help_flag.short_tag, comptime cfg: OptionConfig(string_generics.opt_gen()),
.long_tag = self.help_flag.long_tag, ) void {
.description = "Print this help message and exit", const config = if (cfg.nice_type_name == null)
.flag_bias = true, ncmeta.copy_struct(@TypeOf(cfg), cfg, .{ .nice_type_name = "string" })
}; else
cfg;
self.add_option(string_generics.opt_gen(), config);
}
pub fn string_argument(
comptime self: *@This(),
comptime cfg: OptionConfig(string_generics.arg_gen()),
) void {
const config = if (cfg.nice_type_name == null)
ncmeta.copy_struct(@TypeOf(cfg), cfg, .{ .nice_type_name = "string" })
else
cfg;
self.add_argument(string_generics.arg_gen(), config);
}
pub fn simple_flag(
comptime self: *@This(),
comptime cfg: FlagConfig(string_generics.flag_gen()),
) void {
self.add_flag(string_generics, cfg);
} }
pub fn add_argument( pub fn add_argument(

View File

@ -151,7 +151,7 @@ pub fn OptionConfig(comptime generics: ParameterGenerics) type {
global: bool = false, global: bool = false,
secret: bool = false, secret: bool = false,
nice_type_name: []const u8 = @typeName(generics.OutputType), nice_type_name: ?[]const u8 = null,
flag_bias: FlagBias = .unbiased, flag_bias: FlagBias = .unbiased,
}; };
} }
@ -279,7 +279,7 @@ pub fn make_option(comptime generics: ParameterGenerics, comptime opts: OptionCo
.global = opts.global, .global = opts.global,
// //
.secret = opts.secret, .secret = opts.secret,
.nice_type_name = opts.nice_type_name, .nice_type_name = opts.nice_type_name orelse @typeName(generics.OutputType),
.flag_bias = opts.flag_bias, .flag_bias = opts.flag_bias,
}; };
} }
@ -320,7 +320,7 @@ pub fn make_argument(
.global = opts.global, .global = opts.global,
// //
.secret = opts.secret, .secret = opts.secret,
.nice_type_name = opts.nice_type_name, .nice_type_name = opts.nice_type_name orelse @typeName(generics.OutputType),
.flag_bias = .unbiased, .flag_bias = .unbiased,
}; };
} }