In a new addition to a script I am working on, I want to loop through parameters associated with a flag until zsh reaches the next flag in the command. For instance, when the user enters the following:
datt -p Package1 Package2 Package3 --build
The script should take Package1
, Package2
, and Package3
and assign these each to a place in an array. My thought process is to do something similar to this:
packages=()
for i in "$@"; do
packages+=("$i")
done
My question becomes, then, how do I write the script in such a way that this loop keeps going until it hits the next flag on the command line? With the command shown at the beginning of my question (datt -p Package1 Package2 Package3 --build
), how do I prevent the for loop from capturing --build
as a package name and make it stop adding strings to the array after PackageN
?
-p
have something to do with it? The usual way would be to take options first, and non-options after, like Peregrino69 suggested above. That would be directly supported by e.g.getopts
, though it only does short options.bash
tag as your question explicitly mentionszsh
. If thebash
tag is warranted, please mention this in the question (and put the tag back in ;) ).