Subject: Re: [PATCH 1/7] branch -h: show usage even in an invalid repository

From: Jonathan Nieder <[email-masked]>
Date: Sun, 24 Oct 2010 02:20:32 -0500

Junio C Hamano wrote:

>  - Some tests redirect both the standard output and the standard error
>    (like this patch) and check the combined result, while some others
>    (e.g. 2/7) check only the standard error stream.  Don't we want to be
>    testing them more uniformly?

Good point.  The current state is:

 - Messages from a mistake in usage are reported to stderr.
 - Messages from git <command> -h are reported to stdout or
   stderr, with a slight preference for stdout.

A nicer behavior would be

 - Messages from a mistake in usage are reported to stderr.
 - Messages from git <command> -h are reported to stdout.

Here's a helper to make it easier for commands that use parse-options
to adopt that nicer behavior.  It writes its output to stdout, so it
should only be used to be used to handle the -h option.  Example:

	if (argc == 2 && !strcmp(argv[1], "-h"))
		usage_for_help_opt(builtin_foo_usage, options);

	... some scary commands that should not run with -h ...

	argc = parse_options(argc, ...
	if (argc != 1)
		usage_with_options(builtin_foo_usage, options);

Signed-off-by: Jonathan Nieder <[email-masked]>
---

diff --git a/parse-options.c b/parse-options.c
index 0fa79bc..e92fcfe 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -567,6 +567,13 @@ void usage_with_options(const char * const *usagestr,
 	exit(129);
 }
 
+void usage_for_help_opt(const char * const *usagestr,
+			const struct option *opts)
+{
+	usage_with_options_internal(NULL, usagestr, opts, 0, 0);
+	exit(129);
+}
+
 void usage_msg_opt(const char *msg,
 		   const char * const *usagestr,
 		   const struct option *options)
diff --git a/parse-options.h b/parse-options.h
index d982f0f..de69826 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -152,6 +152,9 @@ extern int parse_options(int argc, const char **argv, const char *prefix,
 extern NORETURN void usage_with_options(const char * const *usagestr,
                                         const struct option *options);
 
+extern NORETURN void usage_for_help_opt(const char * const *usagestr,
+                                        const struct option *options);
+
 extern NORETURN void usage_msg_opt(const char *msg,
 				   const char * const *usagestr,
 				   const struct option *options);