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

Re: Minor issue about zparseopts



On Aug 19,  8:47pm, Jesper Nygards wrote:
}
}     zparseopts -D -E v+:=v || return
} 
} seems a bit inelegant to me.
} 
} 1. Why is zparseopts implemented this way? the $v array has "-v" as odd
} elements, and the arguments themselves as even elements.

Several reasons:

* You can use the same array for multiple different flags, e.g.

    zparseopts -D v+:=args x+=args

and in that case you need to be able to tell which values were paired with
which flag.

* A common use of zparseopts is to extract the arguments of some common
external command, augment them in some way, and then call through to the
actual external command with the augmented list.  It's convenient to be
able to call through with just

    something $v

rather than e.g.

   something ${(s:,:):-"-v,"${^v}}

} 2. Is there a more elegant way of extracting the "real arguments", i.e. the
} even elements of the array, than the one I use above?

There are probably several of them.  Most obvious is that zsh allows
more than one loop variable in a for loop.

    local dashv filter
    for dashv filter in $v; do filters+=($filter); done

Or if you know that none of the filters is "-v" you can just do

    filters=( ${v#-v} )



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