Single file console framework to help you write build scripts.
Use composer:
composer require asika/simple-consoleDownload single file: Download Here
CLI quick download:
# WGET wget https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php chmod +x Console.php # CURL curl https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php -o Console.php chmod +x Console.phpUse closure
#!/bin/sh php <?php // Include single file include_once __DIR__ . '/Console.php'; // Or use composer include_once __DIR__ . '/vendor/autolod.php'; $app = new \Asika\SimpleConsole\Console; // Use closure $app->execute(function (\Asika\SimpleConsole\Console $app) { // PHP 5.3 $app->out('Hello'); // PHP 5.4 or higher use $this $this->out('Hello'); // Return TRUE will auto convert to 0 exitcode. return true; });Or Create your own class.
class Build extends \Asika\SimpleConsole\Console { protected $help = <<<HELP [Usage] php build.php <version> [Options] h | help Show help information v Show more debug information. HELP; protected function doExecute () { $this->out('Hello'); // Return TRUE will auto convert to 0 exitcode. return true; } } $app = new Build; $app->execute();Add -h or --help to show usage, you can add custom usage to $this->help, or override $this->getHelp().
If you want to change h and help option, override $this->helpOptions = array('...').
Just throw Exception in doExecute(), Console will auto catch error.
throw new \RuntimeException('...');Add -v to show backtrace if error.
Wrong Argument use \Asika\SimpleConsole\CommandArgsException
$arg = $this->getArgument(0); if (!$arg) { throw new \Asika\SimpleConsole\CommandArgsException('Please enter a name.'); }Console will auto show help information.
[Warning] Please enter a name. [Usage] console.php <name> [Options] h | help Show help info. v Show more debug information.Use delegate() to delegate to different methods.
//... protected function doExecute() { return $this->delegate($this->getArgument(0)); } protected function foo() { $this->getArgument(1); // bar } protected function baz() { // ... }Now you can add sub commands
php console.php foo bar php console.php bazIf you want to strip first argument after delgated, you can follow this code:
$this->delegate(array_shift($this->args));Now can use getArgument(0) in sub method and ignore the first command name.
The is another way:
$command = array_shift($this->args); $this->delegate($command, ...$this->args);protected function foo($first, $second = null) { }$first = $this->getArgument(0, 'default value');$this->setArgument(1, 'value');Get option --foo
$this->getOption('foo');Get option -f or --foo, first match will return.
$this->getOption(array('f', 'foo'));NOTE:
-abcwill convert toa => 1, b => 1, c => 1And-vvvwill convert tov => 3
Set otpion to toption list. $name also support array.
Write to STDOUT,
$this->out('Hello')->out('World');Write to STDERR
$this->err('Hello')->err('World');Ask a question, read from STDIN
$un = $this->in('Please enter username: ', 'default_name');Read as boolean, add true to third argument:
$bool = $this->in('Are you sure? [Y/n]', [default true/false], true);yes, y, 1, truewill convert toTRUEno, n, 0, falsewill convert toFALSE
A proxy to execute a cmd by exec() and return value.
It will add a title >> {your command} before exec so you will know what has been executed.