简体   繁体   中英

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!

${(... introduces parameter expansion flags (for expample: ${(s: :)...} ).

It cannot handle ${(${(@s: :... as a parameter expansion, especially as the parameter expansion flags for the (${(@s... part, so zsh yields an error "zsh: error in flags" .

    % RUBYOPT="-W:no-deprecated -X -W1"
    % print -- ${${(s: :)RUBYOPT}/-W*}
    # -X

could rescue.


Use typeset -T feature to manipulate the scalar value by array operators is an option.

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

    typeset -xT RUBYOPT rubyopt ' '
    rubyopt=(${rubyopt:#-W*})

    print -l -- "$RUBYOPT"
    # -X

typeset
...
-T [ SCALAR[=VALUE] ARRAY[=(VALUE...)] [ SEP ] ]
...
the -T option requires zero, two, or three arguments to be present. With no arguments, the list of parameters created in this fashion is shown. With two or three arguments, the first two are the name of a scalar and of an array parameter (in that order) that will be tied together in the manner of $PATH and $path. The optional third argument is a single-character separator which will be used to join the elements of the array to form the scalar; if absent, a colon is used, as with $PATH. Only the first character of the separator is significant; any remaining characters are ignored. Multibyte characters are not yet supported. ...
Both the scalar and the array may be manipulated as normal. If one is unset, the other will automatically be unset too. ...

--- zshbuiltin(1), Shell Bultin Commands, typeset

And rubyopt=(${rubyopt:#-W*}) to filter the array elements

${name:#pattern}

If the pattern matches the value of name, then substitute the empty string; otherwise, just substitute the value of name. If name is an array the matching array elements are removed (use the (M) flag to remove the non-matched elements).

--- zshexpn(1), Parameter Expansion, ${name:#pattern}


Note: It is possible to omit "@" from flags because the empty values are not necessary in this case.

    RUBYOPT="-W:no-deprecated -X -W1"
    parts=(${(s: :)RUBYOPT})
    print -- ${parts/-W*}
    # -X
    print -- ${${(s: :)RUBYOPT}/-W*}
    # -X

Parameter Expansion Flags
...
@
In double quotes, array elements are put into separate words. Eg, "${(@)foo}" is equivalent to "${foo[@]}" and "${(@)foo[1,2]}" is the same as "$foo[1]" "$foo[2]" . This is distinct from field splitting by the f , s or z flags, which still applies within each array element.

--- zshexpn(1), Parameter Expansion Flags, @

If we cannot omit the empty value, ${name:#pattern} syntax could rescue.

    RUBYOPT="-W:no-deprecated  -X -W1"
    parts=("${(@s: :)RUBYOPT}")
    # parts=("-W:no-deprecated" ""  "-X" "-W1")
    # Note the empty value are retained
    print -rC1 -- "${(@qqq)parts:#-W*}"
    # ""
    # "-X"
    print -rC1 -- "${(@qqq)${(@s: :)RUBYOPT}:#-W*}"
    # ""
    # "-X"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM