How can I modify complex command-line argument strings in Perl?

Posted by mmccoo on Stack Overflow See other posts from Stack Overflow or by mmccoo
Published on 2010-03-25T20:44:21Z Indexed on 2010/03/25 23:33 UTC
Read the original article Hit count: 382

Filed under:
|

I have a command line that I'm trying to modify to remove some of the arguments. What makes this complex is that I can have nested arguments.

Say that I have this:

$cmdline = "-a -xyz -a- -b -xyz -b- -a -xyz -a-"

I have three different -xyz flags that are to be interpreted in two different contexts. One is the -a context and the other is the -b context.

I want to remove the "a" -xyz's but leave the ones in the "b" -xyz.

in the above case, I want:

-a -a- -b -xyz -b- -a -a-

Alternately, if I have:

-a -123 -a- -b -xyz -b- -a -xyz -a-"

I want:

-a -123 -a- -a -xyz -a- -b -xyz -b- -a -a-

It's this second case that I'm stuck on.

How can I most effectively do this in Perl?

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex