zsh-workers
 help / color / mirror / Atom feed
* [PATCH] Add leading '-' to zparseopts option parsing errors
@ 2021-02-03 15:46 Joshua Krusell
  0 siblings, 0 replies; only message in thread
From: Joshua Krusell @ 2021-02-03 15:46 UTC (permalink / raw)
  To: zsh-workers; +Cc: Joshua Krusell

This only affects error messages associated with parsing positional
parameters. Error messages from parsing option specs are kept as is
without the leading dash to match how options are defined when calling
zparseopts.

---

This would be the new behaviour:

% set -- -h
% zparseopts -- h:
zparseopts: no default array defined: h:
% zparseopts -a foo -- h h
zparseopts: option defined more than once: h
% zparseopts -- h:=foo
zparseopts: missing argument for option: -h

I'm not sure this reduces ambiguity besides the example that Daniel
mentioned earlier.

% set -- --x
% zparseopts -F -- x:=foo
zparseopts: bad option: --x

/Joshua

 Src/Modules/zutil.c     | 8 ++++----
 Test/V12zparseopts.ztst | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c
index c8017d0c0..cecea6d51 100644
--- a/Src/Modules/zutil.c
+++ b/Src/Modules/zutil.c
@@ -1874,9 +1874,9 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		if (!(d = sopts[STOUC(*o)])) {
 		    if (fail) {
 			if (*o != '-')
-			    zwarnnam(nam, "bad option: %c", *o);
+			    zwarnnam(nam, "bad option: -%c", *o);
 			else
-			    zwarnnam(nam, "bad option: %s", o);
+			    zwarnnam(nam, "bad option: -%s", o);
 			return 1;
 		    }
 		    o = NULL;
@@ -1889,7 +1889,7 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		    } else if (!(d->flags & ZOF_OPT) ||
 			       (pp[1] && pp[1][0] != '-')) {
 			if (!pp[1]) {
-			    zwarnnam(nam, "missing argument for option: %s",
+			    zwarnnam(nam, "missing argument for option: -%s",
 				    d->name);
 			    return 1;
 			}
@@ -1916,7 +1916,7 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		else if (!(d->flags & ZOF_OPT) ||
 			 (pp[1] && pp[1][0] != '-')) {
 		    if (!pp[1]) {
-			zwarnnam(nam, "missing argument for option: %s",
+			zwarnnam(nam, "missing argument for option: -%s",
 				d->name);
 			return 1;
 		    }
diff --git a/Test/V12zparseopts.ztst b/Test/V12zparseopts.ztst
index c41c49022..816e1d041 100644
--- a/Test/V12zparseopts.ztst
+++ b/Test/V12zparseopts.ztst
@@ -65,11 +65,11 @@
     } $=1
   done
 0:zparseopts -F
-?(anon):zparseopts:2: bad option: x
+?(anon):zparseopts:2: bad option: -x
 >ret: 1, optv: , argv: -a -x -z
-?(anon):zparseopts:2: bad option: x
->ret: 1, optv: , argv: -ax -z
 ?(anon):zparseopts:2: bad option: -x
+>ret: 1, optv: , argv: -ax -z
+?(anon):zparseopts:2: bad option: --x
 >ret: 1, optv: , argv: -a --x -z
 
   for 1 in '-a 1 2 3' '1 2 3'; do
@@ -168,5 +168,5 @@
     print -r - ret: $?, optv: $optv, argv: $argv
   } -ab1 -c
 0:missing optarg
-?(anon):zparseopts:2: missing argument for option: c
+?(anon):zparseopts:2: missing argument for option: -c
 >ret: 1, optv: , argv: -ab1 -c
-- 
2.30.0



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-03 15:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-03 15:46 [PATCH] Add leading '-' to zparseopts option parsing errors Joshua Krusell

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git