From b1b5a38db4f5e7ce5798685dc8c9b6f0bd3e99de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 20 Apr 2012 22:49:20 +0100 Subject: [PATCH] Use getopt in other cli commands. --- cli/cli_diff_state.cpp | 38 ++++++++++++++++++++------------------ cli/cli_repack.cpp | 35 +++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/cli/cli_diff_state.cpp b/cli/cli_diff_state.cpp index 8536e32..7f75484 100644 --- a/cli/cli_diff_state.cpp +++ b/cli/cli_diff_state.cpp @@ -26,6 +26,8 @@ *********************************************************************/ #include +#include + #include #include "cli.hpp" @@ -45,32 +47,32 @@ usage(void) " Both input files should be the result of running 'glretrace -D XYZ '.\n"; } +const static char * +shortOptions = "h"; + +const static struct option +longOptions[] = { + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} +}; + static int command(int argc, char *argv[]) { - int i; - - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - i++; - break; - } else if (!strcmp(arg, "--help")) { + int opt; + while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { + switch (opt) { + case 'h': usage(); return 0; - } else { - std::cerr << "error: unknown option " << arg << "\n"; + default: + std::cerr << "error: unexpected option `" << opt << "`\n"; usage(); return 1; } } - if (argc - i != 2) { + if (argc != optind + 2) { std::cerr << "Error: diff-state requires exactly two state-dump files as arguments.\n"; usage(); return 1; @@ -78,8 +80,8 @@ command(int argc, char *argv[]) char *file1, *file2; - file1 = argv[i]; - file2 = argv[i+1]; + file1 = argv[optind]; + file2 = argv[optind + 1]; os::String command = trace::findScript("jsondiff.py"); diff --git a/cli/cli_repack.cpp b/cli/cli_repack.cpp index a92723d..6f08896 100644 --- a/cli/cli_repack.cpp +++ b/cli/cli_repack.cpp @@ -25,6 +25,8 @@ #include +#include + #include #include "cli.hpp" @@ -46,6 +48,15 @@ usage(void) << "\n"; } +const static char * +shortOptions = "h"; + +const static struct option +longOptions[] = { + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} +}; + static int repack(const char *inFileName, const char *outFileName) { @@ -78,34 +89,26 @@ repack(const char *inFileName, const char *outFileName) static int command(int argc, char *argv[]) { - int i; - - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - break; - } else if (strcmp(arg, "--help") == 0) { + int opt; + while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { + switch (opt) { + case 'h': usage(); return 0; - } else { - std::cerr << "error: unknown option " << arg << "\n"; + default: + std::cerr << "error: unexpected option `" << opt << "`\n"; usage(); return 1; } } - if (argc != i + 2) { + if (argc != optind + 2) { std::cerr << "error: insufficient number of arguments\n"; usage(); return 1; } - return repack(argv[i], argv[i + 1]); + return repack(argv[optind], argv[optind + 1]); } const Command repack_command = { -- 2.43.0