Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] don't exit shell on [[ -o invalid@option ]]



Op 14-11-17 om 14:26 schreef Daniel Shahaf:
> As Bart hinted, the proposed change is backwards *incompatible*: it
> makes [[ -o invalid@option ]] return 1 where currently it returns 2.

Actually, it exits/aborts the shell with status 2. This is an essential
difference from simply returning status 2.

> Moreover, if cross-version compatibility is the goal, why is it a good
> thing to lump "This shell does not have INVALID_OPTION" and "This shell
> has INVALID_OPTION and it's unset"?

Because
(a) that's how all other [[ -o ... ]] implementations work,
(b) that's what the current documentation in zshmisc.1 says that [[ -o
...] does, and
(c) the canonical way to check if an option exists
   if (set +o someoption) 2>/dev/null; then ...
works fine on all shells.

Re (b), zshmisc.1 simply says that [[ -o ... ]] checks if the option is
on. It says nothing about checking if the option exists, much less
aborting the shell if it doesn't.

> It's easy to imagine a situation in
> which that'd be a bug: if INVALID_OPTION was added in zsh version N, is
> set by default, and a plugin that was developed against version N is
> installed by a user running version N-1.

I don't see how that would introduce a bug. If a new option NEW_OPTION
is introduced in zsh version N, default on, then [[ -o NEW_OPTION ]] can
be used to run code dependent on that new option only if that new option
is on. That code will then never be run on version N-1, which is what is
expected.

> With the current code that
> situation would result in a (proper) warning.

A mere warning would be fine, but what actually happens is that the
shell aborts with an error message.

> Devil's advocate, but why can't people just do, today,
> 
>     if [[ -o INVALID_OPTION ]] 2>/dev/null; then

Because, on zsh (unlike bash and *ksh) that will cause the shell to
exit, as in, the program aborts. Not only that, adding 2>/dev/null will
cause the program to abort silently, because the error message is
suppressed.

You can of course do

    if ([[ -o INVALID_OPTION ]]) 2>/dev/null; then

but that comes at the cost of forking a subshell, so that had better not
be within a loop with many iterations.

>     if [[ ${options[invalidoption]:-off} == off ]]; then

That's fine if the script needs to work on zsh only. Not very intuitive,
though.

- Martijn



Messages sorted by: Reverse Date, Date, Thread, Author