Skip to content

Commit

Permalink
Merge branch 'uk/rev-parse-parse-opt'
Browse files Browse the repository at this point in the history
* uk/rev-parse-parse-opt:
  parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse
  more tests for git rev-parse --parse-opt
  • Loading branch information
Junio C Hamano committed Jul 6, 2009
2 parents 3115fe9 + 6e0800e commit 5cacf3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Documentation/git-rev-parse.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ OPTIONS
Only meaningful in `--parseopt` mode. Tells the option parser to echo
out the first `--` met instead of skipping it.

--stop-at-non-option::
Only meaningful in `--parseopt` mode. Lets the option parser stop at
the first non-option argument. This can be used to parse sub-commands
that take options themself.

--sq-quote::
Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
section below). In contrast to the `--sq` option below, this
Expand Down
8 changes: 6 additions & 2 deletions builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,17 @@ static const char *skipspaces(const char *s)

static int cmd_parseopt(int argc, const char **argv, const char *prefix)
{
static int keep_dashdash = 0;
static int keep_dashdash = 0, stop_at_non_option = 0;
static char const * const parseopt_usage[] = {
"git rev-parse --parseopt [options] -- [<args>...]",
NULL
};
static struct option parseopt_opts[] = {
OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
"keep the `--` passed as an arg"),
OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
"stop parsing after the "
"first non-option argument"),
OPT_END(),
};

Expand Down Expand Up @@ -394,7 +397,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
ALLOC_GROW(opts, onb + 1, osz);
memset(opts + onb, 0, sizeof(opts[onb]));
argc = parse_options(argc, argv, prefix, opts, usage,
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);

strbuf_addf(&parsed, " --");
sq_quote_argv(&parsed, argv, 0);
Expand Down
43 changes: 41 additions & 2 deletions t/t1502-rev-parse-parseopt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Extras
EOF

test_expect_success 'test --parseopt help output' '
git rev-parse --parseopt -- -h 2> output.err <<EOF
cat > optionspec << EOF
some-command [options] <args>...
some-command does foo and bar!
Expand All @@ -37,7 +36,47 @@ C? option C with an optional argument
Extras
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
'

cat > expect <<EOF
set -- --foo --bar 'ham' -- 'arg'
EOF

test_expect_success 'test --parseopt' '
git rev-parse --parseopt -- --foo --bar=ham arg < optionspec > output &&
test_cmp expect output
'

test_expect_success 'test --parseopt with mixed options and arguments' '
git rev-parse --parseopt -- --foo arg --bar=ham < optionspec > output &&
test_cmp expect output
'

cat > expect <<EOF
set -- --foo -- 'arg' '--bar=ham'
EOF

test_expect_success 'test --parseopt with --' '
git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
test_cmp expect output
'

test_expect_success 'test --parseopt --stop-at-non-option' '
git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
test_cmp expect output
'

cat > expect <<EOF
set -- --foo -- '--' 'arg' '--bar=ham'
EOF

test_expect_success 'test --parseopt --keep-dashdash' '
git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
test_cmp expect output
'

test_done

0 comments on commit 5cacf3d

Please sign in to comment.