2017 © Pedro Peláez
 

library cmd

A simple command line option parser

image

enygma/cmd

A simple command line option parser

  • Monday, April 24, 2017
  • by enygma
  • Repository
  • 2 Watchers
  • 4 Stars
  • 220 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 17 % Grown

The README.md

Command Line Tools

Travis-CI Build Status, (*1)

This library provides some handy utilities for working on the command line:, (*2)

  • a simple interface for parsing the $_SERVER['argv'] input values out into key/value pairs (Command)
  • an output handling library making formatted output easy (Output)

Installation

Using Composer:, (*3)

composer require enygma/cmd

Example Usage:

execute($_SERVER['argv']);

echo 'RESULT: '.var_export($args, true)."\n";
?>

Command: Example

For the command line call of:, (*4)

php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s

The result would be:, (*5)

Array
(
    [0] => plain-arg
    [foo] => 1
    [bar] => baz
    [funny] => spam=eggs
    [also-funny] => spam=eggs
    [1] => plain arg 2
    [abc] => 1
    [k] => value
    [2] => plain arg 3
    [s] => 1
)

Options that are either plain arguments (like plain-arg or plain arg 3) will just be added to the results with numeric indexes. The other options will be assiged as key/value pairs in the resulting array. For values that are set (like -abc or --foo) but don't have a value, the value will be set to the boolean true., (*6)

Command: Required and default

You can also set up default values and required parameters for your command line options. Use the optional second paramster on the execute method call to define these:, (*7)

 ['foo' => true],
  'required' => ['bar']
];

$args = $cmd->execute($_SERVER['argv'], $config);

echo 'RESULT: '.var_export($args, true)."\n";

?>

The default values are defined as an array of parameter name => default value. The required settings are just presented as an array of option names., (*8)

Output: Example

success('Success message goes here!');
?>

Default methods:, (*9)

  • success
  • warning
  • info
  • error

You can also define a custom type if you want a reusable format:, (*10)

addType('custom1', 'white', 'blue');

$out->custom1('A custom message');
?>

The first parameter is the name of the custom format. The second and third are the foreground and background colors., (*11)

The Versions

24/04 2017

dev-master

9999999-dev https://github.com/enygma/cmd.git

A simple command line option parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

command parse argument

09/06 2016

0.4

0.4.0.0 https://github.com/enygma/cmd.git

A simple command line option parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

command parse argument

06/06 2016

0.3

0.3.0.0 https://github.com/enygma/cmd.git

A simple command line option parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

command parse argument

21/12 2015

0.2

0.2.0.0 https://github.com/enygma/cmd.git

A simple command line option parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

command parse argument

19/11 2015

0.1

0.1.0.0 https://github.com/enygma/cmd.git

A simple command line option parser

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

command parse argument