summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e0319ff)
raw | patch | inline | side by side (parent: e0319ff)
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | |
Thu, 7 May 2009 19:45:42 +0000 (21:45 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sat, 9 May 2009 07:29:50 +0000 (00:29 -0700) |
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>
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>
parse-options.c | patch | blob | history | |
parse-options.h | patch | blob | history | |
t/t0040-parse-options.sh | patch | blob | history | |
test-parse-options.c | patch | blob | history |
diff --git a/parse-options.c b/parse-options.c
index aaa218d19158af036c5ea96ebbec4513530cb38e..c52b8ccf59a4848be6f3f810e01029f7c6625c78 100644 (file)
--- a/parse-options.c
+++ b/parse-options.c
return -2;
}
+static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
+ const struct option *options)
+{
+ for (; options->type != OPTION_END; options++) {
+ if (!(options->flags & PARSE_OPT_NODASH))
+ continue;
+ if ((options->flags & PARSE_OPT_OPTARG) ||
+ !(options->flags & PARSE_OPT_NOARG))
+ die("BUG: dashless options don't support arguments");
+ if (!(options->flags & PARSE_OPT_NONEG))
+ die("BUG: dashless options don't support negation");
+ if (options->long_name)
+ die("BUG: dashless options can't be long");
+ if (options->short_name == arg[0] && arg[1] == '\0')
+ return get_value(p, options, OPT_SHORT);
+ }
+ return -2;
+}
+
static void check_typos(const char *arg, const struct option *options)
{
if (strlen(arg) < 3)
const char *arg = ctx->argv[0];
if (*arg != '-' || !arg[1]) {
+ if (parse_nodash_opt(ctx, arg, options) == 0)
+ continue;
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
break;
ctx->out[ctx->cpidx++] = ctx->argv[0];
continue;
pos = fprintf(stderr, " ");
- if (opts->short_name)
- pos += fprintf(stderr, "-%c", opts->short_name);
+ if (opts->short_name) {
+ if (opts->flags & PARSE_OPT_NODASH)
+ pos += fprintf(stderr, "%c", opts->short_name);
+ else
+ pos += fprintf(stderr, "-%c", opts->short_name);
+ }
if (opts->long_name && opts->short_name)
pos += fprintf(stderr, ", ");
if (opts->long_name)
diff --git a/parse-options.h b/parse-options.h
index 77919a77fb155f13a791dcd21e95bb6a30199138..919b9b441f97511695308287a2fdc6f3a45e383d 100644 (file)
--- a/parse-options.h
+++ b/parse-options.h
PARSE_OPT_NONEG = 4,
PARSE_OPT_HIDDEN = 8,
PARSE_OPT_LASTARG_DEFAULT = 16,
+ PARSE_OPT_NODASH = 32,
};
struct option;
* PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
* PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
* PARSE_OPT_NONEG: says that this option cannot be negated
- * PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
- * the long one.
+ * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
+ * shown only in the full usage.
+ * PARSE_OPT_LASTARG_DEFAULT: if no argument is given, the default value
+ * is used.
+ * PARSE_OPT_NODASH: this option doesn't start with a dash.
*
* `callback`::
* pointer to the callback to use for OPTION_CALLBACK.
index 8ca62ef453443e7c0f95e0e3cb3bb871b8273ed0..a40c1236c0862da4d77231bf9255b499ccddd99f 100755 (executable)
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
Magic arguments
--quux means --quux
-NUM set integer to NUM
+ + same as -b
Standard options
--abbrev[=<n>] use <n> digits to display SHA-1s
test_cmp expect output
'
+test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
+ test-parse-options + + + + + + > output 2> output.err &&
+ test ! -s output.err &&
+ test_cmp expect output
+'
+
cat > expect <<EOF
boolean: 0
integer: 12345
diff --git a/test-parse-options.c b/test-parse-options.c
index d46eac21b11f8e10f29105dc6bc7262496d33110..e0669dcb41d1d779f274e1ff785352e284953b14 100644 (file)
--- a/test-parse-options.c
+++ b/test-parse-options.c
OPT_ARGUMENT("quux", "means --quux"),
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
number_callback),
+ { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
OPT_GROUP("Standard options"),
OPT__ABBREV(&abbrev),
OPT__VERBOSE(&verbose),