]> git.cworth.org Git - apitrace/commitdiff
Use getopt on apitrace pickle.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 18 Feb 2012 15:40:59 +0000 (15:40 +0000)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 18 Feb 2012 15:40:59 +0000 (15:40 +0000)
cli/cli_pickle.cpp

index c37c0f8bf8c85d00d4a83484bb9eab294c903c6d..8fdcd448afb9f4cafec24dffc187f0b4fb27e0f0 100644 (file)
@@ -25,6 +25,8 @@
 
 
 #include <string.h>
+#include <limits.h> // for CHAR_MAX
+#include <getopt.h>
 
 #include "pickle.hpp"
 
@@ -151,36 +153,42 @@ static void
 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;
         }
@@ -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])) {