Skip to content

Commit

Permalink
Add a "help" command
Browse files Browse the repository at this point in the history
So that we can spawn the correct -t <type> backend
and have it print the usage information, including
type specific arguments.

Signed-off-by: Ronnie Sahlberg <[email protected]>
  • Loading branch information
RonnieSahlberg committed Feb 15, 2025
1 parent da74605 commit 8edbd16
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions ublk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ static const struct ublksrv_tgt_type *ublksrv_find_tgt_type(const char *name)
return NULL;
}

static void ublksrv_for_each_tgt_type(void (*handle_tgt_type)(unsigned idx,
const struct ublksrv_tgt_type *type, void *data),
void *data)
{
int i;

for (i = 0; i < UBLKSRV_TGT_TYPE_MAX; i++) {
const struct ublksrv_tgt_type *type = tgt_list[i];

if (!type)
continue;
handle_tgt_type(i, type, data);
}
}

int ublksrv_register_tgt_type(struct ublksrv_tgt_type *type)
{
if (type->type < UBLKSRV_TGT_TYPE_MAX && !tgt_list[type->type]) {
Expand Down Expand Up @@ -178,8 +163,8 @@ static void cmd_dev_add_usage(const char *cmd)
{
printf("%s add -t <type>\n", cmd);
ublksrv_print_std_opts();
printf("\ttarget specific command line:\n");
ublksrv_for_each_tgt_type(show_tgt_add_usage, NULL);
printf("\tFor type specific options, run:\n");
printf("\t\tublk help -t <type>\n");
}

static int __cmd_dev_del(int number, bool log, bool async)
Expand Down Expand Up @@ -553,10 +538,31 @@ static void cmd_usage(const char *cmd)
cmd_dev_recover_usage(cmd);
cmd_dev_get_features_help(cmd);

printf("%s help -t <target>\n", cmd);
printf("%s -v [--version]\n", cmd);
printf("%s -h [--help]\n", cmd);
}

static int cmd_dev_help(int argc, char *argv[])
{
struct ublksrv_dev_data data = {0};
struct ublksrv_ctrl_dev *dev;
const struct ublksrv_tgt_type *tgt_type;
int ret;
const char *dump_buf;

ublksrv_parse_std_opts(&data, argc, argv);

if (data.tgt_type == NULL) {
cmd_usage("ublk");
return EXIT_SUCCESS;
}

ublksrv_execve_helper("help", data.tgt_type, argc, argv);
fprintf(stderr, "failed to spawn target\n");
return EXIT_FAILURE;
}

int main(int argc, char *argv[])
{
const char *prog_name = "ublk";
Expand All @@ -579,6 +585,8 @@ int main(int argc, char *argv[])
ret = cmd_dev_add(argc, argv);
else if (!strcmp(cmd, "del"))
ret = cmd_dev_del(argc, argv);
else if (!strcmp(cmd, "help"))
ret = cmd_dev_help(argc, argv);
else if (!strcmp(cmd, "list"))
ret = cmd_list_dev_info(argc, argv);
else if (!strcmp(cmd, "recover"))
Expand Down

0 comments on commit 8edbd16

Please sign in to comment.