tags:

views:

251

answers:

7

I've been all over google and haven't been able to find a regex that would parse (correctly) CLI arguments.

Does anyone have in their code library such a thing?

Ideally it would parse all styles of arguments (i.e.: -v -abc --arg=val --arg="val1 val2" --arg "val")

Thanks!

P.S.: This would be used in PHP context (preg)

+7  A: 

Have a look at PEAR's Console_Getopt or the Zend Framework Zend_Console_Getopt

Your PHP version may have support for getopt as a built-in also.

Paul Dixon
actually it's not in PEAR anymore, it's a standard function
vartec
it is in PEAR, Seems SO does odd things with URLs containing underscores, have changed the link...
Paul Dixon
A: 

Not sure if this would help with this question, but try RegexLib. It's an inmense Regex library :)

Seb
+1  A: 

Thanks for the fast replies, I did have a look into those 2, the only problem is that they don't allow anonymous options, every option has to be defined in the first place to be matched

A: 

The global $argv already parses the command line arguments automatically if you're running CLI. Also see: Using PHP from the command line

acrosman
+4  A: 

getopt()

vartec
+1  A: 

@acrosman: $argv parses the arguments just partially, i.e.: it extracts them individually but you still have to build the logic that extracts the arguments into property -> value

"comment", nor "answer" for comments
vartec
A: 

Just thought I'd let you know I found a solution.

http://nlindblad.org/2007/05/12/handling-command-line-arguments-in-php/

Andrei Serdeliuc