I discovered that I reinvented the square wheel. (Round wheel found here.) Now I know how this guy feels.

But I don't feel too bad because everywhere I look I see yet another bad implementation of getopt. Come on people, GNU style is cool style.

I feel like I might fix up my PHP command line options parser to conform more closely with what I'd call "expected" behavior, and support alternative syntaxes. I also half want to write my own in Bash, because everything I've found sucks in one way or another.

I think the best one I've seen so far (clean code, sensible syntax) is the getopt parser in the Python library. Even better, though, is the whole system provided by the Python optparse module. Makes me want to use Python.

Damn good documentation, here. This really explains everything there is to know about command line argument parsing:

option

an argument used to supply extra information to guide or customize the execution of a program. There are many different syntaxes for options; the traditional Unix syntax is a hyphen (“-“) followed by a single letter, e.g. "-x" or "-F". Also, traditional Unix syntax allows multiple options to be merged into a single argument, e.g. "-x -F" is equivalent to "-xF". The GNU project introduced "--" followed by a series of hyphen-separated words, e.g."--file" or "--dry-run". These are the only two option syntaxes provided by optparse.

Some other option syntaxes that the world has seen include:

  • a hyphen followed by a few letters, e.g. "-pf" (this is not the same as multiple options merged into a single argument)
  • a hyphen followed by a whole word, e.g. "-file" (this is technically equivalent to the previous syntax, but they aren’t usually seen in the same program)
  • a plus sign followed by a single letter, or a few letters, or a word, e.g."+f", "+rgb"
  • a slash followed by a letter, or a few letters, or a word, e.g. "/f","/file"

These option syntaxes are not supported by optparse, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you’re exclusively targeting VMS, MS-DOS, and/or Windows.