2017 © Pedro Peláez
 

library flags

GNU-style PHP command line argument parser

image

donatj/flags

GNU-style PHP command line argument parser

  • Wednesday, December 6, 2017
  • by donatj
  • Repository
  • 1 Watchers
  • 11 Stars
  • 35,811 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 2 Forks
  • 6 Open issues
  • 6 Versions
  • 6 % Grown

The README.md

Flags

Latest Stable Version Total Downloads License Build Status, (*1)

Flags is an argument parser inspired by the Go-lang Flag package, taking its methodology but attaching a GNU-style flag parser., (*2)


Flags supports the following style of parameters:, (*3)

Long-Flags
--key=value / --key value, (*4)

Short-Flags
-v, (*5)

GNU Style Multi-Short-Flags
-Xasd, (*6)

Multiple of the Same Short-Flag
-vvv, (*7)

As well as the  --  operator for absolute separation of arguments from options., (*8)

Requirements

  • php: >=5.3.0

Installing

Install the latest version with:, (*9)

composer require 'donatj/flags'

Example

Here is a simple example script:, (*10)

<?php

require __DIR__ . '/../vendor/autoload.php';

$flags = new donatj\Flags();

$foo     = & $flags->bool('foo', false, 'Enable the foo');
$bar     = & $flags->uint('bar', 10, 'Number of bars');
$baz     = & $flags->string('baz', 'default', 'What to name the baz');
$verbose = & $flags->short('v', 'verbosity');

/**
 * No Default value, making qux is *required*
 */
$qux = & $flags->bool('qux');

try {
    $flags->parse();
} catch( Exception $e ) {
    die($e->getMessage() . PHP_EOL . $flags->getDefaults() . PHP_EOL);
}

The by-reference = & allows the value to be updated from the default to the argument value once the parse() method has been triggered. This is inspired by the Go Flag packages use of pointers, (*11)

bash-3.2$ php example/example.php
Expected option --qux missing.
        -v   verbosity
     --foo   Enable the foo
     --bar   [uint] Number of bars
     --baz   [string] What to name the baz
     --qux   <bool>

Documentation

Class: \donatj\Flags

Method: Flags->__construct

function __construct([ array $args = null [, $skipFirstArgument = true]])

Flags constructor., (*12)

Parameters:
  • array $args - The arguments to parse, defaults to $_SERVER['argv']
  • bool $skipFirstArgument - Setting to false causes the first argument to be parsed as an parameter rather than the command.

Method: Flags->arg

function arg($index)

Returns the n'th command-line argument. arg(0) is the first remaining argument after flags have been processed., (*13)

Parameters:
  • int $index
Returns:
  • string

Method: Flags->args

function args()

Returns the non-flag command-line arguments., (*14)

Returns:
  • string[] - Array of argument strings

Method: Flags->shorts

function shorts()

Returns an array of short-flag call-counts indexed by character, (*15)

-v would set the 'v' index to 1, whereas -vvv will set the 'v' index to 3, (*16)

Returns:
  • array

Method: Flags->longs

function longs()

Returns an array of long-flag values indexed by flag name, (*17)

Returns:
  • array

Method: Flags->short

function short($letter [, $usage = ''])

Defines a short-flag of specified name, and usage string., (*18)

The return value is a reference to an integer variable that stores the number of times the short-flag was called., (*19)

This means the value of the reference for v would be the following., (*20)

-v => 1  
-vvv => 3
Parameters:
  • string $letter - The character of the short-flag to define
  • string $usage - The usage description
Returns:
  • int

Method: Flags->bool

function bool($name [, $value = null [, $usage = '']])

Defines a bool long-flag of specified name, default value, and usage string., (*21)

The return value is a reference to a variable that stores the value of the flag., (*22)

Examples
Truth-y
 --mybool=[true|t|1]  
 --mybool [true|t|1]  
 --mybool  
False-y
 --mybool=[false|f|0]  
 --mybool [false|f|0]  
   [not calling --mybool and having the default false]
Parameters:
  • string $name - The name of the long-flag to define
  • mixed $value - The default value - usually false for bool - which if null marks the flag required
  • string $usage - The usage description
Returns:
  • mixed - A reference to the flags value

Method: Flags->float

function float($name [, $value = null [, $usage = '']])

Defines a float long-flag of specified name, default value, and usage string., (*23)

The return value is a reference to a variable that stores the value of the flag., (*24)

Examples
--myfloat=1.1  
--myfloat 1.1
Parameters:
  • string $name - The name of the long-flag to define
  • mixed $value - The default value which if null marks the flag required
  • string $usage - The usage description
Returns:
  • mixed - A reference to the flags value

Method: Flags->int

function int($name [, $value = null [, $usage = '']])

Defines an integer long-flag of specified name, default value, and usage string., (*25)

The return value is a reference to a variable that stores the value of the flag., (*26)

Note: Float values trigger an error, rather than casting., (*27)

Examples
--myinteger=1  
--myinteger 1
Parameters:
  • string $name - The name of the long-flag to define
  • mixed $value - The default value which if null marks the flag required
  • string $usage - The usage description
Returns:
  • mixed - A reference to the flags value

Method: Flags->uint

function uint($name [, $value = null [, $usage = '']])

Defines a unsigned integer long-flag of specified name, default value, and usage string., (*28)

The return value is a reference to a variable that stores the value of the flag., (*29)

Note: Negative values trigger an error, rather than casting., (*30)

Examples
--myinteger=1  
--myinteger 1
Parameters:
  • string $name - The name of the long-flag to define
  • mixed $value - The default value which if null marks the flag required
  • string $usage - The usage description
Returns:
  • mixed - A reference to the flags value

Method: Flags->string

function string($name [, $value = null [, $usage = '']])

Defines a string long-flag of specified name, default value, and usage string., (*31)

The return value is a reference to a variable that stores the value of the flag., (*32)

Examples, (*33)

--mystring=vermouth  
--mystring="blind jazz singers"  
--mystring vermouth  
--mystring "blind jazz singers"
Parameters:
  • string $name - The name of the long-flag to define
  • mixed $value - The default value which if null marks the flag required
  • string $usage - The usage description
Returns:
  • mixed - A reference to the flags value

Method: Flags->getDefaults

function getDefaults()

Returns the default values of all defined command-line flags as a formatted string., (*34)

Example
           -v   Output in verbose mode  
  --testsuite   [string] Which test suite to run.  
  --bootstrap   [string] A "bootstrap" PHP file that is run before the specs.  
       --help   Display this help message.  
    --version   Display this applications version.  
Returns:
  • string

Method: Flags->parse

function parse([ array $args = null [, $ignoreExceptions = false [, $skipFirstArgument = null]]])

Parses flag definitions from the argument list, which should include the command name., (*35)

Must be called after all flags are defined and before flags are accessed by the program., (*36)

Will throw exceptions on Missing Require Flags, Unknown Flags or Incorrect Flag Types, (*37)

Parameters:
  • array $args - The arguments to parse. Defaults to arguments defined in the constructor.
  • bool $ignoreExceptions - Setting to true causes parsing to continue even after an exception has been thrown.
  • bool $skipFirstArgument - Option to parse the first argument as an parameter rather than the command. Defaults to constructor value

Method: Flags->parsed

function parsed()

Returns true if the command-line flags have been parsed., (*38)

Returns:
  • bool

The Versions

06/12 2017

dev-master

9999999-dev

GNU-style PHP command line argument parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments gnu-style

16/01 2017

v1.2.0

1.2.0.0

GNU-style PHP command line argument parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments gnu-style

09/05 2014

v1.1.0

1.1.0.0

GNU-style PHP command line argument parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments gnu-style

19/11 2013

v1.0.2

1.0.2.0

Go-lang `Flags` inspired command-line flag/option parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments

07/10 2013

v1.0.1

1.0.1.0

Go-lang `Flags` inspired command-line flag/option parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments

05/10 2013

v1

1.0.0.0

Go-lang `Flags` inspired command-line flag/option parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cli flags options arguments