]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
Make help and version generic commands
authorKeith Busch <keith.busch@intel.com>
Tue, 7 Jun 2016 22:38:40 +0000 (16:38 -0600)
committerKeith Busch <keith.busch@intel.com>
Wed, 8 Jun 2016 18:25:22 +0000 (12:25 -0600)
Plugins don't need to implement these.

Signed-off-by: Keith Busch <keith.busch@intel.com>
nvme-builtin.h
nvme.c
plugin.c

index 84c49fc260c9795c0782e509854b855373ab8580..d8dc8989bc76bf00749ad1c300fcd5ddebcb5e73 100644 (file)
@@ -58,8 +58,6 @@ COMMAND_LIST(
        ENTRY("connect-all", "Discover and Connect to NVMeoF subsystems", connect_all_cmd)
        ENTRY("connect", "Connect to NVMeoF subsystem", connect_cmd)
        ENTRY("disconnect", "Disconnect from NVMeoF subsystem", disconnect_cmd)
-       ENTRY("version", "Shows the program version", version)
-       ENTRY("help", "Display this help", help)
 );
 
 #endif
diff --git a/nvme.c b/nvme.c
index bc7e8945fb608e0db3f801a5c3d820e29dfe7987..193096fdf7ed9454a4e69c7155abf6fc9eff8eec 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2888,29 +2888,6 @@ static int disconnect_cmd(int argc, char **argv, struct command *command, struct
        return disconnect(desc, argc, argv);
 }
 
-static int version(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
-       printf("nvme version %s\n", nvme_version_string);
-       return 0;
-}
-
-static int help(int argc, char **argv, struct command *command, struct plugin *plugin)
-{
-       char man[0x100];
-
-       if (argc == 1) {
-               general_help(plugin);
-               return 0;
-       }
-
-       sprintf(man, "nvme-%s", argv[1]);
-       if (execlp("man", "man", man, (char *)NULL)) {
-               perror(argv[1]);
-               exit(errno);
-       }
-       return 0;
-}
-
 void register_extention(struct plugin *plugin)
 {
        plugin->parent = &nvme;
index 06950b1727eeebe5e37cf415a0fb6b00d4e543e3..9fbf8407ed6ad642871a0a79f8de1460ad7249dc 100644 (file)
--- a/plugin.c
+++ b/plugin.c
@@ -1,14 +1,47 @@
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "plugin.h"
 #include "argconfig.h"
 
+static int version(struct plugin *plugin)
+{
+       struct program *prog = plugin->parent;
+
+       if (plugin->name)
+               printf("%s %s version %s\n", prog->name, plugin->name, prog->version);
+       else
+               printf("%s version %s\n", prog->name, prog->version);
+       return 0;
+}
+
+static int help(int argc, char **argv, struct plugin *plugin)
+{
+       char man[0x100];
+
+       if (argc == 1) {
+               general_help(plugin);
+               return 0;
+       }
+
+       sprintf(man, "%s-%s", plugin->name, argv[1]);
+       if (execlp("man", "man", man, (char *)NULL)) {
+               perror(argv[1]);
+               exit(errno);
+       }
+       return 0;
+}
+
 void usage(struct plugin *plugin)
 {
        struct program *prog = plugin->parent;
 
-       printf("usage: %s %s\n", prog->name, prog->usage);
+       if (plugin->name)
+               printf("usage: %s %s %s\n", prog->name, plugin->name, prog->usage);
+       else
+               printf("usage: %s %s\n", prog->name, prog->usage);
 }
 
 void general_help(struct plugin *plugin)
@@ -36,6 +69,9 @@ void general_help(struct plugin *plugin)
        for (; plugin->commands[i]; i++)
                printf("  %-*s %s\n", 15, 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("\n");
 
        if (plugin->name)
@@ -68,6 +104,7 @@ int handle_plugin(int argc, char **argv, struct plugin *plugin)
        unsigned i = 0;
        char *str = argv[0];
        char use[0x100];
+
        struct plugin *extension;
        struct program *prog = plugin->parent;
 
@@ -89,6 +126,10 @@ int handle_plugin(int argc, char **argv, struct plugin *plugin)
        for (; plugin->commands[i]; i++) {
                struct command *cmd = plugin->commands[i];
 
+               if (!strcmp(str, "help"))
+                       return help(argc, argv, plugin);
+               if (!strcmp(str, "version"))
+                       return version(plugin);
                if (strcmp(str, cmd->name))
                        continue;