Skip to content

Commit 6e0800e

Browse files
Uwe Kleine-Königgitster
authored andcommitted
parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse
Signed-off-by: Uwe Kleine-König <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 824af25 commit 6e0800e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Documentation/git-rev-parse.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ OPTIONS
3030
Only meaningful in `--parseopt` mode. Tells the option parser to echo
3131
out the first `--` met instead of skipping it.
3232

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+
3338
--sq-quote::
3439
Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
3540
section below). In contrast to the `--sq` option below, this

builtin-rev-parse.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,17 @@ static const char *skipspaces(const char *s)
301301

302302
static int cmd_parseopt(int argc, const char **argv, const char *prefix)
303303
{
304-
static int keep_dashdash = 0;
304+
static int keep_dashdash = 0, stop_at_non_option = 0;
305305
static char const * const parseopt_usage[] = {
306306
"git rev-parse --parseopt [options] -- [<args>...]",
307307
NULL
308308
};
309309
static struct option parseopt_opts[] = {
310310
OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
311311
"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"),
312315
OPT_END(),
313316
};
314317

@@ -394,7 +397,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
394397
ALLOC_GROW(opts, onb + 1, osz);
395398
memset(opts + onb, 0, sizeof(opts[onb]));
396399
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);
398402

399403
strbuf_addf(&parsed, " --");
400404
sq_quote_argv(&parsed, argv, 0);

t/t1502-rev-parse-parseopt.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ test_expect_success 'test --parseopt with --' '
6565
test_cmp expect output
6666
'
6767

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+
6873
cat > expect <<EOF
6974
set -- --foo -- '--' 'arg' '--bar=ham'
7075
EOF

0 commit comments

Comments
 (0)