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;
Written by Nate Good
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#