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:
parent
6ffc1c1a4c
commit
2c09113a37
@ -73,14 +73,6 @@ fn BuilderGenerics(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 {
|
||||
param_spec: ncmeta.TupleBuilder = .{},
|
||||
// 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;
|
||||
}
|
||||
|
||||
pub fn mock_help_parameter(comptime self: @This()) ?HelpFlagOption {
|
||||
if (self.help_flag.short_tag == null and self.help_flag.long_tag == null) return null;
|
||||
const string_generics = BuilderGenerics(UserContext){ .OutputType = []const u8 };
|
||||
|
||||
return HelpFlagOption{
|
||||
.name = "_internal_help_flag",
|
||||
.short_tag = self.help_flag.short_tag,
|
||||
.long_tag = self.help_flag.long_tag,
|
||||
.description = "Print this help message and exit",
|
||||
.flag_bias = true,
|
||||
};
|
||||
pub fn string_option(
|
||||
comptime self: *@This(),
|
||||
comptime cfg: OptionConfig(string_generics.opt_gen()),
|
||||
) void {
|
||||
const config = if (cfg.nice_type_name == null)
|
||||
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(
|
||||
|
@ -151,7 +151,7 @@ pub fn OptionConfig(comptime generics: ParameterGenerics) type {
|
||||
global: bool = false,
|
||||
|
||||
secret: bool = false,
|
||||
nice_type_name: []const u8 = @typeName(generics.OutputType),
|
||||
nice_type_name: ?[]const u8 = null,
|
||||
flag_bias: FlagBias = .unbiased,
|
||||
};
|
||||
}
|
||||
@ -279,7 +279,7 @@ pub fn make_option(comptime generics: ParameterGenerics, comptime opts: OptionCo
|
||||
.global = opts.global,
|
||||
//
|
||||
.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,
|
||||
};
|
||||
}
|
||||
@ -320,7 +320,7 @@ pub fn make_argument(
|
||||
.global = opts.global,
|
||||
//
|
||||
.secret = opts.secret,
|
||||
.nice_type_name = opts.nice_type_name,
|
||||
.nice_type_name = opts.nice_type_name orelse @typeName(generics.OutputType),
|
||||
.flag_bias = .unbiased,
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user