@matheusbsbs / matheus-bagliano / commits / 51a9949eda
Fork 0

parseopt: add PARSE_OPT_NODASH

Add support for options that don't start with a dash. Initially, they don't accept arguments and can only be short options, i.e. consist of a single character. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 7, 2009 at 21:45 UTC 51a9949eda7421a2dd9cb45b2110d6571ba09bbd
4 files changed +42 -4
parse-options.c
+27 -2
@@ -245,6 +245,25 @@ is_abbreviated:
245 return -2;
246 }
247
248 +static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
249 + const struct option *options)
250 +{
251 + for (; options->type != OPTION_END; options++) {
252 + if (!(options->flags & PARSE_OPT_NODASH))
253 + continue;
254 + if ((options->flags & PARSE_OPT_OPTARG) ||
255 + !(options->flags & PARSE_OPT_NOARG))
256 + die("BUG: dashless options don't support arguments");
257 + if (!(options->flags & PARSE_OPT_NONEG))
258 + die("BUG: dashless options don't support negation");
259 + if (options->long_name)
260 + die("BUG: dashless options can't be long");
261 + if (options->short_name == arg[0] && arg[1] == '\0')
262 + return get_value(p, options, OPT_SHORT);
263 + }
264 + return -2;
265 +}
266 +
267 static void check_typos(const char *arg, const struct option *options)
268 {
269 if (strlen(arg) < 3)
@@ -295,6 +314,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
314 const char *arg = ctx->argv[0];
315
316 if (*arg != '-' || !arg[1]) {
317 + if (parse_nodash_opt(ctx, arg, options) == 0)
318 + continue;
319 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
320 break;
321 ctx->out[ctx->cpidx++] = ctx->argv[0];
@@ -427,8 +448,12 @@ int usage_with_options_internal(const char * const *usagestr,
448 continue;
449
450 pos = fprintf(stderr, " ");
430 - if (opts->short_name)
431 - pos += fprintf(stderr, "-%c", opts->short_name);
451 + if (opts->short_name) {
452 + if (opts->flags & PARSE_OPT_NODASH)
453 + pos += fprintf(stderr, "%c", opts->short_name);
454 + else
455 + pos += fprintf(stderr, "-%c", opts->short_name);
456 + }
457 if (opts->long_name && opts->short_name)
458 pos += fprintf(stderr, ", ");
459 if (opts->long_name)
parse-options.h
+6 -2
@@ -33,6 +33,7 @@ enum parse_opt_option_flags {
33 PARSE_OPT_NONEG = 4,
34 PARSE_OPT_HIDDEN = 8,
35 PARSE_OPT_LASTARG_DEFAULT = 16,
36 + PARSE_OPT_NODASH = 32,
37 };
38
39 struct option;
@@ -66,8 +67,11 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
67 * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
68 * PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
69 * PARSE_OPT_NONEG: says that this option cannot be negated
69 - * PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
70 - * the long one.
70 + * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
71 + * shown only in the full usage.
72 + * PARSE_OPT_LASTARG_DEFAULT: if no argument is given, the default value
73 + * is used.
74 + * PARSE_OPT_NODASH: this option doesn't start with a dash.
75 *
76 * `callback`::
77 * pointer to the callback to use for OPTION_CALLBACK.
t/t0040-parse-options.sh
+7
@@ -31,6 +31,7 @@ String options
31 Magic arguments
32 --quux means --quux
33 -NUM set integer to NUM
34 + + same as -b
35
36 Standard options
37 --abbrev[=<n>] use <n> digits to display SHA-1s
@@ -276,6 +277,12 @@ test_expect_success 'OPT_NEGBIT() works' '
277 test_cmp expect output
278 '
279
280 +test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
281 + test-parse-options + + + + + + > output 2> output.err &&
282 + test ! -s output.err &&
283 + test_cmp expect output
284 +'
285 +
286 cat > expect <<EOF
287 boolean: 0
288 integer: 12345
test-parse-options.c
+2
@@ -54,6 +54,8 @@ int main(int argc, const char **argv)
54 OPT_ARGUMENT("quux", "means --quux"),
55 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
56 number_callback),
57 + { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
58 + PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
59 OPT_GROUP("Standard options"),
60 OPT__ABBREV(&abbrev),
61 OPT__VERBOSE(&verbose),