cmd: add support for showing placeholder commands/opts/args in command usage strings
This commit is contained in:
@@ -305,6 +305,21 @@ b_status b_command_usage_add_arg(
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_command_usage_add_command(b_command_usage *usage, unsigned int cmd_id)
|
||||
{
|
||||
struct b_command_usage_command *u_cmd = malloc(sizeof *u_cmd);
|
||||
if (!u_cmd) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(u_cmd, 0x0, sizeof *u_cmd);
|
||||
|
||||
u_cmd->cmd_id = cmd_id;
|
||||
|
||||
b_queue_push_back(&usage->u_cmd, &u_cmd->cmd_entry);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static void prepend_command_name(struct b_command *cmd, b_string *out)
|
||||
{
|
||||
int nr_names = 0;
|
||||
@@ -362,7 +377,33 @@ static void get_usage_string(
|
||||
{
|
||||
get_qualified_command_name(cmd, out);
|
||||
|
||||
struct b_string *cmd_name = b_string_create();
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &usage->u_cmd) {
|
||||
struct b_command_usage_command *subcmd = b_unbox(
|
||||
struct b_command_usage_command, it.entry, cmd_entry);
|
||||
|
||||
if (!subcmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_string_clear(cmd_name);
|
||||
b_string_append_cstr(out, " ");
|
||||
|
||||
if (subcmd->cmd_id == B_COMMAND_INVALID_ID) {
|
||||
b_string_append_cstr(out, "[COMMAND]");
|
||||
} else {
|
||||
struct b_command *subcmd_info
|
||||
= get_command(&command_list, subcmd->cmd_id);
|
||||
prepend_command_name(subcmd_info, cmd_name);
|
||||
|
||||
b_string_append_s(out, cmd_name);
|
||||
}
|
||||
}
|
||||
|
||||
b_string_release(cmd_name);
|
||||
|
||||
b_queue_foreach (&it, &usage->u_opt) {
|
||||
struct b_command_usage_opt *opt = b_unbox(
|
||||
struct b_command_usage_opt, it.entry, opt_entry);
|
||||
@@ -372,7 +413,13 @@ static void get_usage_string(
|
||||
}
|
||||
|
||||
b_string_append_cstr(out, " ");
|
||||
z__b_get_option_usage_string(opt->opt, CMD_STR_DIRECT_USAGE, out);
|
||||
|
||||
if (opt->opt == (struct b_command_option *)B_COMMAND_INVALID_ID) {
|
||||
b_string_append_cstr(out, "[OPTIONS]");
|
||||
} else {
|
||||
z__b_get_option_usage_string(
|
||||
opt->opt, CMD_STR_DIRECT_USAGE, out);
|
||||
}
|
||||
}
|
||||
|
||||
b_queue_foreach (&it, &usage->u_arg) {
|
||||
@@ -384,7 +431,12 @@ static void get_usage_string(
|
||||
}
|
||||
|
||||
b_string_append_cstr(out, " ");
|
||||
z__b_get_arg_usage_string(arg->arg, false, out);
|
||||
|
||||
if (arg->arg == (struct b_command_arg *)B_COMMAND_INVALID_ID) {
|
||||
b_string_append_cstr(out, "[ARGS]");
|
||||
} else {
|
||||
z__b_get_arg_usage_string(arg->arg, false, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user