summaryrefslogtreecommitdiff
path: root/parse-options.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2009-05-25 07:59:07 (GMT)
committerJunio C Hamano <gitster@pobox.com>2009-05-25 07:59:07 (GMT)
commitee969693c5e77e1b5ebda45bead569806d31b379 (patch)
tree25d803cd17cbe891a9937f3e3442a1d7863ae773 /parse-options.c
parent4c8d4c14c6de59bc14d6118724ffee949e8654a7 (diff)
parent9619ff14159ab3401636b9883a715b0f20b051df (diff)
downloadgit-ee969693c5e77e1b5ebda45bead569806d31b379.zip
git-ee969693c5e77e1b5ebda45bead569806d31b379.tar.gz
git-ee969693c5e77e1b5ebda45bead569806d31b379.tar.bz2
Merge branch 'master' into sb/opt-filename
* master: (654 commits) http-push.c::remove_locks(): fix use after free t/t3400-rebase.sh: add more tests to help migrating git-rebase.sh to C post-receive-email: hooks.showrev: show how to include both web link and patch MinGW: Fix compiler warning in merge-recursive MinGW: Add a simple getpass() MinGW: use POSIX signature of waitpid() MinGW: the path separator to split GITPERLLIB is ';' on Win32 MinGW: Scan for \r in addition to \n when reading shbang lines gitweb: Sanitize title attribute in format_subject_html Terminate argv with NULL before calling setup_revisions() doc/git-rebase.txt: remove mention of multiple strategies git-send-email: Handle quotes when parsing .mailrc files git-svn: add --authors-prog option git-svn: Set svn.authorsfile if it is passed to git svn clone git-svn: Correctly report max revision when following deleted paths git-svn: Fix for svn paths removed > log-window-size revisions ago git-svn testsuite: use standard configuration for Subversion tools grep: fix word-regexp colouring completion: use git rev-parse to detect bare repos Cope better with a _lot_ of packs ...
Diffstat (limited to 'parse-options.c')
-rw-r--r--parse-options.c91
1 files changed, 81 insertions, 10 deletions
diff --git a/parse-options.c b/parse-options.c
index 4c5d09d..c52b8cc 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -50,6 +50,7 @@ static int get_value(struct parse_opt_ctx_t *p,
/* FALLTHROUGH */
case OPTION_BOOLEAN:
case OPTION_BIT:
+ case OPTION_NEGBIT:
case OPTION_SET_INT:
case OPTION_SET_PTR:
return opterror(opt, "takes no value", flags);
@@ -66,6 +67,13 @@ static int get_value(struct parse_opt_ctx_t *p,
*(int *)opt->value |= opt->defval;
return 0;
+ case OPTION_NEGBIT:
+ if (unset)
+ *(int *)opt->value |= opt->defval;
+ else
+ *(int *)opt->value &= ~opt->defval;
+ return 0;
+
case OPTION_BOOLEAN:
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
return 0;
@@ -121,11 +129,33 @@ static int get_value(struct parse_opt_ctx_t *p,
static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
{
+ const struct option *numopt = NULL;
+
for (; options->type != OPTION_END; options++) {
if (options->short_name == *p->opt) {
p->opt = p->opt[1] ? p->opt + 1 : NULL;
return get_value(p, options, OPT_SHORT);
}
+
+ /*
+ * Handle the numerical option later, explicit one-digit
+ * options take precedence over it.
+ */
+ if (options->type == OPTION_NUMBER)
+ numopt = options;
+ }
+ if (numopt && isdigit(*p->opt)) {
+ size_t len = 1;
+ char *arg;
+ int rc;
+
+ while (isdigit(p->opt[len]))
+ len++;
+ arg = xmemdupz(p->opt, len);
+ p->opt = p->opt[len] ? p->opt + len : NULL;
+ rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
+ free(arg);
+ return rc;
}
return -2;
}
@@ -215,6 +245,25 @@ is_abbreviated:
return -2;
}
+static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
+ const struct option *options)
+{
+ for (; options->type != OPTION_END; options++) {
+ if (!(options->flags & PARSE_OPT_NODASH))
+ continue;
+ if ((options->flags & PARSE_OPT_OPTARG) ||
+ !(options->flags & PARSE_OPT_NOARG))
+ die("BUG: dashless options don't support arguments");
+ if (!(options->flags & PARSE_OPT_NONEG))
+ die("BUG: dashless options don't support negation");
+ if (options->long_name)
+ die("BUG: dashless options can't be long");
+ if (options->short_name == arg[0] && arg[1] == '\0')
+ return get_value(p, options, OPT_SHORT);
+ }
+ return -2;
+}
+
static void check_typos(const char *arg, const struct option *options)
{
if (strlen(arg) < 3)
@@ -244,6 +293,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
ctx->out = argv;
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
ctx->flags = flags;
+ if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
+ (flags & PARSE_OPT_STOP_AT_NON_OPTION))
+ die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
}
static int usage_with_options_internal(const char * const *,
@@ -253,6 +305,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options,
const char * const usagestr[])
{
+ int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
+
/* we must reset ->opt, unknown short option leave it dangling */
ctx->opt = NULL;
@@ -260,6 +314,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
const char *arg = ctx->argv[0];
if (*arg != '-' || !arg[1]) {
+ if (parse_nodash_opt(ctx, arg, options) == 0)
+ continue;
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
break;
ctx->out[ctx->cpidx++] = ctx->argv[0];
@@ -268,18 +324,18 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
if (arg[1] != '-') {
ctx->opt = arg + 1;
- if (*ctx->opt == 'h')
+ if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
switch (parse_short_opt(ctx, options)) {
case -1:
return parse_options_usage(usagestr, options);
case -2:
- return PARSE_OPT_UNKNOWN;
+ goto unknown;
}
if (ctx->opt)
check_typos(arg + 1, options);
while (ctx->opt) {
- if (*ctx->opt == 'h')
+ if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
switch (parse_short_opt(ctx, options)) {
case -1:
@@ -292,7 +348,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
*/
ctx->argv[0] = xstrdup(ctx->opt - 1);
*(char *)ctx->argv[0] = '-';
- return PARSE_OPT_UNKNOWN;
+ goto unknown;
}
}
continue;
@@ -306,16 +362,22 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
break;
}
- if (!strcmp(arg + 2, "help-all"))
+ if (internal_help && !strcmp(arg + 2, "help-all"))
return usage_with_options_internal(usagestr, options, 1);
- if (!strcmp(arg + 2, "help"))
+ if (internal_help && !strcmp(arg + 2, "help"))
return parse_options_usage(usagestr, options);
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
return parse_options_usage(usagestr, options);
case -2:
- return PARSE_OPT_UNKNOWN;
+ goto unknown;
}
+ continue;
+unknown:
+ if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
+ return PARSE_OPT_UNKNOWN;
+ ctx->out[ctx->cpidx++] = ctx->argv[0];
+ ctx->opt = NULL;
}
return PARSE_OPT_DONE;
}
@@ -356,6 +418,9 @@ int parse_options(int argc, const char **argv, const struct option *options,
int usage_with_options_internal(const char * const *usagestr,
const struct option *opts, int full)
{
+ if (!usagestr)
+ return PARSE_OPT_HELP;
+
fprintf(stderr, "usage: %s\n", *usagestr++);
while (*usagestr && **usagestr)
fprintf(stderr, " or: %s\n", *usagestr++);
@@ -383,12 +448,18 @@ int usage_with_options_internal(const char * const *usagestr,
continue;
pos = fprintf(stderr, " ");
- if (opts->short_name)
- pos += fprintf(stderr, "-%c", opts->short_name);
+ if (opts->short_name) {
+ if (opts->flags & PARSE_OPT_NODASH)
+ pos += fprintf(stderr, "%c", opts->short_name);
+ else
+ pos += fprintf(stderr, "-%c", opts->short_name);
+ }
if (opts->long_name && opts->short_name)
pos += fprintf(stderr, ", ");
if (opts->long_name)
pos += fprintf(stderr, "--%s", opts->long_name);
+ if (opts->type == OPTION_NUMBER)
+ pos += fprintf(stderr, "-NUM");
switch (opts->type) {
case OPTION_ARGUMENT:
@@ -425,7 +496,7 @@ int usage_with_options_internal(const char * const *usagestr,
pos += fprintf(stderr, " ...");
}
break;
- default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
+ default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
break;
}