summaryrefslogtreecommitdiff
path: root/parse-options.c
diff options
context:
space:
mode:
authorJonathan Nieder <jrnieder@gmail.com>2010-12-02 06:01:18 (GMT)
committerJunio C Hamano <gitster@pobox.com>2010-12-07 22:15:12 (GMT)
commit1e5ce570ca368b97c8e3b238bb0228c5ca41b494 (patch)
tree8b56df17fd7985bd93d2ff1d5c55b5b7a577962a /parse-options.c
parent9ca1169fd92c71ebbef92ff18aa5d91a2157d1bd (diff)
downloadgit-1e5ce570ca368b97c8e3b238bb0228c5ca41b494.zip
git-1e5ce570ca368b97c8e3b238bb0228c5ca41b494.tar.gz
git-1e5ce570ca368b97c8e3b238bb0228c5ca41b494.tar.bz2
parse-options: clearer reporting of API misuse
The PARSE_OPT_LASTARG_DEFAULT flag is meant for options like --contains that (1) traditionally had a mandatory argument and (2) have some better behavior to use when appearing in the final position. It makes no sense to combine this with OPTARG, so ever since v1.6.4-rc0~71 (parse-options: add parse_options_check to validate option specs, 2009-07-09) this mistake is flagged with error: `--option` uses incompatible flags LASTARG_DEFAULT and OPTARG and an exit status representing an error in commandline usage. Unfortunately that which might confuse scripters calling such an erroneous program into thinking the _script_ contains an error. Clarify that it is an internal error by dying with a message beginning "error: BUG: ..." and status 128. While at it, clean up parse_options_check to prepare for more checks. Long term, it would be nicer to make such checks happen at compile time. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'parse-options.c')
-rw-r--r--parse-options.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/parse-options.c b/parse-options.c
index 9bbbc6a..67d1adc 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -11,6 +11,13 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx,
#define OPT_SHORT 1
#define OPT_UNSET 2
+static int optbug(const struct option *opt, const char *reason)
+{
+ if (opt->long_name)
+ return error("BUG: option '%s' %s", opt->long_name, reason);
+ return error("BUG: switch '%c' %s", opt->short_name, reason);
+}
+
static int opterror(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
@@ -320,20 +327,12 @@ static void parse_options_check(const struct option *opts)
for (; opts->type != OPTION_END; opts++) {
if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
- (opts->flags & PARSE_OPT_OPTARG)) {
- if (opts->long_name) {
- error("`--%s` uses incompatible flags "
- "LASTARG_DEFAULT and OPTARG", opts->long_name);
- } else {
- error("`-%c` uses incompatible flags "
- "LASTARG_DEFAULT and OPTARG", opts->short_name);
- }
- err |= 1;
- }
+ (opts->flags & PARSE_OPT_OPTARG))
+ err |= optbug(opts, "uses incompatible flags "
+ "LASTARG_DEFAULT and OPTARG");
}
-
if (err)
- exit(129);
+ exit(128);
}
void parse_options_start(struct parse_opt_ctx_t *ctx,