cmd: update to use new object system

This commit is contained in:
2025-10-19 21:02:12 +01:00
parent fd04a49d84
commit 34c54eb545
7 changed files with 36 additions and 41 deletions

View File

@@ -102,8 +102,7 @@ b_status b_command_arg_set_allowed_values(
return B_SUCCESS;
}
void z__b_get_arg_usage_string(
struct b_command_arg *arg, bool colour, struct b_string *out)
void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string *out)
{
bool optional = false, multi = false;
switch (arg->arg_nr_values) {

View File

@@ -318,7 +318,7 @@ static b_status parse_short_opt(struct argv_parser *parser)
}
if (!opt) {
struct b_string *usage = z__b_command_default_usage_string(
b_string *usage = z__b_command_default_usage_string(
parser->cmd, NULL, parser->arglist);
b_err("unrecognised argument '" F_YELLOW "-%c" F_RESET
"'\n\n",
@@ -326,7 +326,7 @@ static b_status parse_short_opt(struct argv_parser *parser)
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW
"--help" F_RESET "'\n");
b_string_release(usage);
b_string_unref(usage);
return B_ERR_NO_ENTRY;
}
@@ -456,13 +456,13 @@ static b_status parse_long_opt(struct argv_parser *parser)
}
if (!opt && !subcmd) {
struct b_string *usage = z__b_command_default_usage_string(
b_string *usage = z__b_command_default_usage_string(
parser->cmd, NULL, parser->arglist);
b_err("unrecognised argument '" F_YELLOW "--%s" F_RESET
"'\n\nusage: %s\n\nfor more information, use '" F_YELLOW
"--help" F_RESET "'\n",
opt_name, b_string_ptr(usage));
b_string_release(usage);
b_string_unref(usage);
return B_ERR_NO_ENTRY;
}

View File

@@ -315,9 +315,9 @@ b_status b_command_usage_add_command(b_command_usage *usage, unsigned int cmd_id
static void prepend_command_name(struct b_command *cmd, b_string *out)
{
int nr_names = 0;
cmd->b_name &&nr_names++;
cmd->b_long_name &&nr_names++;
cmd->b_short_name &&nr_names++;
cmd->b_name&& nr_names++;
cmd->b_long_name&& nr_names++;
cmd->b_short_name&& nr_names++;
if (nr_names > 1) {
b_string_prepend_cstr(out, "}");
@@ -380,7 +380,7 @@ static void get_usage_string(
{
get_qualified_command_name(cmd, args, out);
struct b_string *cmd_name = b_string_create();
b_string *cmd_name = b_string_create();
b_queue_iterator it;
b_queue_foreach (&it, &usage->u_parts) {
@@ -428,7 +428,7 @@ static void get_usage_string(
}
}
b_string_release(cmd_name);
b_string_unref(cmd_name);
}
b_string *z__b_command_default_usage_string(
@@ -611,8 +611,8 @@ static void print_options_list(struct b_command *cmd)
}
}
b_string_release(opt_str);
b_string_release(desc_str);
b_string_unref(opt_str);
b_string_unref(desc_str);
}
static void print_args_list(struct b_command *cmd)
@@ -668,7 +668,7 @@ static void print_args_list(struct b_command *cmd)
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
}
b_string_release(str);
b_string_unref(str);
}
static void print_commands_list(struct b_command *cmd)
@@ -724,7 +724,7 @@ static void print_commands_list(struct b_command *cmd)
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
}
b_string_release(str);
b_string_unref(str);
}
struct b_command *b_command_get_subcommand_with_name(
@@ -871,7 +871,7 @@ static void print_usage(struct b_command *cmd, struct b_arglist *args)
b_string *usage
= z__b_command_default_usage_string(cmd, NULL, args);
b_print_paragraph(b_string_ptr(usage), OUTPUT_STREAM, &format);
b_string_release(usage);
b_string_unref(usage);
return;
}
@@ -889,7 +889,7 @@ static void print_usage(struct b_command *cmd, struct b_arglist *args)
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
}
b_string_release(str);
b_string_unref(str);
}
static void print_help(struct b_command *cmd, struct b_arglist *args)

View File

@@ -4,6 +4,7 @@
#include <blue/cmd.h>
#include <blue/core/btree.h>
#include <blue/core/queue.h>
#include <blue/ds/string.h>
#define F_RED "[bright_red]"
#define F_GREEN "[bright_green]"
@@ -26,8 +27,6 @@ enum b_command_usage_entry_type {
CMD_USAGE_COMMAND,
};
struct b_string;
struct b_command {
unsigned int b_id;
unsigned int b_parent_id;
@@ -136,19 +135,17 @@ BLUE_API b_status b_arglist_parse(
const char **argv);
BLUE_API void b_arglist_destroy(struct b_arglist *args);
BLUE_API struct b_string *z__b_command_default_usage_string(
BLUE_API b_string *z__b_command_default_usage_string(
struct b_command *cmd, struct b_command_option *with_opt,
const struct b_arglist *args);
BLUE_API void z__b_get_arg_usage_string(
struct b_command_arg *arg, bool colour, struct b_string *out);
BLUE_API void z__b_get_arg_description(
struct b_command_arg *arg, struct b_string *out);
struct b_command_arg *arg, bool colour, b_string *out);
BLUE_API void z__b_get_arg_description(struct b_command_arg *arg, b_string *out);
BLUE_API void z__b_get_option_usage_string(
struct b_command_option *opt, enum cmd_string_flags flags,
struct b_string *out);
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out);
BLUE_API void z__b_get_option_description(
struct b_command_option *opt, struct b_string *out);
struct b_command_option *opt, b_string *out);
#endif

View File

@@ -1,5 +1,5 @@
#ifndef BLUELIB_COMMAND_H_
#define BLUELIB_COMMAND_H_
#ifndef BLUE_CMD_H_
#define BLUE_CMD_H_
#include <blue/core/btree.h>
#include <blue/core/init.h>

View File

@@ -146,8 +146,7 @@ void z__b_get_option_description(struct b_command_option *opt, b_string *out)
}
void z__b_get_option_usage_string(
struct b_command_option *opt, enum cmd_string_flags flags,
struct b_string *out)
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out)
{
if (flags & CMD_STR_DIRECT_USAGE) {
b_string_append_cstr(out, "{");

View File

@@ -19,7 +19,7 @@ enum b_status b_arglist_report_missing_option(
return B_ERR_INVALID_ARGUMENT;
}
struct b_string *opt_string = b_string_create();
b_string *opt_string = b_string_create();
z__b_get_option_usage_string(opt, 0, opt_string);
char opt_name_s[128];
@@ -50,7 +50,7 @@ enum b_status b_arglist_report_missing_option(
b_i("usage: %s", b_string_ptr(opt_string));
b_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
b_string_release(opt_string);
b_string_unref(opt_string);
return B_SUCCESS;
}
@@ -58,7 +58,7 @@ enum b_status b_arglist_report_missing_option(
enum b_status b_arglist_report_unexpected_arg(
const b_arglist *args, const char *value)
{
struct b_string *usage = z__b_command_default_usage_string(
b_string *usage = z__b_command_default_usage_string(
args->list_command, NULL, args);
b_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value);
@@ -84,9 +84,9 @@ enum b_status b_arglist_report_invalid_arg_value(
: b_command_get_arg_with_id(args->list_command, arg_id);
}
struct b_string *usage = z__b_command_default_usage_string(
b_string *usage = z__b_command_default_usage_string(
args->list_command, opt, args);
struct b_string *opt_string = b_string_create();
b_string *opt_string = b_string_create();
if (opt) {
z__b_get_option_usage_string(opt, 0, opt_string);
@@ -118,8 +118,8 @@ enum b_status b_arglist_report_invalid_arg_value(
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW "--help" F_RESET);
b_string_release(usage);
b_string_release(opt_string);
b_string_unref(usage);
b_string_unref(opt_string);
return B_SUCCESS;
}
@@ -142,9 +142,9 @@ enum b_status b_arglist_report_missing_args(
assert(arg);
}
struct b_string *usage = z__b_command_default_usage_string(
b_string *usage = z__b_command_default_usage_string(
args->list_command, opt, args);
struct b_string *opt_string = b_string_create();
b_string *opt_string = b_string_create();
if (opt) {
z__b_get_option_usage_string(opt, 0, opt_string);
@@ -189,8 +189,8 @@ enum b_status b_arglist_report_missing_args(
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
b_string_release(usage);
b_string_release(opt_string);
b_string_unref(usage);
b_string_unref(opt_string);
return B_SUCCESS;
}