zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] getopts: Accurately report '-x' or '+x' in error messages
@ 2017-12-01  7:12 ` dana
  2017-12-01  9:20   ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: dana @ 2017-12-01  7:12 UTC (permalink / raw)
  To: zsh-workers

Related to zsh-workers 40010:

getopts error messages about illegal options or missing values always reference
the -x variant of the option, even if the +x one was used instead. The included
patch simply makes it report the correct prefix.

PS: There are a few other places in the code where it seems that the - prefix is
hard-coded into the error message, but i didn't actually check to see if that
was a problem. This patch in addition to the previous one should cover most
cases people are likely to run into, though, i'd think.

PPS: I didn't add any tests because there aren't any for getopts — let me know
if you'd like me to create some.

dana


diff --git a/Src/builtin.c b/Src/builtin.c
index 0ff9e4f16..f002b9912 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5442,8 +5442,9 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
	if(quiet) {
	    zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
	} else {
-	    zwarn(*p == '?' ? "bad option: -%c" :
-		  "argument expected after -%c option", opch);
+	    zwarn(*p == '?' ? "bad option: %c%c" :
+		  "argument expected after %c%c option",
+		  "?-+"[lenoptbuf], opch);
	    zoptarg=ztrdup("");
	}
	return 0;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] getopts: Accurately report '-x' or '+x' in error messages
  2017-12-01  7:12 ` [PATCH] getopts: Accurately report '-x' or '+x' in error messages dana
@ 2017-12-01  9:20   ` Peter Stephenson
  2017-12-01  9:39     ` dana
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2017-12-01  9:20 UTC (permalink / raw)
  To: dana, zsh-workers

On Fri, 1 Dec 2017 01:12:15 -0600
dana <dana@dana.is> wrote:
> getopts error messages about illegal options or missing values always reference
> the -x variant of the option, even if the +x one was used instead. The included
> patch simply makes it report the correct prefix.
> 
> PS: There are a few other places in the code where it seems that the - prefix is
> hard-coded into the error message, but i didn't actually check to see if that
> was a problem. This patch in addition to the previous one should cover most
> cases people are likely to run into, though, i'd think.
> 
> PPS: I didn't add any tests because there aren't any for getopts — let me know
> if you'd like me to create some.

Thanks, I've committed this.  I'd be happy to add some getopts tests if
you came up with any.

pws


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] getopts: Accurately report '-x' or '+x' in error messages
  2017-12-01  9:20   ` Peter Stephenson
@ 2017-12-01  9:39     ` dana
  2017-12-01  9:48       ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: dana @ 2017-12-01  9:39 UTC (permalink / raw)
  To: zsh-workers; +Cc: Peter Stephenson

> Thanks, I've committed this.  I'd be happy to add some getopts tests if
> you came up with any.

I did actually come up with some, though i'm slightly unsure of their quality.
How do you feel about these?

dana


diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst
new file mode 100644
index 000000000..7eba5a4b1
--- /dev/null
+++ b/Test/B10getopts.ztst
@@ -0,0 +1,81 @@
+# Test the getopts builtin.
+
+%prep
+
+  test_getopts() {
+    local OPTARG OPTIND opt
+    local -a res
+    while getopts abc: opt 2>&1; do
+      [[ $opt == [?:] ]] || res+=( $opt )
+    done
+    (( $#res )) && print -r -- $res
+    return $(( $#res ? 0 : 1 ))
+  }
+
+%test
+
+  test_getopts
+1:no arguments
+
+  test_getopts foo
+1:one operand
+
+  test_getopts -a
+0:one option
+>a
+
+  test_getopts -a foo
+0:one option, one operand
+>a
+
+  test_getopts -a foo -b
+0:one option, two operands, leading hyphen
+>a
+
+  test_getopts -ab
+0:two options, single argument
+>a b
+
+  test_getopts -a -b
+0:two options, separate arguments
+>a b
+
+  test_getopts -a -b +a
+0:three options, + variant
+>a b +a
+
+  test_getopts -cx
+0:one option with value, single argument
+>c
+
+  test_getopts +cx
+0:one option with value, single argument, + variant
+>+c
+
+  test_getopts -c x
+0:one option with value, separate arguments
+>c
+
+  test_getopts -acx
+0:two options, one with value, single argument
+>a c
+
+  test_getopts -ac x
+0:two options, one with value, separate arguments
+>a c
+
+  test_getopts -c
+1:one option missing value
+>test_getopts:3: argument expected after -c option
+
+  test_getopts +c
+1:one option missing value, + variant
+>test_getopts:3: argument expected after +c option
+
+  test_getopts -x
+1:one illegal option
+>test_getopts:3: bad option: -x
+
+  test_getopts +x
+1:one illegal option, + variant
+>test_getopts:3: bad option: +x



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] getopts: Accurately report '-x' or '+x' in error messages
  2017-12-01  9:39     ` dana
@ 2017-12-01  9:48       ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2017-12-01  9:48 UTC (permalink / raw)
  To: dana, zsh-workers

On Fri, 1 Dec 2017 03:39:00 -0600
dana <dana@dana.is> wrote:
> > Thanks, I've committed this.  I'd be happy to add some getopts tests if
> > you came up with any.
> 
> I did actually come up with some, though i'm slightly unsure of their quality.
> How do you feel about these?

Given we don't have any, I think this is a good start and I'll commit
them, thanks.

pws


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-12-01  9:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20171201071310epcas1p4e06bf3264433a5d7318fbb9d6c594c18@epcas1p4.samsung.com>
2017-12-01  7:12 ` [PATCH] getopts: Accurately report '-x' or '+x' in error messages dana
2017-12-01  9:20   ` Peter Stephenson
2017-12-01  9:39     ` dana
2017-12-01  9:48       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).