From 312ed10900df878ef8476febe72c8f1308aba106 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Fri, 20 Jun 2014 12:18:48 -0700 Subject: Add --help and --version long options. Also, make the convention for long-only options be that their case-statement values start at 128, so they avoid colliding with any ASCII code points, including control characters. Make the tables of long options "static const" while we're at it, and get rid of unnecessary casts. Change-Id: I55702a85e9bc078b1cd0f2803ebb68a710405bab Reviewed-on: https://code.wireshark.org/review/2491 Reviewed-by: Guy Harris --- capture_opts.h | 31 +++++++++++++++++++++++-------- dumpcap.c | 8 +++++--- tshark.c | 4 +++- ui/qt/main.cpp | 6 ++++-- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/capture_opts.h b/capture_opts.h index f9bdf89eb6..a48635d68b 100644 --- a/capture_opts.h +++ b/capture_opts.h @@ -40,15 +40,30 @@ extern "C" { #endif /* __cplusplus */ -/* Attention: - for tshark, we're using a leading - in the optstring to prevent getopt() - from permuting the argv[] entries, in this case, unknown argv[] entries - will be returned as parameters to a dummy-option 1 - in short: we must not use 1 here */ - -/* this does not clash with tshark's -2 option which returns '2' */ -#define LONGOPT_NUM_CAP_COMMENT 2 +/* + * Long options. + * We do not currently have long options corresponding to all short + * options; we should probably pick appropriate option names for them. + * + * For long options with no corresponding short options, we define values + * outside the range of ASCII graphic characters, make that the last + * component of the entry for the long option, and have a case for that + * option in the switch statement. + * + * NOTE: + * for tshark, we're using a leading - in the optstring to prevent getopt() + * from permuting the argv[] entries, in this case, unknown argv[] entries + * will be returned as parameters to a dummy-option 1. + * In short: we must not use 1 here, which is another reason to use + * values outside the range of ASCII graphic characters. + */ +#define LONGOPT_NUM_CAP_COMMENT 128 +/* + * Non-capture long-only options should start here, to avoid collision + * with capture options. + */ +#define MIN_NON_CAPTURE_LONGOPT 129 #ifdef HAVE_PCAP_REMOTE /* Type of capture source */ diff --git a/dumpcap.c b/dumpcap.c index 2ceed867a8..1a94d9fceb 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -4176,8 +4176,10 @@ main(int argc, char *argv[]) GString *comp_info_str; GString *runtime_info_str; int opt; - struct option long_options[] = { + static const struct option long_options[] = { {(char *)"capture-comment", required_argument, NULL, LONGOPT_NUM_CAP_COMMENT }, + {(char *)"help", no_argument, NULL, 'h'}, + {(char *)"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; @@ -4525,8 +4527,8 @@ main(int argc, char *argv[]) global_capture_opts.saving_to_file = TRUE; global_capture_opts.has_ring_num_files = TRUE; - /* Pass on capture_child mode for capture_opts */ - global_capture_opts.capture_child = capture_child; + /* Pass on capture_child mode for capture_opts */ + global_capture_opts.capture_child = capture_child; /* Now get our args */ while ((opt = getopt_long(argc, argv, OPTSTRING, long_options, NULL)) != -1) { diff --git a/tshark.c b/tshark.c index 539792cd74..c50d28a353 100644 --- a/tshark.c +++ b/tshark.c @@ -922,8 +922,10 @@ main(int argc, char *argv[]) GString *runtime_info_str; char *init_progfile_dir_error; int opt; - struct option long_options[] = { + static const struct option long_options[] = { {(char *)"capture-comment", required_argument, NULL, LONGOPT_NUM_CAP_COMMENT }, + {(char *)"help", no_argument, NULL, 'h'}, + {(char *)"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; gboolean arg_error = FALSE; diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index 871d0e92a0..c91937e846 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -516,8 +516,10 @@ int main(int argc, char *argv[]) #endif #define OPTSTRING "a:b:" OPTSTRING_B "c:C:Df:g:Hhi:" OPTSTRING_I "jJ:kK:lLm:nN:o:P:pQr:R:Ss:t:u:vw:X:y:z:" - struct option long_options[] = { - {(char *)"read-file", required_argument, NULL, (int)'r' }, + static const struct option long_options[] = { + {(char *)"help", no_argument, NULL, 'h'}, + {(char *)"read-file", required_argument, NULL, 'r' }, + {(char *)"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; static const char optstring[] = OPTSTRING; -- cgit v1.2.1