From: José Fonseca Date: Sun, 30 Oct 2011 14:21:03 +0000 (+0000) Subject: Better isolation of CLI source files. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=3c167fe5060d5fcd49422ef47c9602f6aedd978c;p=apitrace Better isolation of CLI source files. --- diff --git a/cli/cli.hpp b/cli/cli.hpp index 1bdc689..bc2dcf9 100644 --- a/cli/cli.hpp +++ b/cli/cli.hpp @@ -28,12 +28,20 @@ #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_ */ diff --git a/cli/cli_dump.cpp b/cli/cli_dump.cpp index 3a428db..9f7752e 100644 --- a/cli/cli_dump.cpp +++ b/cli/cli_dump.cpp @@ -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] ...\n" - APITRACE_DUMP_SYNOPSIS "\n" + std::cout + << "usage: apitrace dump [OPTIONS] ...\n" + << synopsis << "\n" "\n" " --color=\n" " --colour= 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 +}; diff --git a/cli/cli_main.cpp b/cli/cli_main.cpp index e96673a..fd4dbbb 100644 --- a/cli/cli_main.cpp +++ b/cli/cli_main.cpp @@ -40,42 +40,34 @@ #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 []\n" - APITRACE_HELP_SYNOPSIS "\n" + std::cout + << "usage: apitrace help []\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);