Skip to content

Commit 51a9949

Browse files
René Scharfegitster
authored andcommitted
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>
1 parent e0319ff commit 51a9949

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

parse-options.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
245245
return -2;
246246
}
247247

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+
248267
static void check_typos(const char *arg, const struct option *options)
249268
{
250269
if (strlen(arg) < 3)
@@ -295,6 +314,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
295314
const char *arg = ctx->argv[0];
296315

297316
if (*arg != '-' || !arg[1]) {
317+
if (parse_nodash_opt(ctx, arg, options) == 0)
318+
continue;
298319
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
299320
break;
300321
ctx->out[ctx->cpidx++] = ctx->argv[0];
@@ -427,8 +448,12 @@ int usage_with_options_internal(const char * const *usagestr,
427448
continue;
428449

429450
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+
}
432457
if (opts->long_name && opts->short_name)
433458
pos += fprintf(stderr, ", ");
434459
if (opts->long_name)

parse-options.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum parse_opt_option_flags {
3333
PARSE_OPT_NONEG = 4,
3434
PARSE_OPT_HIDDEN = 8,
3535
PARSE_OPT_LASTARG_DEFAULT = 16,
36+
PARSE_OPT_NODASH = 32,
3637
};
3738

3839
struct option;
@@ -66,8 +67,11 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
6667
* PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
6768
* PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
6869
* 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.
7175
*
7276
* `callback`::
7377
* pointer to the callback to use for OPTION_CALLBACK.

t/t0040-parse-options.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ String options
3131
Magic arguments
3232
--quux means --quux
3333
-NUM set integer to NUM
34+
+ same as -b
3435
3536
Standard options
3637
--abbrev[=<n>] use <n> digits to display SHA-1s
@@ -276,6 +277,12 @@ test_expect_success 'OPT_NEGBIT() works' '
276277
test_cmp expect output
277278
'
278279

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+
279286
cat > expect <<EOF
280287
boolean: 0
281288
integer: 12345

test-parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ int main(int argc, const char **argv)
5454
OPT_ARGUMENT("quux", "means --quux"),
5555
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
5656
number_callback),
57+
{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
58+
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
5759
OPT_GROUP("Standard options"),
5860
OPT__ABBREV(&abbrev),
5961
OPT__VERBOSE(&verbose),

0 commit comments

Comments
 (0)