PHP code example of lukman-ss / console

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

    

lukman-ss / console example snippets




declare(strict_types=1);

 Lukman\Console\ConsoleApplication;
use Lukman\Console\Input;
use Lukman\Console\Output;

$app = new ConsoleApplication('My CLI', '1.0.0');

$app->command('greet', function (Input $input, Output $output): int {
    $name = $input->argument(0, 'Guest');
    $output->success('Hello ' . $name);

    return Command::SUCCESS;
}, 'Greet a user');

$exitCode = $app->run();



declare(strict_types=1);

use Lukman\Console\Command;
use Lukman\Console\Input;
use Lukman\Console\Output;

final class GreetCommand extends Command
{
    protected string $name = 'greet';
    protected string $description = 'Greet a user';
    protected string $signature = 'greet {name=Guest} {--yell}';

    public function handle(Input $input, Output $output): int
    {
        $message = 'Hello ' . $input->argument(0, 'Guest');

        if ($input->option('yell', false) === true) {
            $message = strtoupper($message);
        }

        $output->writeln($message);

        return self::SUCCESS;
    }
}

$app->add(new GreetCommand());

$stdout = fopen('php://memory', 'r+');
$stderr = fopen('php://memory', 'r+');

$output = new Output($stdout, $stderr, decorated: false);
$output->writeln('ok');
$output->errorLine('error');

use Lukman\Console\Testing\CommandTester;

$tester = new CommandTester($app);
$exitCode = $tester->run(['greet', 'Lukman']);

$stdout = $tester->output();
$stderr = $tester->errorOutput();
bash
php bin/console list
php bin/console help greet