PHP code example of meritum / cli

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

    

meritum / cli example snippets


#!/usr/bin/env php


ronment;
use Meritum\Cli\CliKernel;

$kernel = new CliKernel(Environment::Production);

// register modules and commands here

$kernel->boot();

exit($kernel->run());

$kernel = new CliKernel(Environment::Production);
$kernel->setName('myapp');
$kernel->setVersion('1.0.0');

use Meritum\Cli\CliKernelOption;

$kernel->define(CreateUserCommand::class, function ($c) {
    $command = new CreateUserCommand($c->get(UserRepository::class));

    $command->setName('user:create')
            ->setDescription('Create a new user')
            ->addArgument('email')->description('The user email address')
            ->addOption('admin')->description('Grant admin privileges');

    return $command;
})->tag(CliKernelOption::CommandTag->value);

use Meritum\Cli\Command\Command;
use Meritum\Cli\ExitCode;
use Meritum\Cli\Output\SageStyleInterface;

final class CreateUserCommand extends Command
{
    public function __construct(private readonly UserRepository $users) {}

    public function __invoke(SageStyleInterface $io): ExitCode
    {
        $email = $io->argument('email');

        $this->users->create($email);

        $io->success("User {$email} created.");

        return ExitCode::Success;
    }
}

// Get a single argument (returns null if not provided, or $default if given)
$io->argument('name');
$io->argument('name', 'fallback');

// Get all arguments as an associative array
$io->arguments();

// Get a single option (returns null if not provided, or $default if given)
$io->option('format');
$io->option('format', 'text');

// Get all options as an associative array
$io->options();

$command->addArgument('name');                          //  optional
$command->addArgument('name')->optional('default');     // optional with default
$command->addArgument('tags')->array();                 // ll completion hints

$command->addOption('force');                           // boolean flag (--force)
$command->addOption('output')->)->  ->shortcut('o')                                 // -o shortcut
        ->description('Output path')
        ->suggest('/tmp', '/var/log');                  // shell completion hints

public function __invoke(SageStyleInterface $io): ExitCode
{
    $this->call('cache:clear', $io);

    $this->call('cache:warm', $io, ['--env' => 'prod']);

    return ExitCode::Success;
}

$command = $kernel->find('cache:clear');

use Meritum\Cli\Exception\ExceptionHandlerInterface;
use Meritum\Cli\Output\SageStyleInterface;

final class ConsoleExceptionHandler implements ExceptionHandlerInterface
{
    public function handle(\Throwable $e, SageStyleInterface $io): void
    {
        $io->error($e->getMessage());
    }
}

$kernel->define(ExceptionHandlerInterface::class, fn () => new ConsoleExceptionHandler())->share();

$kernel->onBooting(function ($kernel) { /* before boot */ });
$kernel->onBooted(function ($kernel)  { /* after boot  */ });
$kernel->onShutdown(function ($kernel) { /* before shutdown */ });

use Georgeff\Kernel\KernelInterface;
use Georgeff\Kernel\Module\ModuleInterface;
use Meritum\Cli\CliKernelOption;

final class MigrationsModule implements ModuleInterface
{
    public function register(KernelInterface $kernel): void
    {
        $kernel->define(MigrateCommand::class, fn ($c) => (new MigrateCommand(
            $c->get(Database::class)
        ))->setName('migrate')->setDescription('Run pending migrations'))
        ->tag(CliKernelOption::CommandTag->value);
    }
}

$kernel->addModule(new MigrationsModule());