git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: torvalds@linux-foundation.org, gitster@pobox.com, peff@peff.net,
	Johannes.Schindelin@gmx.de, Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH 1/7] parse-opt: have parse_options_{start,end}.
Date: Tue, 24 Jun 2008 11:12:06 +0200	[thread overview]
Message-ID: <1214298732-6247-2-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <1214298732-6247-1-git-send-email-madcoder@debian.org>

Make the struct optparse_t public under the better name parse_opt_ctx_t.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |   69 +++++++++++++++++++++++++++++++------------------------
 parse-options.h |   16 ++++++++++++
 2 files changed, 55 insertions(+), 30 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index b8bde2b..774e647 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -4,14 +4,7 @@
 #define OPT_SHORT 1
 #define OPT_UNSET 2
 
-struct optparse_t {
-	const char **argv;
-	const char **out;
-	int argc, cpidx;
-	const char *opt;
-};
-
-static inline const char *get_arg(struct optparse_t *p)
+static inline const char *get_arg(struct parse_opt_ctx_t *p)
 {
 	if (p->opt) {
 		const char *res = p->opt;
@@ -37,7 +30,7 @@ static int opterror(const struct option *opt, const char *reason, int flags)
 	return error("option `%s' %s", opt->long_name, reason);
 }
 
-static int get_value(struct optparse_t *p,
+static int get_value(struct parse_opt_ctx_t *p,
                      const struct option *opt, int flags)
 {
 	const char *s, *arg;
@@ -131,7 +124,7 @@ 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 parse_opt_ctx_t *p, const struct option *options)
 {
 	for (; options->type != OPTION_END; options++) {
 		if (options->short_name == *p->opt) {
@@ -142,7 +135,7 @@ static int parse_short_opt(struct optparse_t *p, const struct option *options)
 	return error("unknown switch `%c'", *p->opt);
 }
 
-static int parse_long_opt(struct optparse_t *p, const char *arg,
+static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
                           const struct option *options)
 {
 	const char *arg_end = strchr(arg, '=');
@@ -247,45 +240,63 @@ void check_typos(const char *arg, const struct option *options)
 	}
 }
 
+void parse_options_start(struct parse_opt_ctx_t *ctx,
+                         int argc, const char **argv, int flags)
+{
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->argc = argc - 1;
+	ctx->argv = argv + 1;
+	ctx->out  = argv;
+	ctx->flags = flags;
+}
+
+int parse_options_end(struct parse_opt_ctx_t *ctx)
+{
+	memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
+	ctx->out[ctx->cpidx + ctx->argc] = NULL;
+	return ctx->cpidx + ctx->argc;
+}
+
 static NORETURN void usage_with_options_internal(const char * const *,
                                                  const struct option *, int);
 
 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 };
+	struct parse_opt_ctx_t ctx;
 
-	for (; args.argc; args.argc--, args.argv++) {
-		const char *arg = args.argv[0];
+	parse_options_start(&ctx, argc, argv, flags);
+	for (; ctx.argc; ctx.argc--, ctx.argv++) {
+		const char *arg = ctx.argv[0];
 
 		if (*arg != '-' || !arg[1]) {
-			if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
+			if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
 				break;
-			args.out[args.cpidx++] = args.argv[0];
+			ctx.out[ctx.cpidx++] = ctx.argv[0];
 			continue;
 		}
 
 		if (arg[1] != '-') {
-			args.opt = arg + 1;
-			if (*args.opt == 'h')
+			ctx.opt = arg + 1;
+			if (*ctx.opt == 'h')
 				usage_with_options(usagestr, options);
-			if (parse_short_opt(&args, options) < 0)
+			if (parse_short_opt(&ctx, options) < 0)
 				usage_with_options(usagestr, options);
-			if (args.opt)
+			if (ctx.opt)
 				check_typos(arg + 1, options);
-			while (args.opt) {
-				if (*args.opt == 'h')
+			while (ctx.opt) {
+				if (*ctx.opt == 'h')
 					usage_with_options(usagestr, options);
-				if (parse_short_opt(&args, options) < 0)
+				if (parse_short_opt(&ctx, options) < 0)
 					usage_with_options(usagestr, options);
 			}
 			continue;
 		}
 
 		if (!arg[2]) { /* "--" */
-			if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
-				args.argc--;
-				args.argv++;
+			if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
+				ctx.argc--;
+				ctx.argv++;
 			}
 			break;
 		}
@@ -294,13 +305,11 @@ 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(&ctx, arg + 2, options))
 			usage_with_options(usagestr, options);
 	}
 
-	memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
-	args.out[args.cpidx + args.argc] = NULL;
-	return args.cpidx + args.argc;
+	return parse_options_end(&ctx);
 }
 
 #define USAGE_OPTS_WIDTH 24
diff --git a/parse-options.h b/parse-options.h
index 4ee443d..db6c986 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -111,6 +111,22 @@ extern int parse_options(int argc, const char **argv,
 extern NORETURN void usage_with_options(const char * const *usagestr,
                                         const struct option *options);
 
+/*----- incremantal advanced APIs -----*/
+
+struct parse_opt_ctx_t {
+	const char **argv;
+	const char **out;
+	int argc, cpidx;
+	const char *opt;
+	int flags;
+};
+
+extern void parse_options_start(struct parse_opt_ctx_t *ctx,
+                                int argc, const char **argv, int flags);
+
+extern int parse_options_end(struct parse_opt_ctx_t *ctx);
+
+
 /*----- some often used options -----*/
 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
 extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
-- 
1.5.6.110.g736c7.dirty

  reply	other threads:[~2008-06-24  9:13 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-23  5:15 Convert 'git blame' to parse_options() Linus Torvalds
2008-06-23  6:35 ` Junio C Hamano
2008-06-23 12:28   ` Johannes Schindelin
2008-06-23  8:22 ` [RFC] " Pierre Habouzit
2008-06-23 12:26   ` Johannes Schindelin
2008-06-23 15:53     ` Pierre Habouzit
2008-06-23 16:25       ` Johannes Schindelin
2008-06-23 16:25     ` Linus Torvalds
2008-06-23 16:49       ` Jeff King
2008-06-23 17:06         ` Linus Torvalds
2008-06-23 17:15           ` Jeff King
2008-06-23 17:32             ` Linus Torvalds
2008-06-23 18:15               ` Jeff King
2008-06-23 18:36                 ` Linus Torvalds
2008-06-23 18:20               ` Linus Torvalds
2008-06-23 18:33                 ` Jeff King
2008-06-23 18:47                   ` Linus Torvalds
2008-06-23 19:16                     ` Linus Torvalds
2008-06-23 21:09                       ` Pierre Habouzit
2008-06-23 21:11                         ` [PATCH] parse-opt: have parse_options_{start,end} Pierre Habouzit
2008-06-23 21:11                           ` [PATCH] parse-opt: Export a non NORETURN usage dumper Pierre Habouzit
2008-06-23 21:11                             ` [PATCH] parse-opt: create parse_options_step Pierre Habouzit
2008-06-23 21:11                               ` [PATCH] parse-opt: do not pring errors on unknown options, return -2 intead Pierre Habouzit
2008-06-23 21:11                                 ` [PATCH] parse-opt: fake short strings for callers to believe in Pierre Habouzit
2008-06-23 22:08                                 ` [PATCH] parse-opt: do not pring errors on unknown options, return -2 intead Junio C Hamano
2008-06-23 22:13                                   ` Pierre Habouzit
2008-06-23 21:23                         ` [RFC] Re: Convert 'git blame' to parse_options() Pierre Habouzit
2008-06-23 21:23                         ` Junio C Hamano
2008-06-23 21:28                           ` Pierre Habouzit
2008-06-23 21:26                         ` Linus Torvalds
2008-06-23 21:41                           ` Linus Torvalds
2008-06-23 21:47                           ` Pierre Habouzit
2008-06-23 22:11                           ` Junio C Hamano
2008-06-23 22:24                             ` Pierre Habouzit
2008-06-23 22:36                               ` Pierre Habouzit
2008-06-23 22:38                               ` Junio C Hamano
2008-06-23 23:31                                 ` Pierre Habouzit
2008-06-23 23:40                                   ` Linus Torvalds
2008-06-23 23:51                                   ` Junio C Hamano
2008-06-24  7:50                                     ` Pierre Habouzit
2008-06-24  1:27                                   ` Jeff King
2008-06-23 19:53                     ` Jeff King
2008-06-23 20:04                       ` Pierre Habouzit
2008-06-23 20:12                       ` Linus Torvalds
2008-06-24  5:35                         ` Jeff King
2008-06-24 16:59                           ` Linus Torvalds
2008-06-24 17:13                             ` Johannes Schindelin
2008-06-24 17:34                             ` Jeff King
2008-06-24 17:44                               ` Linus Torvalds
2008-06-24 19:46                                 ` Jeff King
2008-06-24  0:30                 ` Junio C Hamano
2008-06-24  8:24                   ` Pierre Habouzit
2008-06-24 17:05                     ` Linus Torvalds
2008-06-24 19:30                       ` Pierre Habouzit
2008-06-24 19:43                         ` Pierre Habouzit
2008-06-25  6:09                         ` Johannes Sixt
2008-06-23 17:04       ` Johannes Schindelin
2008-06-23 17:21         ` Linus Torvalds
2008-06-23 18:39           ` Johannes Schindelin
2008-06-23 17:26         ` Jeff King
2008-06-23 18:41           ` Johannes Schindelin
2008-06-23 19:24       ` Pierre Habouzit
2008-06-23 16:11   ` Linus Torvalds
2008-06-24  9:12 ` Making parse-opt incremental, reworked series Pierre Habouzit
2008-06-24  9:12   ` Pierre Habouzit [this message]
2008-06-24  9:12     ` [PATCH 2/7] parse-opt: Export a non NORETURN usage dumper Pierre Habouzit
2008-06-24  9:12       ` [PATCH 3/7] parse-opt: create parse_options_step Pierre Habouzit
2008-06-24  9:12         ` [PATCH 4/7] parse-opt: do not pring errors on unknown options, return -2 intead Pierre Habouzit
2008-06-24  9:12           ` [PATCH 5/7] parse-opt: fake short strings for callers to believe in Pierre Habouzit
2008-06-24  9:12             ` [PATCH 6/7] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option Pierre Habouzit
2008-06-24  9:12               ` [PATCH 7/7] Migrate git-blame to parse-option partially Pierre Habouzit
2008-06-24 10:03               ` [PATCH 6/7] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option Pierre Habouzit
2008-06-24 17:18               ` Linus Torvalds
2008-06-24 19:27                 ` Pierre Habouzit
2008-06-24 20:55                 ` Pierre Habouzit
2008-06-24 17:20             ` [PATCH 5/7] parse-opt: fake short strings for callers to believe in Linus Torvalds
2008-06-24 19:26               ` Pierre Habouzit
2008-06-25 15:07                 ` Andreas Ericsson
2008-06-24 20:58             ` [REPLACEMENT PATCH] " Pierre Habouzit
2008-06-26  8:35               ` Pierre Habouzit
2008-06-26  8:40                 ` Junio C Hamano
2008-06-26  9:37                   ` Pierre Habouzit

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=1214298732-6247-2-git-send-email-madcoder@debian.org \
    --to=madcoder@debian.org \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=torvalds@linux-foundation.org \
    /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).