[REPLACEMENT PATCH] parse-opt: fake short strings for callers to believe in.
- Date: Tue, 24 Jun 2008 22:58:01 +0200
- From: Pierre Habouzit <madcoder@xxxxxxxxxx>
- Subject: [REPLACEMENT PATCH] 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@xxxxxxxxxx>
---
On top of my leaky() mini series, one can on purpose leak some memory
here, and avoid the strbuf and limitations at once. It means that in my
current git-blame proof of concept one can drop the strdup(), which
makes it slightly less disgusting.
parse-options.c | 9 +++++++++
parse-options.h | 5 +++++
2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 90935f3..60a11e8 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -1,5 +1,6 @@
#include "git-compat-util.h"
#include "parse-options.h"
+#include "cache.h"
#define OPT_SHORT 1
#define OPT_UNSET 2
@@ -257,6 +258,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options,
const char * const usagestr[])
{
+ /* we must reset ->opt, unknown short option leave it dangling */
+ ctx->opt = NULL;
+
for (; ctx->argc; ctx->argc--, ctx->argv++) {
const char *arg = ctx->argv[0];
@@ -286,6 +290,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
case -1:
return parse_options_usage(usagestr, options);
case -2:
+ /* fake a short option thing to hide the fact that we may have
+ * started to parse aggregated stuff
+ */
+ ctx->argv[0] = leaky(xstrdup(ctx->opt - 1));
+ *(char *)ctx->argv[0] = '-';
return PARSE_OPT_UNKNOWN;
}
}
diff --git a/parse-options.h b/parse-options.h
index 9da5e8c..b391bb6 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -119,6 +119,11 @@ enum {
PARSE_OPT_UNKNOWN,
};
+/*
+ * It's okay for the caller to consume argv/argc in the usual way.
+ * Other fields of that structure are private to parse-options and should not
+ * be modified in any way.
+ */
struct parse_opt_ctx_t {
const char **argv;
const char **out;
--
1.5.6.120.g3adb8.dirty
Attachment:
pgpPWRZoYJ2UM.pgp
Description: PGP signature
- Follow-Ups:
- Re: [REPLACEMENT PATCH] parse-opt: fake short strings for callers to believe in.
- From: Pierre Habouzit
- Re: [REPLACEMENT PATCH] parse-opt: fake short strings for callers to believe in.
- References:
- Convert 'git blame' to parse_options()
- From: Linus Torvalds
- Making parse-opt incremental, reworked series
- From: Pierre Habouzit
- [PATCH 1/7] parse-opt: have parse_options_{start,end}.
- From: Pierre Habouzit
- [PATCH 2/7] parse-opt: Export a non NORETURN usage dumper.
- From: Pierre Habouzit
- [PATCH 3/7] parse-opt: create parse_options_step.
- From: Pierre Habouzit
- [PATCH 4/7] parse-opt: do not pring errors on unknown options, return -2 intead.
- From: Pierre Habouzit
- [PATCH 5/7] parse-opt: fake short strings for callers to believe in.
- From: Pierre Habouzit
- Convert 'git blame' to parse_options()
- Prev by Date: Re: Segmentation fault on http clone, post-1.5.6
- Next by Date: [JGIT PATCH 1/4] LsTree: Do not use the default first empty tree in the walker
- Previous by thread: Re: [PATCH 5/7] parse-opt: fake short strings for callers to believe in.
- Next by thread: Re: [REPLACEMENT PATCH] parse-opt: fake short strings for callers to believe in.
- Index(es):