Last Updated: February 25, 2016
·
4.101K
· nategood

Simple PHP CLI Apps

https://github.com/nategood/commando

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...

<?php

$hello_cmd = new Command();

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

// Define a flag "-t" a.k.a. "--title"
$hello_cmd->option('t')
    ->aka('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"
$hello_cmd->option('c')
    ->aka('capitalize')
    ->aka('cap')
    ->describedAs('Always capitalize the words in a name')
    ->boolean();

$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 ·