I would like to split $argv into two variables. One with opts and one with everything else.
function filter_opts
set -l filtered_opts
set -l new_argv
for opt in $argv
if string match -r -- '-.*' $opt
set -a filtered_opts $opt
else
set -a new_argv $opt
end
end
set -g argv $new_argv
echo $filtered_opts
end
function test_filter
set -g argv $argv
echo $argv
set -l opts (filter_opts $argv)
echo $opts
echo $argv
# prog1 $opts $argv
# prog2 $argv
end
But the output has the filtered opts duplicated and $argv isn't modified... :-(
$ test_filter Twilight Zone --test -t
Twilight Zone --test -t
--test -t --test -t
Twilight Zone --test -t
Ideally the output would look like this:
$ test_filter Twilight Zone --test -t
Twilight Zone --test -t
--test -t
Twilight Zone
Two improvements:
$argv, you can just pass multiple arguments tostring filter.--no-scope-shadowing(see function docs)Putting those ideas together:
this outputs:
also don't miss
argparsewhich is fish's argument parsing command.