I have a zsh script myscr.zsh, which sources another script zparse.zsh, like this:
# This is mysrc.zsh
. $HOME/slib/zparse.zsh FOO BAR # some parameters passed to zparse.zsh
zparse.zsh expects a variable number of parameters, which do NOT need to be parsed using zparseopt, but zparse.zsh should - among other things - parse the positional parameters passed to mysrc.zsh. This is tricky, since mysrc.zsh has its own copy of $, which is different from the $
To make things even more difficult, zparseopt does by design always parse $*; we can't tell it to use a different array.
I know that what I want is a bit like "having a cake and eating it too" - my
has its own parameter list, but also wants to process the parameter list of its parent.
My current, and not elegant, approach goes like this: I define a special, "magic" parameter (say:
), which is known to not occur within the parameter list given to zparse.zsh. I then invoke zparse.zsh like this:
. zparse.zsh FOO BAR % "$@"
Within zparse, I first shift the parameters until the magic parameter out of $* and into a local array, which leaves me with "$@" which is now identical to the one in the calling script, and use zparseopt on this.
I wonder whether there is a simpler solution. For example, can I somehow access the shell variables (and hence the $*) of the sourcing shell? I'm not talking about accessing the environment of the parent process, because we are in the same process here. Maybe zsh with all its goodies has some helpful feature, which I'm just unaware of?