]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-cli: handle long commands in help message by adjusting minimal padding
authorOleksii Timofieiev <tim.oleksii@gmail.com>
Wed, 24 Jan 2018 10:49:13 +0000 (13:49 +0300)
committerOleksii Timofieiev <tim.oleksii@gmail.com>
Wed, 24 Jan 2018 10:49:13 +0000 (13:49 +0300)
Signed-off-by: Oleksii Timofieiev <tim.oleksii@gmail.com>
plugin.c

index a9bf6cd03aff4347c0e7cac6833bcbcd8e09e9b5..4750df6bd50b68f2d16d8d645f1572d34057d5ee 100644 (file)
--- 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)