Wednesday, April 13, 2011

Command line instruction parsing

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)

From stackoverflow
  • 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.

    vartec : actually it's not in PEAR anymore, it's a standard function
    Paul Dixon : it is in PEAR, Seems SO does odd things with URLs containing underscores, have changed the link...
  • Not sure if this would help with this question, but try RegexLib. It's an inmense Regex library :)

  • 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

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

  • getopt()

  • @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

    vartec : "comment", nor "answer" for comments
  • Just thought I'd let you know I found a solution.

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.