From 589a579c395bd9d49e82cd94d7c20faaa550f400 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 18 Feb 2012 15:40:59 +0000 Subject: [PATCH] Use getopt on apitrace pickle. --- cli/cli_pickle.cpp | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/cli/cli_pickle.cpp b/cli/cli_pickle.cpp index c37c0f8..8fdcd44 100644 --- a/cli/cli_pickle.cpp +++ b/cli/cli_pickle.cpp @@ -25,6 +25,8 @@ #include +#include // for CHAR_MAX +#include #include "pickle.hpp" @@ -151,36 +153,42 @@ static void usage(void) { std::cout - << "usage: apitrace pickle [OPTIONS] ...\n" + << "usage: apitrace pickle [OPTIONS] TRACE_FILE...\n" << synopsis << "\n" "\n" - " --calls 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; } @@ -188,7 +196,7 @@ command(int argc, char *argv[]) os::setBinaryMode(stdout); - for (; i < argc; ++i) { + for (int i = optind; i < argc; ++i) { trace::Parser parser; if (!parser.open(argv[i])) { -- 2.45.2