Quantcast
Channel: Active questions tagged fish - Stack Overflow
Viewing all articles
Browse latest Browse all 106

fish shell: filter $argv (lightweight / dynamic argparse)

$
0
0

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_optsendfunction test_filter     set -g argv $argv    echo $argv    set -l opts (filter_opts $argv)    echo $opts    echo $argv    # prog1 $opts $argv    # prog2 $argvend

But the output has the filtered opts duplicated and $argv isn't modified... :-(

$ test_filter Twilight Zone --test -tTwilight Zone --test -t--test -t --test -tTwilight Zone --test -t

Ideally the output would look like this:

$ test_filter Twilight Zone --test -tTwilight Zone --test -t--test -tTwilight Zone

Viewing all articles
Browse latest Browse all 106

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>