[-- Attachment #1: Type: text/plain, Size: 95 bytes --] % zsh-5.9 -f % autoload zed % setopt nounset % zed a.txt zed:21: opts[-x]: parameter not set % [-- Attachment #2: 0001-zed-nounset.txt --] [-- Type: text/plain, Size: 369 bytes --] --- Functions/Misc/zed.orig 2022-05-20 16:45:13.709144850 +0300 +++ Functions/Misc/zed 2022-05-20 16:45:56.313326625 +0300 @@ -18,7 +18,7 @@ fun=$+opts[-f] hist=$+opts[-h] bind=$+opts[-b] -if [[ $opts[-x] == <-> ]]; then +if [[ ${opts[-x]-} == <-> ]]; then expand=(-x $opts[-x]) elif (( $+opts[-x] )); then print -r "Integer expected after -x: $opts[-x]" >&2
> On 20 May 2022 at 15:06 Risto Laitinen <risto.laitinen@gmail.com> wrote: > % zsh-5.9 -f > % autoload zed > % setopt nounset > % zed a.txt > zed:21: opts[-x]: parameter not set > % Looks straightforward. zed runs as "emulate zsh"; it seems reasonable that nounset isn't treated as an emulation option, given it's more for debugging, but that means there could well be a lot more of these lurking. pws diff --git a/Functions/Misc/zed b/Functions/Misc/zed index 7d0d590db..582a15d25 100644 --- a/Functions/Misc/zed +++ b/Functions/Misc/zed @@ -18,7 +18,7 @@ zparseopts -D -A opts f h b x: fun=$+opts[-f] hist=$+opts[-h] bind=$+opts[-b] -if [[ $opts[-x] == <-> ]]; then +if [[ $+opts[-x] == 1 && $opts[-x] == <-> ]]; then expand=(-x $opts[-x]) elif (( $+opts[-x] )); then print -r "Integer expected after -x: $opts[-x]" >&2
On Fri, May 20, 2022 at 7:18 AM Peter Stephenson <p.w.stephenson@ntlworld.com> wrote: > > zed runs as "emulate zsh"; it seems reasonable that nounset isn't > treated as an emulation option, given it's more for debugging Hmm. That's a tough one. "... only those options likely to cause portability problems in scripts and functions are altered." NOUNSET certainly seems to fall into that category, because: > that means there could well be a lot more of these lurking.
On Fri, May 20, 2022 at 7:18 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> Looks straightforward.
>
> +if [[ $+opts[-x] == 1 && $opts[-x] == <-> ]]; then
This actually points out a potential issue with nounset in general:
There's no way to pre-declare an associative array key (or even a
normal array element) to prevent this from happening, the way one can
use typeset to declare a scalar.
> On 20 May 2022 at 15:17 Peter Stephenson <p.w.stephenson@ntlworld.com> wrote: > > > > On 20 May 2022 at 15:06 Risto Laitinen <risto.laitinen@gmail.com> wrote: > > % zsh-5.9 -f > > % autoload zed > > % setopt nounset > > % zed a.txt > > zed:21: opts[-x]: parameter not set > > % > > Looks straightforward. This is a little better (it doesn't take account of the point Bart noted). The option handler will return on a bad argument, so actually the internal handling is now redundant. pws diff --git a/Functions/Misc/zed b/Functions/Misc/zed index 7d0d590db..bb075512c 100644 --- a/Functions/Misc/zed +++ b/Functions/Misc/zed @@ -14,15 +14,17 @@ local var opts zed_file_name integer TMOUT=0 okargs=1 fun hist bind local -a expand -zparseopts -D -A opts f h b x: +zparseopts -D -A opts f h b x: || return 1 fun=$+opts[-f] hist=$+opts[-h] bind=$+opts[-b] -if [[ $opts[-x] == <-> ]]; then - expand=(-x $opts[-x]) -elif (( $+opts[-x] )); then - print -r "Integer expected after -x: $opts[-x]" >&2 - return 1 +if (( $+opts[-x] )); then + if [[ $opts[-x] == <-> ]]; then + expand=(-x $opts[-x]) + else + print -r "Integer expected after -x: $opts[-x]" >&2 + return 1 + fi fi [[ $0 = fned ]] && fun=1