cmd: update to use new object system
This commit is contained in:
@@ -102,8 +102,7 @@ b_status b_command_arg_set_allowed_values(
|
|||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void z__b_get_arg_usage_string(
|
void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string *out)
|
||||||
struct b_command_arg *arg, bool colour, struct b_string *out)
|
|
||||||
{
|
{
|
||||||
bool optional = false, multi = false;
|
bool optional = false, multi = false;
|
||||||
switch (arg->arg_nr_values) {
|
switch (arg->arg_nr_values) {
|
||||||
|
|||||||
@@ -318,7 +318,7 @@ static b_status parse_short_opt(struct argv_parser *parser)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!opt) {
|
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);
|
parser->cmd, NULL, parser->arglist);
|
||||||
b_err("unrecognised argument '" F_YELLOW "-%c" F_RESET
|
b_err("unrecognised argument '" F_YELLOW "-%c" F_RESET
|
||||||
"'\n\n",
|
"'\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("usage: %s", b_string_ptr(usage));
|
||||||
b_i("for more information, use '" F_YELLOW
|
b_i("for more information, use '" F_YELLOW
|
||||||
"--help" F_RESET "'\n");
|
"--help" F_RESET "'\n");
|
||||||
b_string_release(usage);
|
b_string_unref(usage);
|
||||||
|
|
||||||
return B_ERR_NO_ENTRY;
|
return B_ERR_NO_ENTRY;
|
||||||
}
|
}
|
||||||
@@ -456,13 +456,13 @@ static b_status parse_long_opt(struct argv_parser *parser)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!opt && !subcmd) {
|
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);
|
parser->cmd, NULL, parser->arglist);
|
||||||
b_err("unrecognised argument '" F_YELLOW "--%s" F_RESET
|
b_err("unrecognised argument '" F_YELLOW "--%s" F_RESET
|
||||||
"'\n\nusage: %s\n\nfor more information, use '" F_YELLOW
|
"'\n\nusage: %s\n\nfor more information, use '" F_YELLOW
|
||||||
"--help" F_RESET "'\n",
|
"--help" F_RESET "'\n",
|
||||||
opt_name, b_string_ptr(usage));
|
opt_name, b_string_ptr(usage));
|
||||||
b_string_release(usage);
|
b_string_unref(usage);
|
||||||
|
|
||||||
return B_ERR_NO_ENTRY;
|
return B_ERR_NO_ENTRY;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
static void prepend_command_name(struct b_command *cmd, b_string *out)
|
||||||
{
|
{
|
||||||
int nr_names = 0;
|
int nr_names = 0;
|
||||||
cmd->b_name &&nr_names++;
|
cmd->b_name&& nr_names++;
|
||||||
cmd->b_long_name &&nr_names++;
|
cmd->b_long_name&& nr_names++;
|
||||||
cmd->b_short_name &&nr_names++;
|
cmd->b_short_name&& nr_names++;
|
||||||
|
|
||||||
if (nr_names > 1) {
|
if (nr_names > 1) {
|
||||||
b_string_prepend_cstr(out, "}");
|
b_string_prepend_cstr(out, "}");
|
||||||
@@ -380,7 +380,7 @@ static void get_usage_string(
|
|||||||
{
|
{
|
||||||
get_qualified_command_name(cmd, args, out);
|
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_iterator it;
|
||||||
b_queue_foreach (&it, &usage->u_parts) {
|
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(
|
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_unref(opt_str);
|
||||||
b_string_release(desc_str);
|
b_string_unref(desc_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_args_list(struct b_command *cmd)
|
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_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)
|
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_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(
|
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
|
b_string *usage
|
||||||
= z__b_command_default_usage_string(cmd, NULL, args);
|
= z__b_command_default_usage_string(cmd, NULL, args);
|
||||||
b_print_paragraph(b_string_ptr(usage), OUTPUT_STREAM, &format);
|
b_print_paragraph(b_string_ptr(usage), OUTPUT_STREAM, &format);
|
||||||
b_string_release(usage);
|
b_string_unref(usage);
|
||||||
return;
|
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_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)
|
static void print_help(struct b_command *cmd, struct b_arglist *args)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <blue/cmd.h>
|
#include <blue/cmd.h>
|
||||||
#include <blue/core/btree.h>
|
#include <blue/core/btree.h>
|
||||||
#include <blue/core/queue.h>
|
#include <blue/core/queue.h>
|
||||||
|
#include <blue/ds/string.h>
|
||||||
|
|
||||||
#define F_RED "[bright_red]"
|
#define F_RED "[bright_red]"
|
||||||
#define F_GREEN "[bright_green]"
|
#define F_GREEN "[bright_green]"
|
||||||
@@ -26,8 +27,6 @@ enum b_command_usage_entry_type {
|
|||||||
CMD_USAGE_COMMAND,
|
CMD_USAGE_COMMAND,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct b_string;
|
|
||||||
|
|
||||||
struct b_command {
|
struct b_command {
|
||||||
unsigned int b_id;
|
unsigned int b_id;
|
||||||
unsigned int b_parent_id;
|
unsigned int b_parent_id;
|
||||||
@@ -136,19 +135,17 @@ BLUE_API b_status b_arglist_parse(
|
|||||||
const char **argv);
|
const char **argv);
|
||||||
BLUE_API void b_arglist_destroy(struct b_arglist *args);
|
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,
|
struct b_command *cmd, struct b_command_option *with_opt,
|
||||||
const struct b_arglist *args);
|
const struct b_arglist *args);
|
||||||
|
|
||||||
BLUE_API void z__b_get_arg_usage_string(
|
BLUE_API void z__b_get_arg_usage_string(
|
||||||
struct b_command_arg *arg, bool colour, struct b_string *out);
|
struct b_command_arg *arg, bool colour, b_string *out);
|
||||||
BLUE_API void z__b_get_arg_description(
|
BLUE_API void z__b_get_arg_description(struct b_command_arg *arg, b_string *out);
|
||||||
struct b_command_arg *arg, struct b_string *out);
|
|
||||||
|
|
||||||
BLUE_API void z__b_get_option_usage_string(
|
BLUE_API void z__b_get_option_usage_string(
|
||||||
struct b_command_option *opt, enum cmd_string_flags flags,
|
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out);
|
||||||
struct b_string *out);
|
|
||||||
BLUE_API void z__b_get_option_description(
|
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
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef BLUELIB_COMMAND_H_
|
#ifndef BLUE_CMD_H_
|
||||||
#define BLUELIB_COMMAND_H_
|
#define BLUE_CMD_H_
|
||||||
|
|
||||||
#include <blue/core/btree.h>
|
#include <blue/core/btree.h>
|
||||||
#include <blue/core/init.h>
|
#include <blue/core/init.h>
|
||||||
|
|||||||
@@ -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(
|
void z__b_get_option_usage_string(
|
||||||
struct b_command_option *opt, enum cmd_string_flags flags,
|
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out)
|
||||||
struct b_string *out)
|
|
||||||
{
|
{
|
||||||
if (flags & CMD_STR_DIRECT_USAGE) {
|
if (flags & CMD_STR_DIRECT_USAGE) {
|
||||||
b_string_append_cstr(out, "{");
|
b_string_append_cstr(out, "{");
|
||||||
|
|||||||
22
cmd/report.c
22
cmd/report.c
@@ -19,7 +19,7 @@ enum b_status b_arglist_report_missing_option(
|
|||||||
return B_ERR_INVALID_ARGUMENT;
|
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);
|
z__b_get_option_usage_string(opt, 0, opt_string);
|
||||||
|
|
||||||
char opt_name_s[128];
|
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("usage: %s", b_string_ptr(opt_string));
|
||||||
b_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
|
b_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
|
||||||
|
|
||||||
b_string_release(opt_string);
|
b_string_unref(opt_string);
|
||||||
|
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ enum b_status b_arglist_report_missing_option(
|
|||||||
enum b_status b_arglist_report_unexpected_arg(
|
enum b_status b_arglist_report_unexpected_arg(
|
||||||
const b_arglist *args, const char *value)
|
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);
|
args->list_command, NULL, args);
|
||||||
|
|
||||||
b_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value);
|
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);
|
: 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);
|
args->list_command, opt, args);
|
||||||
struct b_string *opt_string = b_string_create();
|
b_string *opt_string = b_string_create();
|
||||||
|
|
||||||
if (opt) {
|
if (opt) {
|
||||||
z__b_get_option_usage_string(opt, 0, opt_string);
|
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("usage: %s", b_string_ptr(usage));
|
||||||
b_i("for more information, use '" F_YELLOW "--help" F_RESET);
|
b_i("for more information, use '" F_YELLOW "--help" F_RESET);
|
||||||
|
|
||||||
b_string_release(usage);
|
b_string_unref(usage);
|
||||||
b_string_release(opt_string);
|
b_string_unref(opt_string);
|
||||||
|
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -142,9 +142,9 @@ enum b_status b_arglist_report_missing_args(
|
|||||||
assert(arg);
|
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);
|
args->list_command, opt, args);
|
||||||
struct b_string *opt_string = b_string_create();
|
b_string *opt_string = b_string_create();
|
||||||
|
|
||||||
if (opt) {
|
if (opt) {
|
||||||
z__b_get_option_usage_string(opt, 0, opt_string);
|
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("usage: %s", b_string_ptr(usage));
|
||||||
b_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
|
b_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
|
||||||
|
|
||||||
b_string_release(usage);
|
b_string_unref(usage);
|
||||||
b_string_release(opt_string);
|
b_string_unref(opt_string);
|
||||||
|
|
||||||
return B_SUCCESS;
|
return B_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user