In my current work I have to occasionally work with PHP code. I don't really
like PHP, for a variety of otiose reasons. But one of the things that
surprised me was that it didn't have an equivalent to Perl's
There are a couple of other modules that are in PHP's PEAR package repository
which attempt to handle more than PHP's built-in getopt function, but all of
these lack a couple of fundamental features:
- I want to be able to pass a single description - e.g. 'verbose|v' - and
have the function recognise both as synonyms for the same setting.
- I want to be able to pass a variable reference and have that updated
directly if the associated command line parameter is supplied.
- I want to have it remove all the processed arguments off the command line
so that all that is left is the array of things that weren't parameters or
- I want a single, single call, rather than calling object methods for each
(To be clear: some of the PEAR modules provide some of these. But all of them
lack goal 2, most lack goal 3, and while are able to achieve goal 1 it's only
by lots of extra code or option specification.)
So I wrote one.
The result is available from my nascent PHP Subversion library at:
It's released under version 3 of the GPL. It also comes with a simple
test framework (written, naturally, in a clearly superior language: Perl).
This is still a work in progress, and there are a number of features I want
to add to it - chief amongst them packaging it for use in PEAR. I'm not a
PHP hacker, and it still astonishes me that PHP programmers have been content
to use the mish-mash of different half-concocted options for command line
processing when something clearly better exists - and that many of the PHP
programs I have to work with don't use any of those but write their own
minimal, failure-prone and ugly command line processing from scratch.
I'd love to hear from people with
patches, suggestions or comments. If you want write access to the repository,
let me know as well.