PHP code example of mix / cli

1. Go to this page and download the library: Download mix/cli 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/ */

    

mix / cli example snippets


Mix\Cli\Cli::setName('app')->setVersion('0.0.0-alpha');
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo', 
    'run' => function () {
        $name = Mix\Cli\Flag::match('n', 'name')->string('default');
        // do something
    }
]);
$opt = new Mix\Cli\Option([
    'names' => ['n', 'name'],
    'usage' => 'Your name'
]);
$cmd->addOption($opt);
Mix\Cli\Cli::addCommand($cmd)->run();

class FooCommand implements Mix\Cli\RunInterface
{
    public function main(): void
    {
        // do something
    }
}
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo', 
    'run' => new FooCommand(),
]);

$name = Mix\Cli\Flag::match('n', 'name')->string('Xiao Ming');

$arg0 = Mix\Cli\Flag::arguments()->first()->string();

foreach (Mix\Cli\Flag::arguments()->values() as $k => $v) {
    // do something
}

if (Mix\Cli\Flag::match('d', 'daemon')->bool()) {
    \Swoole\Process::daemon();
}

$h = function ($next) {
    try {
        $next();
    } catch (\Throwable $ex) {
        // handle exception
        echo(sprintf("ERROR: %s\n", $ex->getMessage()));
    }
};
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo', 
    'run' => function () {
        // do something
    }
]);
Mix\Cli\Cli::use($h)->addCommand($cmd)->run();

$ php app.php
Usage: app.php [OPTIONS] COMMAND [ARG...]

Commands:
  hello         Echo demo

Global Options:
  -h, --help    Print usage
  -v, --version Print version information

Run 'app.php COMMAND --help' for more information on a command.

Developed with Mix PHP framework. (openmix.org/mix-php)

$ php app.php -v
app 0.0.0-alpha

$ php app.php hello --help
Usage: app.php hello [ARG...]

Command Options:
  -n, --name    Your name

Developed with Mix PHP framework. (openmix.org/mix-php)

$ php app.php hello

php /examples/app.php home -d -rf --debug -v vvv --page 23 -s=test --name=john arg0