PHP code example of elliephp / console

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

    

elliephp / console example snippets


#!/usr/bin/env php


lication;

$app = new Application();
$app->add(new YourCommand());
$app->run();

#!/usr/bin/env php


lication;

$container = 0.0');
$app->addCommands([
    YourCommand::class, // Resolved from container
]);
$app->run();



namespace App\Console;

use ElliePHP\Console\Command\BaseCommand;
use Symfony\Component\Console\Input\InputArgument;

class GreetCommand extends BaseCommand
{
    protected function configure(): void
    {
        $this->setName('greet')
             ->setDescription('Greet someone')
             ->addArgument('name', InputArgument::OPTIONAL, 'Name to greet', 'World');
    }

    protected function handle(): int
    {
        $name = $this->argument('name');
        $this->success("Hello, {$name}!");
        return self::SUCCESS;
    }
}

// Without container
$app = new Application();
$app->add(new GreetCommand());

// With container (optional)
$app = new Application($container);
$app->addCommands([
    new GreetCommand(),           // Direct instance
    AnotherCommand::class,        // Resolved from container if available
]);

$app->run();

// Basic output
$this->success('Success message');
$this->error('Error message');
$this->info('Info message');
$this->warning('Warning message');
$this->note('Note message');
$this->comment('Comment message');

// Structured output
$this->title('Section Title');
$this->section('Subsection');
$this->table(['Header1', 'Header2'], [['Row1', 'Row2']]);

// Interactive
$name = $this->ask('What is your name?', 'Guest');
$confirm = $this->confirm('Continue?', true);
$choice = $this->choice('Select option', ['a', 'b', 'c'], 'a');

// Raw output
$this->line('A line of text');
$this->write('Text without newline');

// Get arguments
$name = $this->argument('name');

// Get options
$verbose = $this->option('verbose');

// Access container (only if provided to Application)
if ($this->container) {
    $service = $this->container->get('service');
}

return self::SUCCESS;  // Command succeeded
return self::FAILURE;  // Command failed
return self::INVALID;  // Invalid usage

class MyCommand extends BaseCommand
{
    protected function configure(): void
    {
        $this->setName('my:command')
             ->setDescription('Does something');
    }

    protected function handle(): int
    {
        $this->success('Done!');
        return self::SUCCESS;
    }
}

// Get input
$name = $this->argument('name');
$verbose = $this->option('verbose');

// Output
$this->success('Success!');
$this->error('Error!');
$this->info('Info');
$this->table(['Col1', 'Col2'], $rows);

// Interactive
$answer = $this->ask('Question?', 'default');
$confirm = $this->confirm('Continue?', true);