aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2010-06-21 06:02:45 -0700
committerJunio C Hamano <gitster@pobox.com>2010-06-21 06:02:45 -0700
commit632d3f4b5b80700c6c7c3637d6a4d1d9a457f7e1 (patch)
treebdf796bb5b9c6bbafaaaa9b9ba8b3b50e9c83bc7
parent762655010d6f051f632a77b8c5b95b9f5cad02c9 (diff)
parent47e9cd28f8a404a0d6293935252ddca5fc243931 (diff)
downloadgit-632d3f4b5b80700c6c7c3637d6a4d1d9a457f7e1.tar.gz
git-632d3f4b5b80700c6c7c3637d6a4d1d9a457f7e1.tar.xz
Merge branch 'gs/usage-to-stdout'
* gs/usage-to-stdout: parseopt: wrap rev-parse --parseopt usage for eval consumption print the usage string on stdout instead of stderr Conflicts: parse-options.h
-rw-r--r--builtin/rev-parse.c3
-rw-r--r--parse-options.c82
-rw-r--r--parse-options.h3
-rwxr-xr-xt/t0040-parse-options.sh8
-rwxr-xr-xt/t1502-rev-parse-parseopt.sh8
5 files changed, 61 insertions, 43 deletions
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 8fbf9d0db..b676e2963 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -408,7 +408,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
memset(opts + onb, 0, sizeof(opts[onb]));
argc = parse_options(argc, argv, prefix, opts, usage,
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
- stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);
+ stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0 |
+ PARSE_OPT_SHELL_EVAL);
strbuf_addf(&parsed, " --");
sq_quote_argv(&parsed, argv, 0);
diff --git a/parse-options.c b/parse-options.c
index 8546d8526..0fa79bc31 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -4,8 +4,9 @@
#include "commit.h"
#include "color.h"
-static int parse_options_usage(const char * const *usagestr,
- const struct option *opts);
+static int parse_options_usage(struct parse_opt_ctx_t *ctx,
+ const char * const *usagestr,
+ const struct option *opts, int err);
#define OPT_SHORT 1
#define OPT_UNSET 2
@@ -351,8 +352,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
}
-static int usage_with_options_internal(const char * const *,
- const struct option *, int);
+static int usage_with_options_internal(struct parse_opt_ctx_t *,
+ const char * const *,
+ const struct option *, int, int);
int parse_options_step(struct parse_opt_ctx_t *ctx,
const struct option *options,
@@ -380,10 +382,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(ctx, usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(ctx, usagestr, options, 1);
case -2:
goto unknown;
}
@@ -391,10 +393,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(ctx, usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(ctx, 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 +420,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(ctx, usagestr, options, 1, 0);
if (internal_help && !strcmp(arg + 2, "help"))
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(ctx, usagestr, options, 0);
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
- return parse_options_usage(usagestr, options);
+ return parse_options_usage(ctx, usagestr, options, 1);
case -2:
goto unknown;
}
@@ -468,7 +470,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 +481,81 @@ 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)
+static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
+ const char * const *usagestr,
+ 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++);
+ if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
+ fprintf(outfile, "cat <<\\EOF\n");
+
+ 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);
+
+ if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
+ fputs("EOF\n", outfile);
return PARSE_OPT_HELP;
}
@@ -552,7 +563,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(NULL, usagestr, opts, 0, 1);
exit(129);
}
@@ -564,10 +575,11 @@ void usage_msg_opt(const char *msg,
usage_with_options(usagestr, options);
}
-static int parse_options_usage(const char * const *usagestr,
- const struct option *opts)
+static int parse_options_usage(struct parse_opt_ctx_t *ctx,
+ const char * const *usagestr,
+ const struct option *opts, int err)
{
- return usage_with_options_internal(usagestr, opts, 0);
+ return usage_with_options_internal(ctx, usagestr, opts, 0, err);
}
diff --git a/parse-options.h b/parse-options.h
index 678b58db8..7435cdbf1 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -36,7 +36,8 @@ enum parse_opt_option_flags {
PARSE_OPT_LASTARG_DEFAULT = 16,
PARSE_OPT_NODASH = 32,
PARSE_OPT_LITERAL_ARGHELP = 64,
- PARSE_OPT_NEGHELP = 128
+ PARSE_OPT_NEGHELP = 128,
+ PARSE_OPT_SHELL_EVAL = 256
};
struct option;
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index 3d450ed37..20924506a 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -7,7 +7,7 @@ test_description='our own option parser'
. ./test-lib.sh
-cat > expect.err << EOF
+cat > expect << EOF
usage: test-parse-options <options>
-b, --boolean get a boolean
@@ -46,10 +46,12 @@ EOF
test_expect_success 'test help' '
test_must_fail test-parse-options -h > output 2> output.err &&
- test ! -s output &&
- test_cmp expect.err output.err
+ test ! -s output.err &&
+ test_cmp expect output
'
+mv expect expect.err
+
cat > expect << EOF
boolean: 2
integer: 1729
diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh
index e50405806..434679585 100755
--- a/t/t1502-rev-parse-parseopt.sh
+++ b/t/t1502-rev-parse-parseopt.sh
@@ -3,7 +3,8 @@
test_description='test git rev-parse --parseopt'
. ./test-lib.sh
-cat > expect.err <<EOF
+cat > expect <<\END_EXPECT
+cat <<\EOF
usage: some-command [options] <args>...
some-command does foo and bar!
@@ -19,6 +20,7 @@ Extras
--extra1 line above used to cause a segfault but no longer does
EOF
+END_EXPECT
cat > optionspec << EOF
some-command [options] <args>...
@@ -38,8 +40,8 @@ extra1 line above used to cause a segfault but no longer does
EOF
test_expect_success 'test --parseopt help output' '
- git rev-parse --parseopt -- -h 2> output.err < optionspec
- test_cmp expect.err output.err
+ git rev-parse --parseopt -- -h > output < optionspec
+ test_cmp expect output
'
cat > expect <<EOF