parse-opt: fake short strings for callers to believe in.
If we begin to parse -abc and that the parser knew about -a and -b, it will fake a -c switch for the caller to deal with. Of course in the case of -acb (supposing -c is not taking an argument) the caller will have to be especially clever to do the same thing. We could think about exposing an API to do so if it's really needed, but oh well... Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Jun 23, 2008 at 22:55 UTC
26141b5b60eea36f1d771312f6cae9e56dbbf760
2 files changed
+16
parse-options.c
+11
@@ -1,5 +1,6 @@
1
#include "git-compat-util.h"
2
#include "parse-options.h"
3
+#include "cache.h"
4
5
#define OPT_SHORT 1
6
#define OPT_UNSET 2
@@ -257,6 +258,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
258
const struct option *options,
259
const char * const usagestr[])
260
{
261
+ /* we must reset ->opt, unknown short option leave it dangling */
262
+ ctx->opt = NULL;
263
+
264
for (; ctx->argc; ctx->argc--, ctx->argv++) {
265
const char *arg = ctx->argv[0];
266
@@ -286,6 +290,13 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
290
case -1:
291
return parse_options_usage(usagestr, options);
292
case -2:
293
+ /* fake a short option thing to hide the fact that we may have
294
+ * started to parse aggregated stuff
295
+ *
296
+ * This is leaky, too bad.
297
+ */
298
+ ctx->argv[0] = xstrdup(ctx->opt - 1);
299
+ *(char *)ctx->argv[0] = '-';
300
return PARSE_OPT_UNKNOWN;
301
}
302
}
parse-options.h
+5
@@ -119,6 +119,11 @@ enum {
119
PARSE_OPT_UNKNOWN,
120
};
121
122
+/*
123
+ * It's okay for the caller to consume argv/argc in the usual way.
124
+ * Other fields of that structure are private to parse-options and should not
125
+ * be modified in any way.
126
+ */
127
struct parse_opt_ctx_t {
128
const char **argv;
129
const char **out;