git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shawn Bohrer <shawn.bohrer@gmail.com>
To: git@vger.kernel.org
Cc: peff@peff.net, gitster@pobox.com, madcoder@debian.org,
	Shawn Bohrer <shawn.bohrer@gmail.com>
Subject: [PATCH 1/2] parse_options: Add flag to prevent errors for further processing
Date: Tue, 17 Jun 2008 22:03:55 -0500	[thread overview]
Message-ID: <1213758236-979-2-git-send-email-shawn.bohrer@gmail.com> (raw)
In-Reply-To: <1213758236-979-1-git-send-email-shawn.bohrer@gmail.com>

This adds the PARSE_OPT_NO_ERROR_ON_UNKNOWN flag which prevents
parse_options() from erroring out when it finds an unknown option,
and leaves the original command and unknown options in argv.

This option is useful if the option parsing needs to be done in
multiple stages for example if the remaining options will be passed
to additional git commands.

Signed-off-by: Shawn Bohrer <shawn.bohrer@gmail.com>
---
 parse-options.c |   25 ++++++++++++++++++++-----
 parse-options.h |    5 +++--
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index 8071711..2635e18 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -131,7 +131,8 @@ static int get_value(struct optparse_t *p,
 	}
 }
 
-static int parse_short_opt(struct optparse_t *p, const struct option *options)
+static int parse_short_opt(struct optparse_t *p, const struct option *options,
+			   int flags)
 {
 	for (; options->type != OPTION_END; options++) {
 		if (options->short_name == *p->opt) {
@@ -139,11 +140,16 @@ static int parse_short_opt(struct optparse_t *p, const struct option *options)
 			return get_value(p, options, OPT_SHORT);
 		}
 	}
+
+	if (flags & PARSE_OPT_NO_ERROR_ON_UNKNOWN) {
+		p->out[p->cpidx++] = p->argv[0];
+		return 0;
+	}
 	return error("unknown switch `%c'", *p->opt);
 }
 
 static int parse_long_opt(struct optparse_t *p, const char *arg,
-                          const struct option *options)
+                          const struct option *options, int flags)
 {
 	const char *arg_end = strchr(arg, '=');
 	const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
@@ -224,6 +230,11 @@ is_abbreviated:
 			abbrev_option->long_name);
 	if (abbrev_option)
 		return get_value(p, abbrev_option, abbrev_flags);
+
+	if (flags & PARSE_OPT_NO_ERROR_ON_UNKNOWN) {
+		p->out[p->cpidx++] = p->argv[0];
+		return 0;
+	}
 	return error("unknown option `%s'", arg);
 }
 
@@ -254,6 +265,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
                   const char * const usagestr[], int flags)
 {
 	struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
+	if (flags & PARSE_OPT_NO_ERROR_ON_UNKNOWN)
+		args.out =  argv + 1;
 
 	for (; args.argc; args.argc--, args.argv++) {
 		const char *arg = args.argv[0];
@@ -269,14 +282,14 @@ int parse_options(int argc, const char **argv, const struct option *options,
 			args.opt = arg + 1;
 			if (*args.opt == 'h')
 				usage_with_options(usagestr, options);
-			if (parse_short_opt(&args, options) < 0)
+			if (parse_short_opt(&args, options, flags) < 0)
 				usage_with_options(usagestr, options);
 			if (args.opt)
 				check_typos(arg + 1, options);
 			while (args.opt) {
 				if (*args.opt == 'h')
 					usage_with_options(usagestr, options);
-				if (parse_short_opt(&args, options) < 0)
+				if (parse_short_opt(&args, options, flags) < 0)
 					usage_with_options(usagestr, options);
 			}
 			continue;
@@ -294,11 +307,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
 			usage_with_options_internal(usagestr, options, 1);
 		if (!strcmp(arg + 2, "help"))
 			usage_with_options(usagestr, options);
-		if (parse_long_opt(&args, arg + 2, options))
+		if (parse_long_opt(&args, arg + 2, options, flags))
 			usage_with_options(usagestr, options);
 	}
 
 	memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
+	if (flags & PARSE_OPT_NO_ERROR_ON_UNKNOWN)
+		++args.cpidx;
 	args.out[args.cpidx + args.argc] = NULL;
 	return args.cpidx + args.argc;
 }
diff --git a/parse-options.h b/parse-options.h
index 4ee443d..416ccdd 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -18,8 +18,9 @@ enum parse_opt_type {
 };
 
 enum parse_opt_flags {
-	PARSE_OPT_KEEP_DASHDASH = 1,
-	PARSE_OPT_STOP_AT_NON_OPTION = 2,
+	PARSE_OPT_KEEP_DASHDASH       = 1,
+	PARSE_OPT_STOP_AT_NON_OPTION  = 2,
+	PARSE_OPT_NO_ERROR_ON_UNKNOWN = 4
 };
 
 enum parse_opt_option_flags {
-- 
1.5.4.3

  reply	other threads:[~2008-06-18  3:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-18  3:03 [RFC] convert shortlog to use parse_options Shawn Bohrer
2008-06-18  3:03 ` Shawn Bohrer [this message]
2008-06-18  3:03   ` [PATCH 2/2] git shortlog: Modify " Shawn Bohrer
2008-06-18  3:21   ` [PATCH 1/2] parse_options: Add flag to prevent errors for further processing Junio C Hamano
2008-06-18  3:30     ` Jeff King
2008-06-18  3:34       ` Jeff King
2008-06-18  5:13       ` Junio C Hamano
2008-06-18 16:50         ` Johannes Schindelin
2008-06-18 18:52           ` Junio C Hamano
2008-06-19 14:25             ` Shawn Bohrer
2008-06-22 19:07               ` Johannes Schindelin
2008-06-23 19:55                 ` Junio C Hamano
2008-06-23 19:59                   ` Jeff King
2008-06-23 20:17                     ` Junio C Hamano
2008-06-23 20:24                   ` Johannes Schindelin
2008-06-22 17:07         ` Pierre Habouzit
2008-06-23  1:45           ` Junio C Hamano

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=1213758236-979-2-git-send-email-shawn.bohrer@gmail.com \
    --to=shawn.bohrer@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=madcoder@debian.org \
    --cc=peff@peff.net \
    /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).