]> git.cworth.org Git - apitrace/blobdiff - cli/cli_main.cpp
cli: Add an alias mechanism and alias "retrace" to "replay"
[apitrace] / cli / cli_main.cpp
index e96673ac70e251442e6bd1f20e430dccc164269f..d6db6196121b3d5a9a158c29309c1e7f556a02c8 100644 (file)
@@ -27,7 +27,7 @@
 
 
 /*
- * Top-level application for accessing almost of apitrace
+ * Top-level application for accessing almost all of apitrace
  * functionality.
  */
 
 
 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
 
-typedef void (*command_usage_t) (void);
-typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
-
-typedef struct {
-    const char *name;
-    const char *synopsis;
-    command_usage_t usage;
-    command_function_t function;
-} Command;
-
-#define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
+static const char *help_synopsis = "Print detailed help for the given command.";
 
 static void list_commands(void);
 
 static void
-apitrace_help_usage()
+help_usage()
 {
-    std::cout << "usage: apitrace help [<command>]\n"
-        APITRACE_HELP_SYNOPSIS "\n"
+    std::cout
+        << "usage: apitrace help [<command>]\n"
+        << help_synopsis << "\n"
         "\n";
 
     list_commands();
 }
 
 static int
-apitrace_help_command(int argc, char *argv[], int first_command_arg);
-
-static Command commands[] = {
-    { "dump",
-      APITRACE_DUMP_SYNOPSIS,
-      apitrace_dump_usage,
-      apitrace_dump_command },
-    { "help",
-      APITRACE_HELP_SYNOPSIS,
-      apitrace_help_usage,
-      apitrace_help_command }
+do_help_command(int argc, char *argv[]);
+
+const Command help_command = {
+    "help",
+    help_synopsis,
+    help_usage,
+    do_help_command
+};
+
+static const Command * commands[] = {
+    &diff_command,
+    &diff_state_command,
+    &diff_images_command,
+    &dump_command,
+    &dump_images_command,
+    &pickle_command,
+    &repack_command,
+    &retrace_command,
+    &trace_command,
+    &trim_command,
+    &help_command
+};
+
+/* Aliases provide a mechanism to allow compatibility with old command
+ * names (such as "retrace") for current commands (such as the replay
+ * command). */
+typedef struct {
+    const char *name;
+    const Command *command;
+} Alias;
+
+static const Alias aliases[] = {
+    { "retrace", &retrace_command }
 };
 
 static void
@@ -91,7 +104,7 @@ usage(void)
 
 static void
 list_commands(void) {
-    Command *command;
+    const Command *command;
     int i, max_width = 0;
 
     std::cout << "The available commands are as follows:\n\n";
@@ -99,15 +112,16 @@ list_commands(void) {
     std::cout << std::setiosflags(std::ios::left);
 
     for (i = 0; i < ARRAY_SIZE(commands); i++) {
-        command = &commands[i];
-        if (strlen(command->name) > max_width)
+        command = commands[i];
+        if (strlen(command->name) > max_width) {
             max_width = strlen(command->name);
+        }
     }
 
     for (i = 0; i < ARRAY_SIZE(commands); i++) {
-        command = &commands[i];
+        command = commands[i];
 
-        std::cout << "    " << std::setw(max_width+2) << command->name
+        std::cout << "    " << std::setw(max_width + 2) << command->name
                   << " " << command->synopsis << "\n";
     }
 
@@ -117,20 +131,20 @@ list_commands(void) {
 
 
 static int
-apitrace_help_command(int argc, char *argv[], int first_arg_command)
+do_help_command(int argc, char *argv[])
 {
-    Command *command;
+    const Command *command;
     int i;
 
-    if (first_arg_command == argc) {
-        apitrace_help_usage();
+    if (argc != 2) {
+        help_usage();
         return 0;
     }
 
-    char *command_name = argv[first_arg_command];
+    char *command_name = argv[1];
 
     for (i = 0; i < ARRAY_SIZE(commands); i++) {
-        command = &commands[i];
+        command = commands[i];
 
         if (strcmp(command_name, command->name) == 0) {
             (command->usage) ();
@@ -147,14 +161,10 @@ apitrace_help_command(int argc, char *argv[], int first_arg_command)
 int
 main(int argc, char **argv)
 {
-    const char *command_name = "trace";
-    Command *command;
-    int i, first_command_arg;
-
-    if (argc == 1) {
-        usage();
-        return 1;
-    }
+    const char *command_name;
+    const Command *command;
+    const Alias *alias;
+    int i;
 
     for (i = 1; i < argc; ++i) {
         const char *arg = argv[i];
@@ -164,29 +174,40 @@ main(int argc, char **argv)
         }
 
         if (strcmp(arg, "--help") == 0) {
-            return apitrace_help_command (0, NULL, 0);
+            return do_help_command(0, NULL);
         } else {
             std::cerr << "Error: unknown option " << arg << "\n";
             usage();
             return 1;
         }
     }
-    first_command_arg = i;
 
-    if (first_command_arg < argc) {
-        command_name = argv[first_command_arg];
-        first_command_arg++;
+    if (i == argc) {
+        usage();
+        return 1;
     }
 
+    command_name = argv[i];
+
+    argc -= i;
+    argv = &argv[i];
+
     for (i = 0; i < ARRAY_SIZE(commands); i++) {
-        command = &commands[i];
+        command = commands[i];
 
         if (strcmp(command_name, command->name) == 0)
-            return (command->function) (argc, argv, first_command_arg);
+            return (command->function) (argc, argv);
+    }
+
+    for (i = 0; i < ARRAY_SIZE(aliases); i++) {
+        alias = &aliases[i];
+
+        if (strcmp(command_name, alias->name) == 0)
+            return (alias->command->function) (argc, argv);
     }
 
     std::cerr << "Error: unknown command " << command_name
               << " (see \"apitrace help\").\n";
 
-    return 1;
+   return 1;
 }