cmd: add support for dynamically generating command options at runtime

This commit is contained in:
2026-02-03 14:46:40 +00:00
parent f5c4fa561f
commit 2632feac32
3 changed files with 47 additions and 3 deletions

View File

@@ -282,6 +282,24 @@ struct b_command_usage *b_command_add_usage(struct b_command *cmd)
return usage;
}
const struct b_command_option *b_command_get_option(
const struct b_command *cmd, int id)
{
struct b_queue_entry *cur = b_queue_first(&cmd->b_opt);
while (cur) {
const struct b_command_option *opt
= b_unbox(struct b_command_option, cur, opt_entry);
if (opt->opt_id == id) {
return opt;
}
cur = b_queue_next(cur);
}
return NULL;
}
b_status b_command_usage_add_option(
struct b_command_usage *usage, struct b_command_option *opt)
{

View File

@@ -61,6 +61,12 @@
this_opt = opt_##id; \
if (this_opt)
#define B_COMMAND_OPTION_GEN(id) \
b_command_option *z__b_unique_name() \
= b_command_add_option(this_cmd, (id)); \
this_opt = z__b_unique_name(); \
if (this_opt)
#define B_COMMAND_HELP_OPTION() \
do { \
b_command_option *opt \
@@ -210,6 +216,11 @@ BLUE_API b_command_option *b_command_add_option(b_command *cmd, int id);
BLUE_API b_command_arg *b_command_add_arg(b_command *cmd, int id);
BLUE_API b_command_usage *b_command_add_usage(b_command *cmd);
BLUE_API const b_command_option *b_command_get_option(const b_command *cmd, int id);
BLUE_API const char *b_command_option_get_long_name(const b_command_option *opt);
BLUE_API char b_command_option_get_short_name(const b_command_option *opt);
BLUE_API const char *b_command_option_get_description(b_command_option *opt);
BLUE_API b_status b_command_option_set_long_name(
b_command_option *opt, const char *name);
BLUE_API b_status b_command_option_set_short_name(b_command_option *opt, char name);

View File

@@ -42,6 +42,21 @@ void b_command_option_destroy(struct b_command_option *opt)
free(opt);
}
const char *b_command_option_get_long_name(const struct b_command_option *opt)
{
return opt->opt_long_name;
}
char b_command_option_get_short_name(const struct b_command_option *opt)
{
return opt->opt_short_name;
}
const char *b_command_option_get_description(struct b_command_option *opt)
{
return opt->opt_description;
}
b_status b_command_option_set_long_name(
struct b_command_option *opt, const char *name)
{