Skip to content

Commit b5ce3a5

Browse files
René Scharfegitster
authored andcommitted
parseopt: add PARSE_OPT_KEEP_UNKNOWN
Add a parseopt flag, PARSE_OPT_KEEP_UNKNOWN, that can be used to keep unknown options in argv, similar to the existing KEEP flags. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 934f788 commit b5ce3a5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

parse-options.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
274274
case -1:
275275
return parse_options_usage(usagestr, options);
276276
case -2:
277-
return PARSE_OPT_UNKNOWN;
277+
goto unknown;
278278
}
279279
if (ctx->opt)
280280
check_typos(arg + 1, options);
@@ -292,7 +292,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
292292
*/
293293
ctx->argv[0] = xstrdup(ctx->opt - 1);
294294
*(char *)ctx->argv[0] = '-';
295-
return PARSE_OPT_UNKNOWN;
295+
goto unknown;
296296
}
297297
}
298298
continue;
@@ -314,8 +314,14 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
314314
case -1:
315315
return parse_options_usage(usagestr, options);
316316
case -2:
317-
return PARSE_OPT_UNKNOWN;
317+
goto unknown;
318318
}
319+
continue;
320+
unknown:
321+
if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
322+
return PARSE_OPT_UNKNOWN;
323+
ctx->out[ctx->cpidx++] = ctx->argv[0];
324+
ctx->opt = NULL;
319325
}
320326
return PARSE_OPT_DONE;
321327
}

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum parse_opt_flags {
2121
PARSE_OPT_KEEP_DASHDASH = 1,
2222
PARSE_OPT_STOP_AT_NON_OPTION = 2,
2323
PARSE_OPT_KEEP_ARGV0 = 4,
24+
PARSE_OPT_KEEP_UNKNOWN = 8,
2425
};
2526

2627
enum parse_opt_option_flags {

0 commit comments

Comments
 (0)