ConsoleKit
PHP 5.3+ library to create command line utilities., (*1)
, (*2)
Example
In cli.php:, (*3)
<?php
class HelloCommand extends ConsoleKit\Command
{
public function execute(array $args, array $options = array())
{
$this->writeln('hello world!', ConsoleKit\Colors::GREEN);
}
}
$console = new ConsoleKit\Console();
$console->addCommand('HelloCommand');
$console->run();
In the shell:, (*4)
$ php cli.php hello
hello world!
More examples in example.php, (*5)
Installation
The easiest way to install ConsoleKit is using Composer
with the following requirement:, (*6)
{
"require": {
"maximebf/consolekit": ">=1.0.0"
}
}
Alternatively, you can download the archive
and add the src/ folder to PHP's include path:, (*7)
set_include_path('/path/to/src' . PATH_SEPARATOR . get_include_path());
ConsoleKit does not provide an autoloader but follows the PSR-0 convention.
You can use the following snippet to autoload ConsoleKit classes:, (*8)
spl_autoload_register(function($className) {
if (substr($className, 0, 10) === 'ConsoleKit') {
$filename = str_replace('\\', DIRECTORY_SEPARATOR, trim($className, '\\')) . '.php';
require_once $filename;
}
});
Usage
Options parser
The default options parser parses an argv-like array.
Items can be of the form:, (*9)
- --key=value
- --key
- -a
- -ab (equivalent of -a -b)
When an option has no value, true will be used. If multiple key/value pairs
with the same key are specified, the "key" value will be an array containing all the values.
If "--" is detected, all folowing values will be treated as a single argument, (*10)
Example: the string "-a -bc --longopt --key=value arg1 arg2 -- --any text" will produce the following two arrays:, (*11)
$args = array('arg1', 'arg2', '--any text');
$options = array('a' => true, 'b' => true, 'c' => true, 'longopt' => true, 'key' => 'value');
Creating commands
Any callbacks can be a command. It will receive three parameters: the
arguments array, the options array and the console object., (*12)
function my_command($args, $opts, $console) {
$console->writeln("hello world!");
}
Commands can also be defined as classes. In this case, they must inherit from ConsoleKit\Command
and override the execute()
method., (*13)
class MyCommand extends ConsoleKit\Command {
public function execute(array $args, array $opts) {
$this->writeln("hello world!");
}
}
The ConsoleKit\Command
class offers helper methods, check it out for more info., (*14)
Registering commands
Commands need to be registered in the console object using the addCommand()
method (or addCommands()
)., (*15)
$console = new ConsoleKit\Console();
$console->addCommand('my_command'); // the my_command function
$console->addCommand('MyCommand'); // the MyCommand class
$console->addCommand(function() { echo 'hello!'; }, 'hello'); // using a closure
// or:
$console->addCommand('hello', function() { echo 'hello!'; }); // alternative when using a closure
Notice that in the last example we have provided a second argument which is an alias for a command.
As closures have no name, one must be specified., (*16)
The command name for functions is the same as the function name with underscores replaced
by dashes (ie. my_command becomes my-command)., (*17)
The command name for command classes is the short class name without the Command
suffix and "dashized" (ie. HelloWorldCommand becomes hello-world)., (*18)
Running
Simply call the run()
method of the console object, (*19)
$console->run();
$console->run(array('custom arg1', 'custom arg2')); // overrides $_SERVER['argv']
Automatic help generation
The help command is automatically registered and provides help about available methods based on doc comments.
Check out example.php for example of available tags, (*20)
$ php myscript.php help
Formating text
Colors
The ConsoleKit\Colors::colorize()
method provides an easy way to colorize a text.
Colors are defined as either a string or an integer (through constants of the Colors
class).
Available colors: black, red, green, yellow, blue, magenta, cyan, white., (*21)
Foreground colors are also available in a "bold" variant. Suffix the color name with "+bold" or use the OR bit operator with constants., (*22)
echo Colors::colorize('my red text', Colors::RED);
echo Colors::colorize('my red text', 'red');
echo Colors::colorize('my red bold text', Colors::RED | Colors::BOLD);
echo Colors::colorize('my red bold text', 'red+bold');
echo Colors::colorize('my red text over yellow background', Colors::RED, Colors::YELLOW);
TextFormater
The ConsoleKit\TextFormater
class allows you to format text using the following options:, (*23)
- indentation using
setIndent()
or the indent option
- quoting using
setQuote()
or the quote option
- foreground color using
setFgColor()
or the fgcolor option
- background color using
setBgColor()
or the bgcolor option
Options can be defined using setOptions()
or as the first parameter of the constructor., (*24)
$formater = new ConsoleKit\TextFormater(array('quote' => ' > '));
echo $formater->format("hello!");
// produces: " > hello"
Dialog
Used to interact with the user, (*25)
$dialog = new ConsoleKit\Widgets\Dialog($console);
$name = $dialog->ask('What is your name?');
if ($dialog->confirm('Are you sure?')) {
$console->writeln("hello $name");
}
Box
Wraps text in a box, (*26)
$box = new ConsoleKit\Widgets\Box($console, 'my text');
$box->write();
Produces:, (*27)
********************************************
* my text *
********************************************
Progress bar
Displays a progress bar, (*28)
$total = 100;
$progress = new ConsoleKit\Widgets\ProgressBar($console, $total);
for ($i = 0; $i < $total; $i++) {
$progress->incr();
usleep(10000);
}
$progress->stop();