summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9c7304e)
raw | patch | inline | side by side (parent: 9c7304e)
author | Thomas Rast <trast@student.ethz.ch> | |
Sat, 12 Jun 2010 12:57:39 +0000 (14:57 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 13 Jun 2010 16:38:14 +0000 (09:38 -0700) |
9c7304e (print the usage string on stdout instead of stderr,
2010-05-17) broke rev-parse --parseopt: when run with -h, the usage
notice on stdout ended up in the shell eval.
Wrap the usage in a cat <<\EOF ... EOF block when printing to stdout.
I do not expect any usage lines to ever start with EOF so this
shouldn't be an undue burden.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-05-17) broke rev-parse --parseopt: when run with -h, the usage
notice on stdout ended up in the shell eval.
Wrap the usage in a cat <<\EOF ... EOF block when printing to stdout.
I do not expect any usage lines to ever start with EOF so this
shouldn't be an undue burden.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c | patch | blob | history | |
parse-options.c | patch | blob | history | |
parse-options.h | patch | blob | history | |
t/t1502-rev-parse-parseopt.sh | patch | blob | history |
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 8fbf9d0db6f40aa8c7cb61d72d0f44446de46826..b676e296357c41cdca2f030d28b511ff986fdad9 100644 (file)
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
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 c8aaf95b4aad62e6d0e531c70862e540272a8e19..0fa79bc31d322e2aab8fce738ca6489a35a51ca4 100644 (file)
--- a/parse-options.c
+++ b/parse-options.c
#include "commit.h"
#include "color.h"
-static int parse_options_usage(const char * const *usagestr,
+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
die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
}
-static int usage_with_options_internal(const char * const *,
+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,
if (arg[1] != '-') {
ctx->opt = arg + 1;
if (internal_help && *ctx->opt == 'h')
- return parse_options_usage(usagestr, options, 0);
+ return parse_options_usage(ctx, usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options, 1);
+ return parse_options_usage(ctx, usagestr, options, 1);
case -2:
goto unknown;
}
check_typos(arg + 1, options);
while (ctx->opt) {
if (internal_help && *ctx->opt == 'h')
- return parse_options_usage(usagestr, options, 0);
+ return parse_options_usage(ctx, usagestr, options, 0);
switch (parse_short_opt(ctx, options)) {
case -1:
- return parse_options_usage(usagestr, options, 1);
+ 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
}
if (internal_help && !strcmp(arg + 2, "help-all"))
- return usage_with_options_internal(usagestr, options, 1, 0);
+ return usage_with_options_internal(ctx, usagestr, options, 1, 0);
if (internal_help && !strcmp(arg + 2, "help"))
- return parse_options_usage(usagestr, options, 0);
+ return parse_options_usage(ctx, usagestr, options, 0);
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
- return parse_options_usage(usagestr, options, 1);
+ return parse_options_usage(ctx, usagestr, options, 1);
case -2:
goto unknown;
}
#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, int err)
+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;
+ if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
+ fprintf(outfile, "cat <<\\EOF\n");
+
fprintf(outfile, "usage: %s\n", *usagestr++);
while (*usagestr && **usagestr)
fprintf(outfile, " or: %s\n", *usagestr++);
}
fputc('\n', outfile);
+ if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
+ fputs("EOF\n", outfile);
+
return PARSE_OPT_HELP;
}
void usage_with_options(const char * const *usagestr,
const struct option *opts)
{
- usage_with_options_internal(usagestr, opts, 0, 1);
+ usage_with_options_internal(NULL, usagestr, opts, 0, 1);
exit(129);
}
usage_with_options(usagestr, options);
}
-static int parse_options_usage(const char * const *usagestr,
+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, err);
+ return usage_with_options_internal(ctx, usagestr, opts, 0, err);
}
diff --git a/parse-options.h b/parse-options.h
index 7581e931da13151473739036a89d9d19303eb18b..e16b4d201a998390672cdade90bacde64d9ad29c 100644 (file)
--- a/parse-options.h
+++ b/parse-options.h
PARSE_OPT_NODASH = 32,
PARSE_OPT_LITERAL_ARGHELP = 64,
PARSE_OPT_NEGHELP = 128,
+ PARSE_OPT_SHELL_EVAL = 256
};
struct option;
index 660487dc082465e31476eb4bcc3fb28f36c07831..434679585555c660f76f42a82f2ff84df3119f01 100755 (executable)
test_description='test git rev-parse --parseopt'
. ./test-lib.sh
-cat > expect <<EOF
+cat > expect <<\END_EXPECT
+cat <<\EOF
usage: some-command [options] <args>...
some-command does foo and bar!
--extra1 line above used to cause a segfault but no longer does
EOF
+END_EXPECT
cat > optionspec << EOF
some-command [options] <args>...