@matheusbsbs / matheus-bagliano / commits / 6e0800ef25
Fork 0

parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Uwe Kleine-König committed Jun 14, 2009 at 01:58 UTC 6e0800ef2575751f2e20d11e2cfe305304e5e9b8
3 files changed +16 -2
Documentation/git-rev-parse.txt
+5
@@ -30,6 +30,11 @@ OPTIONS
30 Only meaningful in `--parseopt` mode. Tells the option parser to echo
31 out the first `--` met instead of skipping it.
32
33 +--stop-at-non-option::
34 + Only meaningful in `--parseopt` mode. Lets the option parser stop at
35 + the first non-option argument. This can be used to parse sub-commands
36 + that take options themself.
37 +
38 --sq-quote::
39 Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
40 section below). In contrast to the `--sq` option below, this
builtin-rev-parse.c
+6 -2
@@ -301,7 +301,7 @@ static const char *skipspaces(const char *s)
301
302 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
303 {
304 - static int keep_dashdash = 0;
304 + static int keep_dashdash = 0, stop_at_non_option = 0;
305 static char const * const parseopt_usage[] = {
306 "git rev-parse --parseopt [options] -- [<args>...]",
307 NULL
@@ -309,6 +309,9 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
309 static struct option parseopt_opts[] = {
310 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
311 "keep the `--` passed as an arg"),
312 + OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
313 + "stop parsing after the "
314 + "first non-option argument"),
315 OPT_END(),
316 };
317
@@ -394,7 +397,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
397 ALLOC_GROW(opts, onb + 1, osz);
398 memset(opts + onb, 0, sizeof(opts[onb]));
399 argc = parse_options(argc, argv, prefix, opts, usage,
397 - keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
400 + keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
401 + stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);
402
403 strbuf_addf(&parsed, " --");
404 sq_quote_argv(&parsed, argv, 0);
t/t1502-rev-parse-parseopt.sh
+5
@@ -65,6 +65,11 @@ test_expect_success 'test --parseopt with --' '
65 test_cmp expect output
66 '
67
68 +test_expect_success 'test --parseopt --stop-at-non-option' '
69 + git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
70 + test_cmp expect output
71 +'
72 +
73 cat > expect <<EOF
74 set -- --foo -- '--' 'arg' '--bar=ham'
75 EOF