Re: [PATCH 1/7] branch -h: show usage even in an invalid repository
- From
Jonathan Nieder <jrnieder@gmail.com>
- Date
- Oct 24, 2010, 07:20 UTC
- Message-ID
- <20101024072032.GA23455@burratino>
- In-Reply-To
- <7v8w1qnkr1.fsf@alter.siamese.dyndns.org>
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 <jrnieder@gmail.com> ---
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);