Home » Php » parsing – Is there a complete command-line parser for PHP?

parsing – Is there a complete command-line parser for PHP?

Posted by: admin July 12, 2020 Leave a comment


I am looking for command parsing library similar to Thor, but for PHP.

More specifically, I would like something that can…

  • handle subcommands input, e.g. git remote add <url>
  • associate options with their values, e.g. my-command --some-option='value' creates a key-value pair like array( 'some-option' => 'value' )
  • generates the Usage/Help info from the configuration passed to it
  • processes -short and –long options
  • the library is being maintained
  • has to be in PHP (otherwise, I’d use Thor)

None of the solutions that I have looked at so far has been satisfactory.

The closest thing to hitting the mark is PEAR’s Console_Getargs, though this is not being maintained.

The pear command uses Console_Getopt, though they perform a significant amount of manual processing to achieve the features in my first three requirements.

PHP’s getopt() cannot handle subcommands, and there do not seem to be any other built-in methods that approximate this functionality.

Is there a PHP library that fits these criteria that I may have missed?

How to&Answers:

You could try Zend_Console_Getopt or the Console component of Symfony 2


GetOptionKit parses arguments, handles subcommands, automatically generates help, and has a nice API. It requires PHP 5.4+. It is hosted on GitHub, and seems to be activly maintained.

For boolean options:

-d   => ["debug" => true]
--debug => ["debug" => true]

For incremental options:

-v => [ "verbose" => 1 ]
-vv => [ "verbose" => 2 ]
-vvv => [ "verbose" => 3 ]

For multiple value options:

--foo=bar   => [ "foo" => "bar" ]
--foo=bar --foo=zoo => [ "foo" => ["bar", "zoo"] ]

There is also a type constraint option that helps you get option value like this:

--output=file  => [ "output" => SplFileInfo Object ]


I’m using this https://gist.github.com/2959619 with quite a success for a while.
It’s short (~30 lines), and support both subcommands, named short and long arguments.

When using in a larger project, I would recommend the component Symfony Console. Its capabilities goes beyond command line parsing, but it’s great.


Getopt.php meets your needs.

In addition, its true object oriented interface helps you write elegant PHP code.

It is well documented and it does not have any external dependencies.


  • Supports both short (eg. -v) and long (eg. --version) options
  • Option aliasing, ie. an option can have both a long and a short version
  • Collapsed short options (eg. -abc instead of -a -b -c)
  • Cumulative options (eg. -vvv)
  • Options may take optional or mandatory arguments
  • Two alternative notations for long options with arguments: --option value and --option=value
  • Collapsed short options with mandatory argument at the end (eg. -ab 1 instead of -a -b 1)

Example Code


use Ulrichsg\Getopt\Getopt;
use Ulrichsg\Getopt\Option;

$getopt = new Getopt(array(
    new Option('m', 'mode', Getopt::REQUIRED_ARGUMENT),
    new Option('p', 'parents'),
    new Option('v', 'verbose'),
    new Option('Z', 'context', Getopt::REQUIRED_ARGUMENT),
    new Option(null, 'help'),
    new Option(null, 'version')

try {

    if ($getopt['version']) {
        echo "Getopt example v0.0.1\n";

    // Error handling and --help functionality omitted for brevity

    $createParents = ($getopt['parents'] > 0);
    // Note that these are null if the respective options are not given
    $mode = $getopt['mode'];
    $context = $getopt['context'];

    $dirNames = $getopt->getOperands();

    makeDirectories($dirNames, $createParents, $mode, $context);
} catch (UnexpectedValueException $e) {
    echo "Error: ".$e->getMessage()."\n";
    echo $getopt->getHelpText();


If somone is looking for a framework independent solution with the following features:

  • Simple definition syntax
  • Supports long and short arugment names
  • Supports repeated arguments (e.g. -v -v -v)
  • Supports multiple short arguments (e.g. -abc vs -a -b -c)
  • Supports 2 ways of setting values (e.g. –name=abc or –name abc)
  • Supports double dash delimiter for trailing values
  • builds usage definition

I would suggest this product https://github.com/webtrendi/clapp