PHP command line interface(cli), argv parser


Keywords
php, cli, parser, color, argv, argv-parser, color-cli
License
MIT

Documentation

php-cli

PHP Support PHP Support License Travis

Introduction

php-cli is a command line argv parser for PHP.

Installation

composer require aphp/cli ~1.1.0

or version 1.0.x

composer require aphp/cli ~1.0.0

Features

Install bin autoload to third party package

at root package dir

# bash
cp vendor/aphp/cli/example/__autoload.php bin/__autoload.php
cp vendor/aphp/cli/example/bin_example.php bin/bin_example.php
# win
copy vendor\aphp\cli\example\__autoload.php bin\__autoload.php /Y
copy vendor\aphp\cli\example\bin_example.php bin\bin_example.php /Y

Examples

Argv syntax

Flag - one or many symbols in argument.

-%symbol%%symbol%...
-f -z -rddf -wdwdwd

$app->argv->flags = [f, z, rddf, wdwdwd]

Named value - name and value

--%key% %value% --%key% %value% ...
--name0 value --name1 value

$app->argv->namedParams = [name0 => value, name1 => value]

--name0 --name1 --name3 --name4
$app->argv->namedParams = [name0 => StdClass, name1 => StdClass, ...]

Named array - name and value, same names

--key %value% --key %value% ...
--key value1 --key value2

$app->argv->namedParams = [key => [ value1, value2 ]]

Unamed values

value value value

$app->argv->unamedParams = [value, value, value]

EchoFn

class EchoFn
{
	static $line = '-';
	static $lineSize = 60;
	static $tab = '  ';
	/** @var Style[] */
	static $styles = [];
	/** @var Style */
	static $selectedStyle;

	static function addStyle($name, Style $style);
	static function useStyle($name);
	static function resetStyle();
	static function emptyLine();
	static function line();
	static function setTabsBefore($count);
	static function getTabsBefore();
	static function echoWithTabs($text);
	static function echoException(\Exception $e);
}

See echo_color.php

Colors

CMD
Before use the colors - read the color table

class Color {
static $textColors = [
	'default'       => 39,
	'black'         => 30,
	'red'           => 31,
	'green'         => 32,
	'yellow'        => 33,
	'blue'          => 34,
	'magenta'       => 35,
	'cyan'          => 36,
	'light_gray'    => 37,
	'dark_gray'     => 90,
	'light_red'     => 91,
	'light_green'   => 92,
	'light_yellow'  => 93,
	'light_blue'    => 94,
	'light_magenta' => 95,
	'light_cyan'    => 96,
	'white'         => 97,
]; }

Configure styles

EchoFn::addStyle('name', new Style('light_yellow', 'black');
EchoFn::useStyle('name');
EchoFn::echoWithTabs('string 777');

See

Test running

  • install phpunit, composer, php if not installed
  • run composer install at package dir
  • run composer run-script test
🔵 Useful links

More features

For more features:

  • read source code and examples
  • practice with cli in real code