summaryrefslogtreecommitdiff
path: root/parse-options.c
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivano@gnu.org>2010-05-17 17:34:41 +0200
committerJunio C Hamano <gitster@pobox.com>2010-05-31 18:06:41 -0700
commit9c7304e3e39ed397b3cc6566573333e2698a52b4 (patch)
treefb0b9f66cd82761e67ae77074c973acb4bf6fa1a /parse-options.c
parent81fa024cd8e336ba257f13fe7724b95baacfa3ad (diff)
downloadgit-9c7304e3e39ed397b3cc6566573333e2698a52b4.tar.gz
print the usage string on stdout instead of stderr
When -h is used, print usage messages on stdout. If a command is invoked with wrong arguments then print the usage messages on stderr. Signed-off-by: Giuseppe Scrivano <gscrivano@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'parse-options.c')
-rw-r--r--parse-options.c64
1 files changed, 33 insertions, 31 deletions
diff --git a/parse-options.c b/parse-options.c
index 8546d8526f..c8aaf95b4a 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -5,7 +5,7 @@
#include "color.h"
static int parse_options_usage(const char * const *usagestr,
- const struct option *opts);
+ const struct option *opts, int err);
#define OPT_SHORT 1
#define OPT_UNSET 2
@@ -352,7 +352,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
}
static int usage_with_options_internal(const char * const *,
- const struct option *, int);
+ const struct option *, int, int);
int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options,
@@ -380,10 +380,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
if (arg[1] != '-') {
ctx->opt = arg + 1;
if (internal_help && *ctx->opt == 'h')
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 1);
case -2:
goto unknown;
}
@@ -391,10 +391,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
check_typos(arg + 1, options);
while (ctx->opt) {
if (internal_help && *ctx->opt == 'h')
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 1);
case -2:
/* fake a short option thing to hide the fact that we may have
* started to parse aggregated stuff
@@ -418,12 +418,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
}
if (internal_help && !strcmp(arg + 2, "help-all"))
- return usage_with_options_internal(usagestr, options, 1);
+ return usage_with_options_internal(usagestr, options, 1, 0);
if (internal_help && !strcmp(arg + 2, "help"))
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 0);
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(usagestr, options, 1);
case -2:
goto unknown;
}
@@ -468,7 +468,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
return parse_options_end(&ctx);
}
-static int usage_argh(const struct option *opts)
+static int usage_argh(const struct option *opts, FILE *outfile)
{
const char *s;
int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
@@ -479,72 +479,74 @@ static int usage_argh(const struct option *opts)
s = literal ? "[%s]" : "[<%s>]";
else
s = literal ? " %s" : " <%s>";
- return fprintf(stderr, s, opts->argh ? opts->argh : "...");
+ return fprintf(outfile, s, opts->argh ? opts->argh : "...");
}
#define USAGE_OPTS_WIDTH 24
#define USAGE_GAP 2
static int usage_with_options_internal(const char * const *usagestr,
- const struct option *opts, int full)
+ const struct option *opts, int full, int err)
{
+ FILE *outfile = err ? stderr : stdout;
+
if (!usagestr)
return PARSE_OPT_HELP;
- fprintf(stderr, "usage: %s\n", *usagestr++);
+ fprintf(outfile, "usage: %s\n", *usagestr++);
while (*usagestr && **usagestr)
- fprintf(stderr, " or: %s\n", *usagestr++);
+ fprintf(outfile, " or: %s\n", *usagestr++);
while (*usagestr) {
- fprintf(stderr, "%s%s\n",
+ fprintf(outfile, "%s%s\n",
**usagestr ? " " : "",
*usagestr);
usagestr++;
}
if (opts->type != OPTION_GROUP)
- fputc('\n', stderr);
+ fputc('\n', outfile);
for (; opts->type != OPTION_END; opts++) {
size_t pos;
int pad;
if (opts->type == OPTION_GROUP) {
- fputc('\n', stderr);
+ fputc('\n', outfile);
if (*opts->help)
- fprintf(stderr, "%s\n", opts->help);
+ fprintf(outfile, "%s\n", opts->help);
continue;
}
if (!full && (opts->flags & PARSE_OPT_HIDDEN))
continue;
- pos = fprintf(stderr, " ");
+ pos = fprintf(outfile, " ");
if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
if (opts->flags & PARSE_OPT_NODASH)
- pos += fprintf(stderr, "%c", opts->short_name);
+ pos += fprintf(outfile, "%c", opts->short_name);
else
- pos += fprintf(stderr, "-%c", opts->short_name);
+ pos += fprintf(outfile, "-%c", opts->short_name);
}
if (opts->long_name && opts->short_name)
- pos += fprintf(stderr, ", ");
+ pos += fprintf(outfile, ", ");
if (opts->long_name)
- pos += fprintf(stderr, "--%s%s",
+ pos += fprintf(outfile, "--%s%s",
(opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
opts->long_name);
if (opts->type == OPTION_NUMBER)
- pos += fprintf(stderr, "-NUM");
+ pos += fprintf(outfile, "-NUM");
if (!(opts->flags & PARSE_OPT_NOARG))
- pos += usage_argh(opts);
+ pos += usage_argh(opts, outfile);
if (pos <= USAGE_OPTS_WIDTH)
pad = USAGE_OPTS_WIDTH - pos;
else {
- fputc('\n', stderr);
+ fputc('\n', outfile);
pad = USAGE_OPTS_WIDTH;
}
- fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
+ fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
}
- fputc('\n', stderr);
+ fputc('\n', outfile);
return PARSE_OPT_HELP;
}
@@ -552,7 +554,7 @@ static int usage_with_options_internal(const char * const *usagestr,
void usage_with_options(const char * const *usagestr,
const struct option *opts)
{
- usage_with_options_internal(usagestr, opts, 0);
+ usage_with_options_internal(usagestr, opts, 0, 1);
exit(129);
}
@@ -565,9 +567,9 @@ void usage_msg_opt(const char *msg,
}
static int parse_options_usage(const char * const *usagestr,
- const struct option *opts)
+ const struct option *opts, int err)
{
- return usage_with_options_internal(usagestr, opts, 0);
+ return usage_with_options_internal(usagestr, opts, 0, err);
}