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

Test for features?



I see this came up before [1], and since it was relatively recent, I 
assume the answer is the same, but:

Is there a good way to test for features in Zsh?

Specifically, I didn't realize that the ':a' and ':A' modifiers(sp?) 
were recent additions, while dropping my .zshrc onto an OpenSolaris box 
w/ SUNWzsh 4.3.9.  (Though it does make me feel less crazy for not 
having used them the whole time.)

The suggestion in the prior thread really only covered testing for 
functions by checking whether the associated autoload file would be 
found somewhere in the Zsh dir (/usr/share/zsh).  That doesn't work for 
this feature, correct?  (Mainly confirming that there's not some hidden 
'hook' into this functionality)  And for now, I assume I'm stuck testing 
for:

(( $+ZSH_PATCHLEVEL && $ZSH_PATCHLEVEL > 1.4618 ))

...which isn't sooo bad, I guess -- except that I had to determine that 
number via git logs.

I see that the man page suggests not using that number(-ish), though.  
And it'd be affected by --enable-local-patchlevel.  Argh.  So, I guess 
I'm stuck with testing against ZSH_VERSION.  But, as a multi-part 
version-string, what's the best way to do that?

My best estimation so far is something along the lines of:

function zsh_version_ge () {
    local zver cmp i
	zver=( ${(s:.:)ZSH_VERSION} )
	cmp=( ${(s:.:)1} )
	for (( i=1; i<=$#zver && i<=$#cmp; i++ )) do
		if (( $cmp[i] < $zver[i] )) then
			return 0
		elif (( $cmp[i] > $zver[i] )) then
			return 1
		fi
	done
	if (( $#cmp <= $#zver )) then
		return 0
	else
		return 1
	fi
}
# some tests
for v in 2 3.1 4.{3,4} 4.3.{8..12} 4.3.10.1 4.8 19 ; do
	test="zsh_version_ge $v"
	echo -e "$test? \c"
	eval $test && echo yes || echo no
done

[1] http://www.zsh.org/mla/users/2009/msg00983.html



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