cmd: improve command usage strings
the command usage strings now use the actual argument values provided by the user, in particular for the names of relevant commands
This commit is contained in:
@@ -352,8 +352,17 @@ static void prepend_command_name(struct b_command *cmd, b_string *out)
|
||||
}
|
||||
}
|
||||
|
||||
static void get_qualified_command_name(struct b_command *cmd, b_string *out)
|
||||
static void get_qualified_command_name(
|
||||
struct b_command *cmd, const struct b_arglist *args, b_string *out)
|
||||
{
|
||||
for (unsigned int i = 0; i <= args->list_argv_last_command; i++) {
|
||||
if (i > 0) {
|
||||
b_string_append_cstr(out, " ");
|
||||
}
|
||||
|
||||
b_string_append_cstr(out, args->list_argv[i]);
|
||||
}
|
||||
#if 0
|
||||
prepend_command_name(cmd, out);
|
||||
cmd = cmd->b_parent;
|
||||
|
||||
@@ -362,12 +371,14 @@ static void get_qualified_command_name(struct b_command *cmd, b_string *out)
|
||||
prepend_command_name(cmd, out);
|
||||
cmd = cmd->b_parent;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void get_usage_string(
|
||||
struct b_command *cmd, struct b_command_usage *usage, b_string *out)
|
||||
struct b_command *cmd, struct b_arglist *args,
|
||||
struct b_command_usage *usage, b_string *out)
|
||||
{
|
||||
get_qualified_command_name(cmd, out);
|
||||
get_qualified_command_name(cmd, args, out);
|
||||
|
||||
struct b_string *cmd_name = b_string_create();
|
||||
|
||||
@@ -421,10 +432,11 @@ static void get_usage_string(
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
b_string *str = b_string_create();
|
||||
get_qualified_command_name(cmd, str);
|
||||
get_qualified_command_name(cmd, args, str);
|
||||
|
||||
if (with_opt) {
|
||||
b_string_append_cstr(str, " ");
|
||||
@@ -848,7 +860,7 @@ struct b_command_arg *b_command_get_arg_with_id(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void print_usage(struct b_command *cmd)
|
||||
static void print_usage(struct b_command *cmd, struct b_arglist *args)
|
||||
{
|
||||
b_paragraph_format format = {0};
|
||||
format.p_left_margin = format.p_right_margin = 4;
|
||||
@@ -856,7 +868,8 @@ static void print_usage(struct b_command *cmd)
|
||||
b_tty_puts(OUTPUT_STREAM, 0, F_YELLOW "USAGE:" F_RESET "\n");
|
||||
|
||||
if (b_queue_empty(&cmd->b_usage)) {
|
||||
b_string *usage = z__b_command_default_usage_string(cmd, NULL);
|
||||
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);
|
||||
return;
|
||||
@@ -872,14 +885,14 @@ static void print_usage(struct b_command *cmd)
|
||||
}
|
||||
|
||||
b_string_clear(str);
|
||||
get_usage_string(cmd, usage, str);
|
||||
get_usage_string(cmd, args, usage, str);
|
||||
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
|
||||
}
|
||||
|
||||
b_string_release(str);
|
||||
}
|
||||
|
||||
static void print_help(struct b_command *cmd)
|
||||
static void print_help(struct b_command *cmd, struct b_arglist *args)
|
||||
{
|
||||
b_paragraph_format format = {0};
|
||||
|
||||
@@ -894,7 +907,7 @@ static void print_help(struct b_command *cmd)
|
||||
|
||||
b_tty_putc(OUTPUT_STREAM, 0, '\n');
|
||||
|
||||
print_usage(cmd);
|
||||
print_usage(cmd, args);
|
||||
|
||||
if (!b_queue_empty(&cmd->b_opt)) {
|
||||
print_options_list(cmd);
|
||||
@@ -921,12 +934,12 @@ static int execute_command(struct b_command *cmd, struct b_arglist *args)
|
||||
args, B_COMMAND_OPTION_HELP, B_COMMAND_INVALID_ID);
|
||||
|
||||
if ((cmd->b_flags & B_COMMAND_SHOW_HELP_BY_DEFAULT) && nr_items == 0) {
|
||||
print_help(cmd);
|
||||
print_help(cmd, args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nr_help > 0) {
|
||||
print_help(cmd);
|
||||
print_help(cmd, args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -978,6 +991,8 @@ int b_command_dispatch(unsigned int cmd_id, int argc, const char **argv)
|
||||
}
|
||||
|
||||
struct b_arglist *args = b_arglist_create();
|
||||
args->list_argc = argc;
|
||||
args->list_argv = argv;
|
||||
args->list_command = cmd;
|
||||
|
||||
b_status status = b_arglist_parse(args, &cmd, argc, argv);
|
||||
|
||||
Reference in New Issue
Block a user