Skip to content

Commit

Permalink
parse-opt: fake short strings for callers to believe in.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
MadCoder authored and gitster committed Jun 30, 2008
1 parent 07fe54d commit 26141b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions parse-options.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "git-compat-util.h"
#include "parse-options.h"
#include "cache.h"

#define OPT_SHORT 1
#define OPT_UNSET 2
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -286,6 +290,13 @@ 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
*
* This is leaky, too bad.
*/
ctx->argv[0] = xstrdup(ctx->opt - 1);
*(char *)ctx->argv[0] = '-';
return PARSE_OPT_UNKNOWN;
}
}
Expand Down
5 changes: 5 additions & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 26141b5

Please sign in to comment.