git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Riesen <raa.lkml@gmail.com>
To: Pierre Habouzit <madcoder@debian.org>,
	git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] Add a simple option parser.
Date: Sun, 14 Oct 2007 00:14:50 +0200	[thread overview]
Message-ID: <20071013221450.GC2875@steel.home> (raw)
In-Reply-To: <20071013205404.GK7110@artemis.corp>

Pierre Habouzit, Sat, Oct 13, 2007 22:54:04 +0200:
> On Sat, Oct 13, 2007 at 07:16:55PM +0000, Alex Riesen wrote:
> > Pierre Habouzit, Sat, Oct 13, 2007 15:29:03 +0200:
> > BTW, if you just printed the usage message out (it is about usage of a
> > program, isn't it?) and called exit() everyone would be just as happy.
> > And you wouldn't have to include strbuf (it is the only use of it),
> > less code, too. It'd make simplier to stea^Wcopy your implementation,
> > which I like :)
> 
>   the reason is that usage() is a wrapper around a callback, and I
> suppose it's used by some GUI's or anything like that.

It is not. Not yet. What could they use a usage text for?
Besides, you could just export the callback (call_usage_callback or
something) from usage.c and call it.

>   FWIW you can rework the .c like this:

on top of yours:

From: Alex Riesen <raa.lkml@gmail.com>
Date: Sun, 14 Oct 2007 00:10:51 +0200
Subject: [PATCH] Rework make_usage to print the usage message immediately

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 parse-options.c |   60 ++++++++++++++++++++++++------------------------------
 1 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index 07abb50..1e3940f 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "parse-options.h"
-#include "strbuf.h"
 
 #define OPT_SHORT 1
 #define OPT_UNSET 2
@@ -171,57 +170,52 @@ int parse_options(int argc, const char **argv,
 
 void make_usage(const char * const usagestr[], struct option *opts, int cnt)
 {
-	struct strbuf sb;
-
-	strbuf_init(&sb, 4096);
-	do {
-		strbuf_addstr(&sb, *usagestr++);
-		strbuf_addch(&sb, '\n');
-	} while (*usagestr);
+	fprintf(stderr, "usage: ");
+	while (*usagestr)
+		fprintf(stderr, "%s\n", *usagestr++);
 
 	if (cnt && opts->type != OPTION_GROUP)
-		strbuf_addch(&sb, '\n');
+		fputc('\n', stderr);
 
 	for (; cnt-- > 0; opts++) {
 		size_t pos;
 
 		if (opts->type == OPTION_GROUP) {
-			strbuf_addch(&sb, '\n');
+			fputc('\n', stderr);
 			if (*opts->help)
-				strbuf_addf(&sb, "%s\n", opts->help);
+				fprintf(stderr, "%s\n", opts->help);
 			continue;
 		}
 
-		pos = sb.len;
-		strbuf_addstr(&sb, "    ");
-		if (opts->short_name) {
-			strbuf_addf(&sb, "-%c", opts->short_name);
-		}
-		if (opts->long_name) {
-			strbuf_addf(&sb, opts->short_name ? ", --%s" : "--%s",
-						opts->long_name);
-		}
+		pos = fprintf(stderr, "    ");
+		if (opts->short_name)
+			pos += fprintf(stderr, "-%c", opts->short_name);
+		if (opts->long_name)
+			pos += fprintf(stderr,
+				       opts->short_name ? ", --%s" : "--%s",
+				       opts->long_name);
 		switch (opts->type) {
 		case OPTION_INTEGER:
-			strbuf_addstr(&sb, " <n>");
+			fputs(" <n>", stderr);
+			pos += 4;
 			break;
 		case OPTION_STRING:
-			if (opts->argh) {
-				strbuf_addf(&sb, " <%s>", opts->argh);
-			} else {
-				strbuf_addstr(&sb, " ...");
+			if (opts->argh)
+				pos += fprintf(stderr, " <%s>", opts->argh);
+			else {
+				fputs(" ...", stderr);
+				pos += 4;
 			}
 			break;
 		default:
 			break;
 		}
-		if (sb.len - pos <= USAGE_OPTS_WIDTH) {
-			int pad = USAGE_OPTS_WIDTH - (sb.len - pos) + USAGE_GAP;
-			strbuf_addf(&sb, "%*s%s\n", pad, "", opts->help);
-		} else {
-			strbuf_addf(&sb, "\n%*s%s\n", USAGE_OPTS_WIDTH + USAGE_GAP, "",
-						opts->help);
-		}
+		if (pos <= USAGE_OPTS_WIDTH) {
+			int pad = USAGE_OPTS_WIDTH - pos + USAGE_GAP;
+			fprintf(stderr, "%*s%s\n", pad, "", opts->help);
+		} else
+			fprintf(stderr, "\n%*s%s\n",
+				USAGE_OPTS_WIDTH + USAGE_GAP, "", opts->help);
 	}
-	usage(sb.buf);
+	exit(129);
 }
-- 
1.5.3.4.232.ga843

  reply	other threads:[~2007-10-13 22:15 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-13 13:29 [RFC] CLI option parsing and usage generation for porcelains Pierre Habouzit
     [not found] ` <1192282153-26684-2-git-send-email-madcoder@debian.org>
2007-10-13 14:39   ` [PATCH] Add a simple option parser Johannes Schindelin
2007-10-13 14:58     ` Pierre Habouzit
     [not found]   ` <1192282153-26684-3-git-send-email-madcoder@debian.org>
2007-10-13 14:47     ` [PATCH] Port builtin-add.c to use the new " Johannes Schindelin
2007-10-13 15:03       ` Pierre Habouzit
2007-10-13 19:22         ` Alex Riesen
2007-10-13 20:27           ` Pierre Habouzit
     [not found]     ` <1192282153-26684-4-git-send-email-madcoder@debian.org>
     [not found]       ` <1192282153-26684-5-git-send-email-madcoder@debian.org>
     [not found]         ` <1192282153-26684-6-git-send-email-madcoder@debian.org>
     [not found]           ` <1192282153-26684-7-git-send-email-madcoder@debian.org>
     [not found]             ` <1192282153-26684-8-git-send-email-madcoder@debian.org>
     [not found]               ` <1192282153-26684-9-git-send-email-madcoder@debian.org>
     [not found]                 ` <1192282153-26684-10-git-send-email-madcoder@debian.org>
2007-10-14 14:01                   ` [PATCH] Simplify usage string printing Jonas Fonseca
2007-10-14 16:26                     ` Pierre Habouzit
2007-10-13 19:16   ` [PATCH] Add a simple option parser Alex Riesen
2007-10-13 20:54     ` Pierre Habouzit
2007-10-13 22:14       ` Alex Riesen [this message]
2007-10-14  7:02         ` Pierre Habouzit
2007-10-14 14:10   ` [PATCH] Update manpages to reflect new short and long option aliases Jonas Fonseca
2007-10-14 16:26     ` Pierre Habouzit
2007-10-13 14:53 ` [RFC] CLI option parsing and usage generation for porcelains Wincent Colaiuta
2007-10-14  9:18 ` Eric Wong
2007-10-14  9:57   ` Pierre Habouzit
2007-10-14 16:54     ` [PATCH] parse-options: Allow abbreviated options when unambiguous Johannes Schindelin
2007-10-14 18:02       ` Johannes Schindelin
2007-10-14 18:08         ` Pierre Habouzit
2007-10-14 21:01           ` Eric Wong
2007-10-14 22:12             ` Johannes Schindelin
2007-10-14 22:49               ` Eric Wong
2007-10-14 22:59                 ` git-svn and submodules, was " Johannes Schindelin
2007-10-15  7:07                   ` git-svn and submodules Benoit SIGOURE
2007-10-15 10:00                     ` Andreas Ericsson
2007-10-15 10:51                       ` Benoit SIGOURE
2007-10-15 10:14                     ` David Kastrup
2007-10-15 10:53                       ` Benoit SIGOURE
2007-10-15 16:27                         ` Andreas Ericsson
2007-10-15 14:45                     ` Karl Hasselström
2007-10-15 15:14                       ` .gitignore and svn:ignore [WAS: git-svn and submodules] Chris Shoemaker
2007-10-16  7:58                         ` Eric Wong
2007-10-16  9:43                           ` Karl Hasselström
2007-10-16 13:05                           ` Chris Shoemaker
2007-10-15 15:53                     ` git-svn and submodules Linus Torvalds
2007-10-15 16:17                       ` Performance issue with excludes (was: Re: git-svn and submodules) Benoit SIGOURE
2007-10-15 16:34                         ` Linus Torvalds
2007-10-15 16:51                           ` Benoit SIGOURE
2007-10-15 17:10                             ` Linus Torvalds
2007-10-15 17:38                               ` Benoit SIGOURE
  -- strict thread matches above, loose matches on Subject: below --
2007-10-03 21:45 [PATCH] Add a simple option parser Kristian Høgsberg
2007-10-03 23:11 ` Pierre Habouzit
2007-10-04 14:57   ` Kristian Høgsberg
2007-10-04 15:15     ` Pierre Habouzit
2007-10-04 16:31       ` Pierre Habouzit
2007-10-04 16:39         ` Johannes Schindelin
2007-10-05 10:08 ` Pierre Habouzit
2007-10-05 14:21 ` 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=20071013221450.GC2875@steel.home \
    --to=raa.lkml@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=madcoder@debian.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).