1 #include "git-compat-util.h"
2 #include "parse-options.h"
13 static inline const char *get_arg(struct optparse_t
*p
)
16 const char *res
= p
->opt
;
24 static inline const char *skip_prefix(const char *str
, const char *prefix
)
26 size_t len
= strlen(prefix
);
27 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
30 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
32 if (flags
& OPT_SHORT
)
33 return error("switch `%c' %s", opt
->short_name
, reason
);
34 if (flags
& OPT_UNSET
)
35 return error("option `no-%s' %s", opt
->long_name
, reason
);
36 return error("option `%s' %s", opt
->long_name
, reason
);
39 static int get_value(struct optparse_t
*p
,
40 const struct option
*opt
, int flags
)
43 const int unset
= flags
& OPT_UNSET
;
46 return opterror(opt
, "takes no value", flags
);
47 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
48 return opterror(opt
, "isn't available", flags
);
50 if (!(flags
& OPT_SHORT
) && p
->opt
) {
53 if (!(opt
->flags
& PARSE_OPT_NOARG
))
60 return opterror(opt
, "takes no value", flags
);
66 arg
= p
->opt
? p
->opt
: (p
->argc
> 1 ? p
->argv
[1] : NULL
);
70 *(int *)opt
->value
&= ~opt
->defval
;
72 *(int *)opt
->value
|= opt
->defval
;
76 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
80 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
84 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
89 *(const char **)opt
->value
= NULL
;
92 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
93 *(const char **)opt
->value
= (const char *)opt
->defval
;
97 return opterror(opt
, "requires a value", flags
);
98 *(const char **)opt
->value
= get_arg(p
);
101 case OPTION_CALLBACK
:
103 return (*opt
->callback
)(opt
, NULL
, 1);
104 if (opt
->flags
& PARSE_OPT_NOARG
)
105 return (*opt
->callback
)(opt
, NULL
, 0);
106 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
107 return (*opt
->callback
)(opt
, NULL
, 0);
109 return opterror(opt
, "requires a value", flags
);
110 return (*opt
->callback
)(opt
, get_arg(p
), 0);
114 *(int *)opt
->value
= 0;
117 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
118 *(int *)opt
->value
= opt
->defval
;
122 return opterror(opt
, "requires a value", flags
);
123 *(int *)opt
->value
= strtol(get_arg(p
), (char **)&s
, 10);
125 return opterror(opt
, "expects a numerical value", flags
);
129 die("should not happen, someone must be hit on the forehead");
133 static int parse_short_opt(struct optparse_t
*p
, const struct option
*options
)
135 for (; options
->type
!= OPTION_END
; options
++) {
136 if (options
->short_name
== *p
->opt
) {
137 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
138 return get_value(p
, options
, OPT_SHORT
);
141 return error("unknown switch `%c'", *p
->opt
);
144 static int parse_long_opt(struct optparse_t
*p
, const char *arg
,
145 const struct option
*options
)
147 const char *arg_end
= strchr(arg
, '=');
148 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
149 int abbrev_flags
= 0, ambiguous_flags
= 0;
152 arg_end
= arg
+ strlen(arg
);
154 for (; options
->type
!= OPTION_END
; options
++) {
158 if (!options
->long_name
)
161 rest
= skip_prefix(arg
, options
->long_name
);
164 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
168 * If this is abbreviated, it is
169 * ambiguous. So when there is no
170 * exact match later, we need to
173 ambiguous_option
= abbrev_option
;
174 ambiguous_flags
= abbrev_flags
;
176 if (!(flags
& OPT_UNSET
) && *arg_end
)
177 p
->opt
= arg_end
+ 1;
178 abbrev_option
= options
;
179 abbrev_flags
= flags
;
182 /* negated and abbreviated very much? */
183 if (!prefixcmp("no-", arg
)) {
188 if (strncmp(arg
, "no-", 3))
191 rest
= skip_prefix(arg
+ 3, options
->long_name
);
192 /* abbreviated and negated? */
193 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
203 return get_value(p
, options
, flags
);
206 if (ambiguous_option
)
207 return error("Ambiguous option: %s "
208 "(could be --%s%s or --%s%s)",
210 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
211 ambiguous_option
->long_name
,
212 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
213 abbrev_option
->long_name
);
215 return get_value(p
, abbrev_option
, abbrev_flags
);
216 return error("unknown option `%s'", arg
);
219 void check_typos(const char *arg
, const struct option
*options
)
224 if (!prefixcmp(arg
, "no-")) {
225 error ("did you mean `--%s` (with two dashes ?)", arg
);
229 for (; options
->type
!= OPTION_END
; options
++) {
230 if (!options
->long_name
)
232 if (!prefixcmp(options
->long_name
, arg
)) {
233 error ("did you mean `--%s` (with two dashes ?)", arg
);
239 static NORETURN
void usage_with_options_internal(const char * const *,
240 const struct option
*, int);
242 int parse_options(int argc
, const char **argv
, const struct option
*options
,
243 const char * const usagestr
[], int flags
)
245 struct optparse_t args
= { argv
+ 1, argc
- 1, NULL
};
248 for (; args
.argc
; args
.argc
--, args
.argv
++) {
249 const char *arg
= args
.argv
[0];
251 if (*arg
!= '-' || !arg
[1]) {
252 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
254 argv
[j
++] = args
.argv
[0];
260 if (*args
.opt
== 'h')
261 usage_with_options(usagestr
, options
);
262 if (parse_short_opt(&args
, options
) < 0)
263 usage_with_options(usagestr
, options
);
265 check_typos(arg
+ 1, options
);
267 if (*args
.opt
== 'h')
268 usage_with_options(usagestr
, options
);
269 if (parse_short_opt(&args
, options
) < 0)
270 usage_with_options(usagestr
, options
);
275 if (!arg
[2]) { /* "--" */
276 if (!(flags
& PARSE_OPT_KEEP_DASHDASH
)) {
283 if (!strcmp(arg
+ 2, "help-all"))
284 usage_with_options_internal(usagestr
, options
, 1);
285 if (!strcmp(arg
+ 2, "help"))
286 usage_with_options(usagestr
, options
);
287 if (parse_long_opt(&args
, arg
+ 2, options
))
288 usage_with_options(usagestr
, options
);
291 memmove(argv
+ j
, args
.argv
, args
.argc
* sizeof(*argv
));
292 argv
[j
+ args
.argc
] = NULL
;
293 return j
+ args
.argc
;
296 #define USAGE_OPTS_WIDTH 24
299 void usage_with_options_internal(const char * const *usagestr
,
300 const struct option
*opts
, int full
)
302 fprintf(stderr
, "usage: %s\n", *usagestr
++);
303 while (*usagestr
&& **usagestr
)
304 fprintf(stderr
, " or: %s\n", *usagestr
++);
306 fprintf(stderr
, " %s\n", *usagestr
++);
308 if (opts
->type
!= OPTION_GROUP
)
311 for (; opts
->type
!= OPTION_END
; opts
++) {
315 if (opts
->type
== OPTION_GROUP
) {
318 fprintf(stderr
, "%s\n", opts
->help
);
321 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
324 pos
= fprintf(stderr
, " ");
325 if (opts
->short_name
)
326 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
327 if (opts
->long_name
&& opts
->short_name
)
328 pos
+= fprintf(stderr
, ", ");
330 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
332 switch (opts
->type
) {
334 if (opts
->flags
& PARSE_OPT_OPTARG
)
335 pos
+= fprintf(stderr
, " [<n>]");
337 pos
+= fprintf(stderr
, " <n>");
339 case OPTION_CALLBACK
:
340 if (opts
->flags
& PARSE_OPT_NOARG
)
345 if (opts
->flags
& PARSE_OPT_OPTARG
)
346 pos
+= fprintf(stderr
, " [<%s>]", opts
->argh
);
348 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
350 if (opts
->flags
& PARSE_OPT_OPTARG
)
351 pos
+= fprintf(stderr
, " [...]");
353 pos
+= fprintf(stderr
, " ...");
356 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
360 if (pos
<= USAGE_OPTS_WIDTH
)
361 pad
= USAGE_OPTS_WIDTH
- pos
;
364 pad
= USAGE_OPTS_WIDTH
;
366 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
373 void usage_with_options(const char * const *usagestr
,
374 const struct option
*opts
)
376 usage_with_options_internal(usagestr
, opts
, 0);
379 /*----- some often used options -----*/
382 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
387 v
= unset
? 0 : DEFAULT_ABBREV
;
389 v
= strtol(arg
, (char **)&arg
, 10);
391 return opterror(opt
, "expects a numerical value", 0);
392 if (v
&& v
< MINIMUM_ABBREV
)
397 *(int *)(opt
->value
) = v
;