From: Oleksii Timofieiev Date: Wed, 24 Jan 2018 10:49:13 +0000 (+0300) Subject: nvme-cli: handle long commands in help message by adjusting minimal padding X-Git-Tag: v1.6~104^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=c98321f01d3172ddf47f3ef2e4628761b4e3cfb1;p=users%2Fsagi%2Fnvme-cli.git nvme-cli: handle long commands in help message by adjusting minimal padding Signed-off-by: Oleksii Timofieiev --- diff --git a/plugin.c b/plugin.c index a9bf6cd0..4750df6b 100644 --- a/plugin.c +++ b/plugin.c @@ -61,7 +61,8 @@ void general_help(struct plugin *plugin) struct program *prog = plugin->parent; struct plugin *extension; unsigned i = 0; - + unsigned padding = 15; + unsigned curr_length = 0; printf("%s-%s\n", prog->name, prog->version); usage(plugin); @@ -78,12 +79,19 @@ void general_help(struct plugin *plugin) printf("\nThe following are all implemented sub-commands:\n"); + /* iterate through all commands to get maximum length */ + /* Still need to handle the case of ultra long strings, help messages, etc */ + for (; plugin->commands[i]; i++) + if (padding < (curr_length = 2 + strlen(plugin->commands[i]->name))) + padding = curr_length; + + i = 0; for (; plugin->commands[i]; i++) - printf(" %-*s %s\n", 15, plugin->commands[i]->name, + printf(" %-*s %s\n", padding, plugin->commands[i]->name, plugin->commands[i]->help); - printf(" %-*s %s\n", 15, "version", "Shows the program version"); - printf(" %-*s %s\n", 15, "help", "Display this help"); + printf(" %-*s %s\n", padding, "version", "Shows the program version"); + printf(" %-*s %s\n", padding, "help", "Display this help"); printf("\n"); if (plugin->name)