]> granicus.if.org Git - strace/commitdiff
Add support for long options
authorDmitry V. Levin <ldv@altlinux.org>
Tue, 24 Sep 2019 21:35:26 +0000 (21:35 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 25 Sep 2019 01:02:03 +0000 (01:02 +0000)
Recognize --help and --version options as aliases to -h and -V options,
respectively.

* strace.c: Include <getopt.h>.
(init): Move short options to optstring, add longopts array, use
getopt_long instead of getopt.
(usage): Document --help and --version options.
* strace.1.in: Likewise.
* tests/strace-V.test: Check that "strace --version" output is the same
as "strace -V" output.

strace.1.in
strace.c
tests/strace-V.test

index 0d22dca2240f646eac7211828256ca783738fc5b..bba3bc3b612d11f6d3ae1eb283336fcb08dae6f5 100644 (file)
@@ -981,10 +981,14 @@ and it is ignored at all if used along with one or more instances of
 .B \-f
 option.
 .TP
-.B \-h
+.BR \-h ,
+.TQ
+.B \-\-help
 Print the help summary.
 .TP
-.B \-V
+.BR \-V ,
+.TQ
+.B \-\-version
 Print the version number of
 .BR strace .
 .SH DIAGNOSTICS
index e8ced366db0cab527c5dd1dc5ee57d9a8c8699f0..701e57393ef51ca1231edb1f24600b8e678fa056 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -20,6 +20,7 @@
 #ifdef HAVE_PATHS_H
 # include <paths.h>
 #endif
+#include <getopt.h>
 #include <pwd.h>
 #include <grp.h>
 #include <dirent.h>
@@ -308,8 +309,8 @@ Startup:\n\
 \n\
 Miscellaneous:\n\
   -d             enable debug output to stderr\n\
-  -h             print help message\n\
-  -V             print version\n\
+  -h, --help     print help message\n\
+  -V, --version  print version\n\
 "
 /* ancient, no one should use it
 -F -- attempt to follow vforks (deprecated, use -f)\n\
@@ -1602,11 +1603,19 @@ init(int argc, char *argv[])
 # error Bug in DEFAULT_QUAL_FLAGS
 #endif
        qualify("signal=all");
-       while ((c = getopt(argc, argv, "+"
+
+       static const char optstring[] = "+"
 #ifdef ENABLE_STACKTRACE
            "k"
 #endif
-           "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yzZ")) != EOF) {
+           "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yzZ";
+       static const struct option longopts[] = {
+               { "help", no_argument, 0, 'h' },
+               { "version", no_argument, 0, 'V' },
+               { 0, 0, 0, 0 }
+       };
+
+       while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != EOF) {
                switch (c) {
                case 'a':
                        acolumn = string_to_uint(optarg);
index b1fd5ade9e99bf0e19441b6e9ce1559cb9dbe388..1f5e6825e1f8d92715ecdc07022cccefc8854a02 100755 (executable)
@@ -6,7 +6,9 @@
 run_prog_skip_if_failed date +%Y > /dev/null
 year="$(date +%Y)"
 
-run_strace -V > "$LOG"
+run_strace -V > "$OUT"
+run_strace --version > "$LOG"
+match_diff "$LOG" "$OUT" '-V and --version output mismatch'
 
 getstr()
 {
@@ -66,4 +68,4 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 Optional features enabled:${features}
 __EOF__
 
-match_diff "$LOG" "$EXP"
+match_diff "$OUT" "$EXP"