Last Updated: February 25, 2016
· nategood

Simple PHP CLI Apps

Commando is a PHP command line interface library that beautifies and simplifies writing PHP scripts intended for command line use.

Supports built in help generation, clean option parsing, validation, data mapping, and more. Here is an example that covers many of the the things you can do with Commando...


$hello_cmd = new Command();

// Define first option
    ->describedAs('A person\'s name');

// Define a flag "-t" a.k.a. "--title"
    ->describedAs('When set, use this title to address the person')
    ->must(function($title) {
        $titles = array('Mr','Mrs','Ms');
        return in_array($title, $titles);
    ->map(function($title) {
        $titles = array(
          'Mister' => 'Mr', 
          'Misses' => 'Mrs', 
          'Miss' => 'Ms'
        if (array_key_exists($title, $titles))
            $title = $titles[$title];
        return "$title. ";

// Define a boolean flag "-c" aka "--capitalize"
    ->describedAs('Always capitalize the words in a name')

$name = $hello_cmd['capitalize'] ? 
    ucwords($hello_cmd[0]) : $hello_cmd[0];

echo "Hello {$hello_cmd['title']}$name!", PHP_EOL;

2 Responses
Add your response

The worst DSL style I have seen.

over 1 year ago ·

That's constructive. Can you elaborate? It wasn't intended to be a DSL. It is simply a chaining design pattern. Do you consider jQuery a DSL?

over 1 year ago ·