2017 © Pedro Peláez
 

library cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

image

deweller/cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

  • Thursday, February 28, 2013
  • by dweller
  • Repository
  • 2 Watchers
  • 15 Stars
  • 5,940 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 2 % Grown

The README.md

php-cliopts Build Status project status

A no-nonsense command-line options parser and help generator for PHP CLI apps., (*1)

Features

  • Simple one-line usage with a human readable configuration format
  • Parses $argv data into an associative array similar to getopt()
  • Adds error validation for missing or malformed arguments or options
  • Nicely formatted help generation
  • Supports options (e.g. -i 100) and named arguments (./script.php /tmp/myfile.txt)

Usage

Code

In its simplest form, the parser can be used with one line of php code:, (*2)


// specify the spec as human readable text $values = CLIOpts\CLIOpts::run(" {self} <in_filename> -i, --id <id> specify an id (required) -o, --out <out_filename> output filename -v be verbose -h, --help show this help "); // show the values echo "The values you supplied are:\n"; print_r((array)$values);

CLI Input

The interpretation of flags are somewhat flexible. The following lines are all handled in the same way by cliopts:, (*3)

  1. ./script.php -v -i 101 -o /tmp/myfile.txt /tmp/infile.txt, (*4)

  2. ./script.php -vi 101 -o /tmp/myfile.txt /tmp/infile.txt, (*5)

  3. ./script.php -v --id 101 -o /tmp/myfile.txt /tmp/infile.txt, (*6)

  4. ./script.php -v --id="101" -o /tmp/myfile.txt /tmp/infile.txt, (*7)

All of the above will show this output:, (*8)

The values you supplied are:        
Array                               
(                                   
    [in_filename] => /tmp/infile.txt
    [id] => 101
    [out] => /tmp/myfile.txt
    [v] =>
)                                   

The Human Readable Text Specification

Let's look at the specification in the following bit of code:, (*9)

$values = CLIOpts\CLIOpts::run("
  Usage: process_files.php [options] <in_file1> [<in_file2>]
  -i, --id <id> specify an id (required)
  -v be verbose
")

The Usage Line

The spec begins with a usage line. This line is optional. But if it is provided, here is a breakdown of how the usage line is interpreted:, (*10)

Usage: process_files.php [options] <in_file1> [<in_file2>]
|      |                 |         |          |
|      |                 |         |          + Optional second argument named in_file2.
|      |                 |         |
|      |                 |         + Required first argument named in_file1
|      |                 |          
|      |                 + An options placeholder.  This may be ommitted.  It must come before any arguments.
|      |
|      + An optional script name.  Omit this or use {self} to show $_SERVER['argv'][0].
|
+ The usage keyword.  This may be ommitted.

In this example, 1 argument is expected and the value provided will be assigned to the key "in_file1" in the values object. An optional second argument will be assed to the key "in_file2" if it is provided. And that's all. If a 3rd argument is provided it will not be assigned to a value and validation will fail., (*11)

The Option Lines

Here is how the first option line is interpreted:, (*12)

-i, --id <id> specify an id (required)
|   |    |    |             |
|   |    |    |             + This makes the option required when validating.
|   |    |    |
|   |    |    + Help text.  This can be any text.
|   |    |
|   |    + This specifies that the option requires a value.  Unlike arguments, this is not used for the value name.
|   |
|   + This is a long option name.  It is not required.  If specified, this is used for the value name when arguments are parsed.
|
+ This is the short option name.  It is not required.  Values can be accessed using this shortcut.

The Versions

28/02 2013

dev-master

9999999-dev https://github.com/deweller/php-cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

command command-line cli getopt option line help opt

11/01 2013

1.0.2

1.0.2.0 https://github.com/deweller/php-cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

command command-line cli getopt option line help opt

03/01 2013

1.0.1

1.0.1.0 https://github.com/deweller/php-cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

command command-line cli getopt option line help opt

01/12 2012

1.0.0

1.0.0.0 https://github.com/deweller/php-cliopts

A no-nonsense command-line options parser and help generator for PHP CLI apps.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

command command-line cli getopt option line help opt