git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: "Pierre Habouzit" <madcoder@debian.org>,
	"Junio C Hamano" <gitster@pobox.com>,
	"René Scharfe" <rene.scharfe@lsrfire.ath.cx>
Subject: [PATCH] parse-options: add parse_options_check to validate option specs.
Date: Tue,  9 Jun 2009 10:23:44 +0200	[thread overview]
Message-ID: <1244535824-11970-1-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <7vd49ewfsi.fsf@alter.siamese.dyndns.org>

It only searches for now for the dreaded LASTARG_DEFAULT | OPTARG
combination, but can be extended to check for any other forbidden
combination.

Options are checked each time we call parse_options_start.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index e469fc0..34282ad 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -306,6 +306,28 @@ static void check_typos(const char *arg, const struct option *options)
 	}
 }
 
+static void parse_options_check(const struct option *opts)
+{
+	int err = 0;
+
+	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;
+		}
+	}
+
+	if (err)
+		exit(129);
+}
+
 void parse_options_start(struct parse_opt_ctx_t *ctx,
 			 int argc, const char **argv, const char *prefix,
 			 int flags)
@@ -331,6 +353,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 {
 	int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
 
+	parse_options_check(options);
+
 	/* we must reset ->opt, unknown short option leave it dangling */
 	ctx->opt = NULL;
 
-- 
1.6.3.2.323.gcd28f

  reply	other threads:[~2009-06-09  9:27 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-05  5:43 parse-options: ambiguous LASTARG_DEFAULT and OPTARG Stephen Boyd
2009-06-06 10:30 ` René Scharfe
2009-06-06 20:14   ` Stephen Boyd
2009-06-07 23:39     ` [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG Stephen Boyd
2009-06-08 17:24       ` René Scharfe
2009-06-08 21:56       ` Junio C Hamano
2009-06-09  8:23         ` Pierre Habouzit [this message]
2009-06-12 19:31           ` [PATCH] parse-options: add parse_options_check to validate option specs Pierre Habouzit
2009-06-12 21:25             ` René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1244535824-11970-1-git-send-email-madcoder@debian.org \
    --to=madcoder@debian.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rene.scharfe@lsrfire.ath.cx \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).