Is there a complete command line parser for PHP?

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

In particular, I would like something that can ...

  • entering input subcommands, for example. git remote add <url>
  • associate options with your values, for example. my-command --some-option='value' creates a key value pair, for example array( 'some-option' => 'value' )
  • generates usage / assistance information from the configuration passed to it
  • -short and -long options processes
  • library supported
  • should be in PHP (otherwise I would use Thor)

None of the solutions that I have considered so far have been satisfactory.

The closest to label hit is PEAR Console_Getargs , although this is not supported.

The pear team uses Console_Getopt , although they do significant manual processing to achieve the capabilities in my first three requirements.

PHP getopt() cannot process subcommands, and there seems to be no other built-in methods that approximate this functionality.

Is there a PHP library that meets these criteria that I might have missed?

+7
source share
5 answers
+6
source

GetOptionKit parses arguments, processes subcommands, automatically generates help, and has a good API. This requires PHP 5.4+. It is hosted on GitHub and seems to be actively supported.

For logical options:

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

For additional parameters:

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

For several value options:

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

There is also a type constraint parameter that helps you get the value of the parameter as follows:

 --output=file => [ "output" => SplFileInfo Object ] 
+7
source

I use this https://gist.github.com/2959619 with pretty much success. It is short (~ 30 lines) and supports both subcommands with short and long arguments.

When used in a larger project, I would recommend the Symfony Console component. Its capabilities are superior to command line parsing, but that's great.

+3
source

Getopt.php meets your needs.

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

This is well documented and it has no external dependencies.

Functions

  • Supports both short (e.g. -v ) and long (e.g. --version ) options
  • Aliasing option, i.e. option can have both long and short version
  • Collapse brief parameters (e.g. -abc instead of -a -b -c )
  • Aggregate options (e.g. -vvv )
  • Parameters may take optional or required arguments
  • Two alternative notations for long options with arguments: --option value and --option=value
  • Collapse brief parameters with a required argument at the end (e.g. -ab 1 instead of -a -b 1 )

Code example

 <?php 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 { $getopt->parse(); if ($getopt['version']) { echo "Getopt example v0.0.1\n"; exit(0); } // 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(); exit(1); } 
+3
source

If somone is looking for a structure-independent solution with the following functions:

  • Simple Definition Syntax
  • Supports long and short names for advertising
  • Supports duplicate arguments (e.g. -v -v -v)
  • Supports a few short arguments (e.g. -abc vs -a -b -c)
  • Supports two ways of setting values ​​(e.g. --name = abc or --name abc)
  • Supports double dash delimiter for final values
  • establishes a definition of use

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

+2
source

All Articles