Browse Source

Add support for long options

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.
Dmitry V. Levin 1 month ago
parent
commit
99960ba0a1
3 changed files with 23 additions and 8 deletions
  1. 6
    2
      strace.1.in
  2. 13
    4
      strace.c
  3. 4
    2
      tests/strace-V.test

+ 6
- 2
strace.1.in View File

@@ -981,10 +981,14 @@ and it is ignored at all if used along with one or more instances of
981 981
 .B \-f
982 982
 option.
983 983
 .TP
984
-.B \-h
984
+.BR \-h ,
985
+.TQ
986
+.B \-\-help
985 987
 Print the help summary.
986 988
 .TP
987
-.B \-V
989
+.BR \-V ,
990
+.TQ
991
+.B \-\-version
988 992
 Print the version number of
989 993
 .BR strace .
990 994
 .SH DIAGNOSTICS

+ 13
- 4
strace.c View File

@@ -20,6 +20,7 @@
20 20
 #ifdef HAVE_PATHS_H
21 21
 # include <paths.h>
22 22
 #endif
23
+#include <getopt.h>
23 24
 #include <pwd.h>
24 25
 #include <grp.h>
25 26
 #include <dirent.h>
@@ -308,8 +309,8 @@ Startup:\n\
308 309
 \n\
309 310
 Miscellaneous:\n\
310 311
   -d             enable debug output to stderr\n\
311
-  -h             print help message\n\
312
-  -V             print version\n\
312
+  -h, --help     print help message\n\
313
+  -V, --version  print version\n\
313 314
 "
314 315
 /* ancient, no one should use it
315 316
 -F -- attempt to follow vforks (deprecated, use -f)\n\
@@ -1602,11 +1603,19 @@ init(int argc, char *argv[])
1602 1603
 # error Bug in DEFAULT_QUAL_FLAGS
1603 1604
 #endif
1604 1605
 	qualify("signal=all");
1605
-	while ((c = getopt(argc, argv, "+"
1606
+
1607
+	static const char optstring[] = "+"
1606 1608
 #ifdef ENABLE_STACKTRACE
1607 1609
 	    "k"
1608 1610
 #endif
1609
-	    "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yzZ")) != EOF) {
1611
+	    "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yzZ";
1612
+	static const struct option longopts[] = {
1613
+		{ "help", no_argument, 0, 'h' },
1614
+		{ "version", no_argument, 0, 'V' },
1615
+		{ 0, 0, 0, 0 }
1616
+	};
1617
+
1618
+	while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != EOF) {
1610 1619
 		switch (c) {
1611 1620
 		case 'a':
1612 1621
 			acolumn = string_to_uint(optarg);

+ 4
- 2
tests/strace-V.test View File

@@ -6,7 +6,9 @@
6 6
 run_prog_skip_if_failed date +%Y > /dev/null
7 7
 year="$(date +%Y)"
8 8
 
9
-run_strace -V > "$LOG"
9
+run_strace -V > "$OUT"
10
+run_strace --version > "$LOG"
11
+match_diff "$LOG" "$OUT" '-V and --version output mismatch'
10 12
 
11 13
 getstr()
12 14
 {
@@ -66,4 +68,4 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
66 68
 Optional features enabled:${features}
67 69
 __EOF__
68 70
 
69
-match_diff "$LOG" "$EXP"
71
+match_diff "$OUT" "$EXP"

Loading…
Cancel
Save