'Remove some arguments from argument string in zsh

I'm trying to remove part of an arguments string using zsh parameter expansion (no external tools like sed please). Here's what for:

The RUBYOPT environment variable contains arguments which are applied whenever the ruby interpreter is used just as if they were given along with the ruby command. One argument controls the warning verbosity, possible settings are for instance -W0 or -W:no-deprecated. My goal is to remove all all -W... from RUBYOPT, say:

  • -W0 -X -> -X
  • -W:no-deprecated -X -W1 -> -X

My current approach is to split the string to an array and then make a substitution on every member of the array. This works on two lines of code, but I can't make it work on a single line of code:

% RUBYOPT="-W:no-deprecated -X -W1"

% parts=(${(@s: :)RUBYOPT})
% echo ${parts/-W*}
-X

% echo ${(${(@s: :)RUBYOPT})/-W*}
zsh: error in flags

What am I doing wrong here... or is there a different, more elegant way to achieve this?

Thanks for your hints!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source