This source file includes following definitions.
- parse_long_options
- parse_gnu_standard_options_only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <config.h>
22
23
24 #include "long-options.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30
31 #include "version-etc.h"
32 #include "exitfail.h"
33
34 static struct option const long_options[] =
35 {
36 {"help", no_argument, NULL, 'h'},
37 {"version", no_argument, NULL, 'v'},
38 {NULL, 0, NULL, 0}
39 };
40
41
42
43
44 void
45 parse_long_options (int argc,
46 char **argv,
47 const char *command_name,
48 const char *package,
49 const char *version,
50 void (*usage_func) (int),
51 ...)
52 {
53 int c;
54 int saved_opterr;
55
56 saved_opterr = opterr;
57
58
59 opterr = 0;
60
61 if (argc == 2
62 && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
63 {
64 switch (c)
65 {
66 case 'h':
67 (*usage_func) (EXIT_SUCCESS);
68 break;
69
70 case 'v':
71 {
72 va_list authors;
73 va_start (authors, usage_func);
74 version_etc_va (stdout, command_name, package, version, authors);
75 exit (EXIT_SUCCESS);
76 }
77
78 default:
79
80 break;
81 }
82 }
83
84
85 opterr = saved_opterr;
86
87
88
89 optind = 0;
90 }
91
92
93
94
95
96
97 void
98 parse_gnu_standard_options_only (int argc,
99 char **argv,
100 const char *command_name,
101 const char *package,
102 const char *version,
103 bool scan_all,
104 void (*usage_func) (int),
105 ...)
106 {
107 int c;
108 int saved_opterr = opterr;
109
110
111 opterr = 1;
112
113 const char *optstring = scan_all ? "" : "+";
114
115 if ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1)
116 {
117 switch (c)
118 {
119 case 'h':
120 (*usage_func) (EXIT_SUCCESS);
121 break;
122
123 case 'v':
124 {
125 va_list authors;
126 va_start (authors, usage_func);
127 version_etc_va (stdout, command_name, package, version, authors);
128 exit (EXIT_SUCCESS);
129 }
130
131 default:
132 (*usage_func) (exit_failure);
133 break;
134 }
135 }
136
137
138 opterr = saved_opterr;
139 }