From: Clemens Buchacher <drizzd@aon.at>
To: "Shawn O. Pearce" <spearce@spearce.org>
Cc: git@vger.kernel.org
Subject: [PATCH 1/2] do not mangle short options which take arguments
Date: Thu, 1 Oct 2009 22:16:48 +0200 [thread overview]
Message-ID: <20091001201648.GA12175@localhost> (raw)
In-Reply-To: <20090925233226.GC14660@spearce.org>
Instead of
$ git commit -a -ammend
[work ce38944] mend
1 files changed, 2 insertions(+), 0 deletions(-)
we now get
$ git commit -a -ammend
error: switch `m' must not be mangled with other options
usage: git commit [options] [--] <filepattern>...
[...]
Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
On Fri, Sep 25, 2009 at 04:32:26PM -0700, Shawn O. Pearce wrote:
> I wonder, should the -m flag on commit not allow cuddling its
> value against the switch when its combined in short form with
> other switches?
Here we go.
Clemens
parse-options.c | 16 ++++++++++++----
t/t0040-parse-options.sh | 12 ++++++++++++
t/t3701-add-interactive.sh | 2 +-
3 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index a64a4d6..4f16f37 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -5,6 +5,7 @@
#define OPT_SHORT 1
#define OPT_UNSET 2
+#define OPT_MANY 4
static int opterror(const struct option *opt, const char *reason, int flags)
{
@@ -43,9 +44,12 @@ static int get_value(struct parse_opt_ctx_t *p,
const struct option *opt, int flags)
{
const char *s, *arg;
+ const int many = flags & OPT_MANY;
const int unset = flags & OPT_UNSET;
int err;
+ if (many && !(opt->flags & PARSE_OPT_NOARG))
+ return opterror(opt, "must not be mangled with other options", flags);
if (unset && p->opt)
return opterror(opt, "takes no value", flags);
if (unset && (opt->flags & PARSE_OPT_NONEG))
@@ -149,14 +153,18 @@ static int get_value(struct parse_opt_ctx_t *p,
}
}
-static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
+static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option
+ *options, int many)
{
const struct option *numopt = NULL;
+ int flags = OPT_SHORT;
+ if (many)
+ flags |= OPT_MANY;
for (; options->type != OPTION_END; options++) {
if (options->short_name == *p->opt) {
p->opt = p->opt[1] ? p->opt + 1 : NULL;
- return get_value(p, options, OPT_SHORT);
+ return get_value(p, options, flags);
}
/*
@@ -374,7 +382,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
ctx->opt = arg + 1;
if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
- switch (parse_short_opt(ctx, options)) {
+ switch (parse_short_opt(ctx, options, 0)) {
case -1:
return parse_options_usage(usagestr, options);
case -2:
@@ -385,7 +393,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
while (ctx->opt) {
if (internal_help && *ctx->opt == 'h')
return parse_options_usage(usagestr, options);
- switch (parse_short_opt(ctx, options)) {
+ switch (parse_short_opt(ctx, options, 1)) {
case -1:
return parse_options_usage(usagestr, options);
case -2:
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index bbc821e..86eb350 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -315,4 +315,16 @@ test_expect_success 'OPT_NUMBER_CALLBACK() works' '
test_cmp expect output
'
+cat > mangle.err << EOF
+error: switch \`s' must not be mangled with other options
+EOF
+
+cat mangle.err expect.err > expect-mangle.err
+
+test_expect_success 'do not mangle options which require arguments' '
+ test_must_fail test-parse-options -bs123 > output 2> output.err &&
+ ! test -s output &&
+ test_cmp expect-mangle.err output.err
+'
+
test_done
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 62fd65e..208a134 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -207,7 +207,7 @@ index b6f2c08..61b9053 100755
EOF
# Test splitting the first patch, then adding both
test_expect_success 'add first line works' '
- git commit -am "clear local changes" &&
+ git commit -a -m "clear local changes" &&
git apply patch &&
(echo s; echo y; echo y) | git add -p file &&
git diff --cached > diff &&
--
1.6.5.rc1.214.g13c5a
next prev parent reply other threads:[~2009-10-01 20:16 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-25 23:32 how optparse can go horribly wrong Shawn O. Pearce
2009-09-26 1:51 ` Nicolas Sebrecht
2009-09-26 13:44 ` Sverre Rabbelier
2009-09-26 19:25 ` Shawn O. Pearce
2009-09-28 13:37 ` Clemens Buchacher
2009-10-01 20:16 ` Clemens Buchacher [this message]
2009-10-01 20:23 ` [PATCH 2/2] allow mangling short options which take integer arguments Clemens Buchacher
2009-10-01 21:55 ` Johannes Schindelin
2009-10-02 7:43 ` Clemens Buchacher
2009-10-02 7:50 ` Jeff King
2009-10-02 8:26 ` Clemens Buchacher
2009-10-02 8:41 ` Johannes Schindelin
2009-10-03 9:23 ` Clemens Buchacher
2009-10-01 21:53 ` [PATCH 1/2] do not mangle short options which take arguments Johannes Schindelin
2009-10-02 6:11 ` Jeff King
2009-10-02 7:36 ` Clemens Buchacher
2009-10-02 7:46 ` Paolo Bonzini
2009-10-02 7:57 ` Jeff King
2009-10-02 8:42 ` Johannes Schindelin
2009-10-02 8:43 ` Jeff King
2009-10-02 9:04 ` Johannes Schindelin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20091001201648.GA12175@localhost \
--to=drizzd@aon.at \
--cc=git@vger.kernel.org \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.