PHP code example of asika / simple-console

1. Go to this page and download the library: Download asika/simple-console library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asika / simple-console example snippets

 php
#!/bin/sh php

// Include single file
endor/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;
});
 php
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();
 php
$arg = $this->getArgument(0);

if (!$arg)
{
    throw new \Asika\SimpleConsole\CommandArgsException('Please enter a name.');
}
 php
//...

    protected function doExecute()
    {
        return $this->delegate($this->getArgument(0));
    }

    protected function foo()
    {
        $this->getArgument(1); // bar
    }

    protected function baz()
    {
        // ...
    }
 bash
php console.php foo bar
php console.php baz
 php
$this->delegate(array_shift($this->args));
 php
$command = array_shift($this->args);

$this->delegate($command, ...$this->args);
 php
protected function foo($first, $second = null)
{
}
 php
$first = $this->getArgument(0, 'default value');
 php
$this->setArgument(1, 'value');
 php
$this->getOption('foo');
 php
$this->getOption(array('f', 'foo'));
 bash
$this->out('Hello')->out('World');
 bash
$this->err('Hello')->err('World');
 bash
$bool = $this->in('Are you sure? [Y/n]', [default true/false], true);