]> git.cworth.org Git - apitrace/commitdiff
Better isolation of CLI source files.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 30 Oct 2011 14:21:03 +0000 (14:21 +0000)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 30 Oct 2011 14:21:03 +0000 (14:21 +0000)
cli/cli.hpp
cli/cli_dump.cpp
cli/cli_main.cpp

index 1bdc689b392f142858b4875d948312125e678801..bc2dcf9f4a019dccb212ee54368143be56f5b94f 100644 (file)
 #ifndef _APITRACE_CLI_HPP_
 #define _APITRACE_CLI_HPP_
 
-#define APITRACE_DUMP_SYNOPSIS "Dump given trace(s) to standard output."
 
-void
-apitrace_dump_usage(void);
+struct Command {
+    const char *name;
+    const char *synopsis;
+
+    typedef void (*Usage) (void);
+    Usage usage;
+
+    typedef int (*Function) (int argc, char *argv[], int first_command_arg);
+    Function function;
+};
+
+
+extern const Command dump;
 
-int
-apitrace_dump_command(int argc, char *argv[], int first_command_arg);
 
 #endif /* _APITRACE_CLI_HPP_ */
index 3a428dbb7909456221dc147f5a142b5f490dcc6b..9f7752ec3caa904055a468767779a96dabe4b03b 100644 (file)
@@ -37,19 +37,22 @@ enum ColorOption {
 
 static ColorOption color = COLOR_OPTION_AUTO;
 
-void
-apitrace_dump_usage(void)
+static const char *synopsis = "Dump given trace(s) to standard output.";
+
+static void
+usage(void)
 {
-    std::cout << "usage: apitrace dump [OPTIONS] <trace-file>...\n"
-        APITRACE_DUMP_SYNOPSIS "\n"
+    std::cout
+        << "usage: apitrace dump [OPTIONS] <trace-file>...\n"
+        << synopsis << "\n"
         "\n"
         "    --color=<WHEN>\n"
         "    --colour=<WHEN>     Colored syntax highlighting\n"
         "                        WHEN is 'auto', 'always', or 'never'\n";
 }
 
-int
-apitrace_dump_command(int argc, char *argv[], int first_arg_command)
+static int
+command(int argc, char *argv[], int first_arg_command)
 {
     int i;
 
@@ -63,7 +66,7 @@ apitrace_dump_command(int argc, char *argv[], int first_arg_command)
         if (!strcmp(arg, "--")) {
             break;
         } else if (!strcmp(arg, "--help")) {
-            apitrace_dump_usage();
+            usage();
             return 0;
         } else if (!strcmp(arg, "--color=auto") ||
                    !strcmp(arg, "--colour=auto")) {
@@ -80,7 +83,7 @@ apitrace_dump_command(int argc, char *argv[], int first_arg_command)
             color = COLOR_OPTION_NEVER;
         } else {
             std::cerr << "error: unknown option " << arg << "\n";
-            apitrace_dump_usage();
+            usage();
             return 1;
         }
     }
@@ -110,3 +113,10 @@ apitrace_dump_command(int argc, char *argv[], int first_arg_command)
 
     return 0;
 }
+
+const Command dump = {
+    "dump",
+    synopsis,
+    usage,
+    command
+};
index e96673ac70e251442e6bd1f20e430dccc164269f..fd4dbbb53db2f07d3b59db1787a458dbce8b2b9f 100644 (file)
 
 #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 }
+help_command(int argc, char *argv[], int first_command_arg);
+
+const Command help = {
+    "help",
+    help_synopsis,
+    help_usage,
+    help_command
+};
+
+static const Command * commands[] = {
+    &dump,
+    &help,
 };
 
 static void
@@ -91,7 +83,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 +91,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 +110,20 @@ list_commands(void) {
 
 
 static int
-apitrace_help_command(int argc, char *argv[], int first_arg_command)
+help_command(int argc, char *argv[], int first_arg_command)
 {
-    Command *command;
+    const Command *command;
     int i;
 
     if (first_arg_command == argc) {
-        apitrace_help_usage();
+        help_usage();
         return 0;
     }
 
     char *command_name = argv[first_arg_command];
 
     for (i = 0; i < ARRAY_SIZE(commands); i++) {
-        command = &commands[i];
+        command = commands[i];
 
         if (strcmp(command_name, command->name) == 0) {
             (command->usage) ();
@@ -148,7 +141,7 @@ int
 main(int argc, char **argv)
 {
     const char *command_name = "trace";
-    Command *command;
+    const Command *command;
     int i, first_command_arg;
 
     if (argc == 1) {
@@ -164,7 +157,7 @@ main(int argc, char **argv)
         }
 
         if (strcmp(arg, "--help") == 0) {
-            return apitrace_help_command (0, NULL, 0);
+            return help_command (0, NULL, 0);
         } else {
             std::cerr << "Error: unknown option " << arg << "\n";
             usage();
@@ -179,7 +172,7 @@ main(int argc, char **argv)
     }
 
     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);