2017 © Pedro Peláez
 

library cli-parser

A command line parser utility for PHP scripts.

image

vtardia/cli-parser

A command line parser utility for PHP scripts.

  • Thursday, March 26, 2015
  • by vtardia
  • Repository
  • 1 Watchers
  • 1 Stars
  • 306 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

CLIParser

CLIParser is a PHP utility that parses the command line of a PHP script detecting short and long options, switches and arguments., (*1)

The output is similar to Linux's getopt_long() native function in both behavior and configuration, both long and short options can be validated, for example:, (*2)

$shortOptions = 'vho:a';

$longOptions = array(
    array('id', TRUE),
    array('name', TRUE),
    array('verbose', FALSE, 'v'),
    array('output', TRUE, 'o'),
);

The above code defines, (*3)

  • the long option --verbose with short equivalent -v and no required parameter
  • the long option --output, with short equivalent -o and a required parameter
  • the long options --id and --name with required parameters and no short equivalent
  • the short options -h and -a with no long equivalent and no required parameter

With CLIParser you can build scripts like:, (*4)

$ myscript -abc -v -o somefile.log --name=SomeName --other-option OtherValue Argument1 Argument2...ArgumentN

In addition, you can parse the arguments starting from an arbitrary position, so you can have:, (*5)

$ myscript SomeCommand [OPTIONS] Argument1...ArgumentN

Usage

// require CLIParser.php library file or use an autoloader (eg. Composer)

use CLIParser\CliParser;

// Define some options
$shortOptions = 'vo:';
$longOptions = array(
    array('verbose', false, 'v'),
    array('output', true, 'o'),
);

// Create a parser...
$cli = new CLIParser($shortOptions, $longOptions);

// ... and use it!

// Executable name or path
$program = $cli->program();

// Array of valid options with values
$options = $cli->options();

// Array of arguments
$arguments = $cli->arguments();

Installation

$ composer require vtardia/cli-parser

Requirements

PHP 5.3 or better, PHPUnit in order to run tests., (*6)

Contributing

See CONTRIBUTING file., (*7)

Running the Tests

$ composer test

Credits

CLI Parser uses some code written by Patrick Fisher., (*8)

Contributor Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See CODE_OF_CONDUCT file., (*9)

License

CLIParser is released under the MIT License. See the bundled LICENSE file for details., (*10)

The Versions

26/03 2015

dev-master

9999999-dev https://github.com/vtardia/cli-parser

A command line parser utility for PHP scripts.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

console commands cli shell

26/03 2015

1.0.0

1.0.0.0 https://github.com/vtardia/cli-parser

A command line parser utility for PHP scripts.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

console commands cli shell