#include <string.h>
+#include <limits.h> // for CHAR_MAX
+#include <getopt.h>
#include "pickle.hpp"
usage(void)
{
std::cout
- << "usage: apitrace pickle [OPTIONS] <trace-file>...\n"
+ << "usage: apitrace pickle [OPTIONS] TRACE_FILE...\n"
<< synopsis << "\n"
"\n"
- " --calls <CALLSET> Only pickle specified calls\n"
+ " -h, --help show this help message and exit\n"
+ " --calls=CALLSET only dump specified calls\n"
;
}
-static int
-command(int argc, char *argv[])
-{
- int i;
-
- for (i = 1; i < argc;) {
- const char *arg = argv[i];
+enum {
+ CALLS_OPT = CHAR_MAX + 1,
+};
- if (arg[0] != '-') {
- break;
- }
+const static char *
+shortOptions = "h";
- ++i;
+const static struct option
+longOptions[] = {
+ {"help", no_argument, 0, 'h'},
+ {"calls", required_argument, 0, CALLS_OPT},
+ {0, 0, 0, 0}
+};
- if (!strcmp(arg, "--")) {
- break;
- } else if (!strcmp(arg, "--help")) {
+static int
+command(int argc, char *argv[])
+{
+ int opt;
+ while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
+ switch (opt) {
+ case 'h':
usage();
return 0;
- } else if (!strcmp(arg, "--calls")) {
- calls = trace::CallSet(argv[i++]);
- } else {
- std::cerr << "error: unknown option " << arg << "\n";
+ case CALLS_OPT:
+ calls = trace::CallSet(optarg);
+ break;
+ default:
+ std::cerr << "error: unexpected option `" << opt << "`\n";
usage();
return 1;
}
os::setBinaryMode(stdout);
- for (; i < argc; ++i) {
+ for (int i = optind; i < argc; ++i) {
trace::Parser parser;
if (!parser.open(argv[i])) {