You can do it:
for item in $argv switch "$item" case -f --foo case -b --bar end end
The above does not support writing short options to a single -fbz argument, the option values ββare --foo=baz , --foo baz or f baz , the negative assignment is --name!=value , the end of the options -- and the dash - and -- are always part argument.
To solve these problems, I wrote a getopts function.
getopts -ab1 --foo=bar baz
Now let's look at the result.
a b 1 foo bar _ baz
The items on the left represent parameter flags or keys associated with the CLI. Elements on the right are parameter values. The underscore _ is the default key for keyless arguments.
Use read(1) to process the generated stream and switch(1) to match the patterns:
getopts -ab1 --foo=bar baz | while read -l key option switch $key case _ case a case b case foo end end
See the documentation.
source share