]> git.cworth.org Git - apitrace/blob - cli/cli_main.cpp
Tweak help output.
[apitrace] / cli / cli_main.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Intel Corporation
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  *********************************************************************/
27
28
29 /*
30  * Top-level application for accessing almost of apitrace
31  * functionality.
32  */
33
34 #include <string.h>
35
36 #include <iomanip>
37 #include <iostream>
38
39 #include "cli.hpp"
40
41 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
42
43 typedef void (*command_usage_t) (void);
44 typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
45
46 typedef struct {
47     const char *name;
48     const char *synopsis;
49     command_usage_t usage;
50     command_function_t function;
51 } Command;
52
53 #define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
54
55 static void list_commands(void);
56
57 static void
58 apitrace_help_usage()
59 {
60     std::cout << "usage: apitrace help [<command>]\n"
61         APITRACE_HELP_SYNOPSIS "\n"
62         "\n";
63
64     list_commands();
65 }
66
67 static int
68 apitrace_help_command(int argc, char *argv[], int first_command_arg);
69
70 static Command commands[] = {
71     { "dump",
72       APITRACE_DUMP_SYNOPSIS,
73       apitrace_dump_usage,
74       apitrace_dump_command },
75     { "help",
76       APITRACE_HELP_SYNOPSIS,
77       apitrace_help_usage,
78       apitrace_help_command }
79 };
80
81 static void
82 usage(void)
83 {
84     std::cout <<
85         "Usage: apitrace <command> [<args> ...]\n"
86         "Top-level command line frontend to apitrace.\n"
87         "\n";
88
89     list_commands();
90 }
91
92 static void
93 list_commands(void) {
94     Command *command;
95     int i, max_width = 0;
96
97     std::cout << "The available commands are as follows:\n\n";
98
99     std::cout << std::setiosflags(std::ios::left);
100
101     for (i = 0; i < ARRAY_SIZE(commands); i++) {
102         command = &commands[i];
103         if (strlen(command->name) > max_width)
104             max_width = strlen(command->name);
105     }
106
107     for (i = 0; i < ARRAY_SIZE(commands); i++) {
108         command = &commands[i];
109
110         std::cout << "    " << std::setw(max_width+2) << command->name
111                   << " " << command->synopsis << "\n";
112     }
113
114     std::cout << "\n"
115         "Use \"apitrace help <command>\" for more details on each command.\n";
116 }
117
118
119 static int
120 apitrace_help_command(int argc, char *argv[], int first_arg_command)
121 {
122     Command *command;
123     int i;
124
125     if (first_arg_command == argc) {
126         apitrace_help_usage();
127         return 0;
128     }
129
130     char *command_name = argv[first_arg_command];
131
132     for (i = 0; i < ARRAY_SIZE(commands); i++) {
133         command = &commands[i];
134
135         if (strcmp(command_name, command->name) == 0) {
136             (command->usage) ();
137             return 0;
138         }
139     }
140
141     std::cerr << "Error: Unknown command: " << command_name
142               << " (see \"apitrace help\").\n";
143
144     return 1;
145 }
146
147 int
148 main(int argc, char **argv)
149 {
150     const char *command_name = "trace";
151     Command *command;
152     int i, first_command_arg;
153
154     if (argc == 1) {
155         usage();
156         return 1;
157     }
158
159     for (i = 1; i < argc; ++i) {
160         const char *arg = argv[i];
161
162         if (arg[0] != '-') {
163             break;
164         }
165
166         if (strcmp(arg, "--help") == 0) {
167             return apitrace_help_command (0, NULL, 0);
168         } else {
169             std::cerr << "Error: unknown option " << arg << "\n";
170             usage();
171             return 1;
172         }
173     }
174     first_command_arg = i;
175
176     if (first_command_arg < argc) {
177         command_name = argv[first_command_arg];
178         first_command_arg++;
179     }
180
181     for (i = 0; i < ARRAY_SIZE(commands); i++) {
182         command = &commands[i];
183
184         if (strcmp(command_name, command->name) == 0)
185             return (command->function) (argc, argv, first_command_arg);
186     }
187
188     std::cerr << "Error: unknown command " << command_name
189               << " (see \"apitrace help\").\n";
190
191     return 1;
192 }