PHP code example of eureka / component-console

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

    

eureka / component-console example snippets




declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\AbstractScript;
use Eureka\Component\Console\Help;
use Eureka\Component\Console\Option\Option;
use Eureka\Component\Console\Option\Options;

class ExampleScript extends AbstractScript
{
    public function __construct()
    {
        $this->setExecutable();
        $this->setDescription('Example script');

        $this->initOptions(
            (new Options())
                ->add(
                    new Option(
                        shortName:   'u',
                        longName:    'user',
                        description: 'User name',
                        mandatory:   true,
                        hasArgument: true,
                        default:     'Joe doe',
                    )
                )
                ->add(
                    new Option(
                        shortName:   'n',
                        longName:    'is-night',
                        description: 'If is night'
                    )
                )
        );
    }

    public function help(): void
    {
        (new Help(self::class, $this->declaredOptions(), $this->output(), $this->options()))
            ->display()
        ;
    }

    /**
     * @return void
     */
    public function run(): void
    {
        $user = $this->options()->get('user', 'u')->getArgument();
        $say  = $this->options()->get('is-night', 'n')->getArgument() ? 'Good night' : 'Hello';
        
        //~ Can also use short version:
        // $user = $this->options()->value('user', 'u');
        // $say  = $this->options()->value('is-night', 'n') ? 'Good night' : 'Hello';

        $this->output()->writeln("$say $user!");
    }
}




declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Help;
use Eureka\Component\Console\Option\Option;
use Eureka\Component\Console\Option\Options;
use Eureka\Component\Console\Output\NullOutput;

$output  = new NullOutput();
$options = (new Options())
    ->add(
        new Option(
            shortName:   'u',
            longName:    'user',
            description: 'User name',
            mandatory:   true,
            hasArgument: true,
            default:     'Joe doe',
        )
    )
    ->add(
        new Option(
            shortName:   'n',
            longName:    'is-night',
            description: 'If is night'
        )
    )
);
$help = new Help('Application/My/Script', $options, new NullOutput());
$help->display();



declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Output\StreamOutput;

$output = new StreamOutput(\STDOUT, false);
$output->writeln('Hello!'); // PHP_EOL is added to the line
$output->write('Hello!');   // No new line

$output = new StreamOutput(\STDERR, false);
$output->writeln('Error message for "error output (stderr)');



declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Color\Bit4HighColor;
use Eureka\Component\Console\Color\Bit4StandardColor;
use Eureka\Component\Console\Style\Style;

$whiteBold      = (new Style())->bold();
$greenHighlight = (new Style())->color(Bit4StandardColor::Green);
$bgErrorRed     = (new Style())->background(Bit4HighColor::Red);

echo $bgErrorRed->apply('An error as occurred!');



declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Table\Table;

//~ Table with header
$table = new Table(3, new Border(Border::BASE);
$table->newRow(['Col 1', 'Col 2', 'Col 3'], true);
$table->newRow([1, 2, 3]);
$table->newRow(["text", "very long text", 1.2]);
$table->newRowSpan('Spanned row');
echo $table->render();



declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Progress\ProgressBar;

$maxIteration = 10;
$maxSize      = 20;
$progress = new ProgressBar(new Options(), $maxIteration, $maxIteration);
$progress->setTypeDisplay(ProgressOld::TYPE_BAR);

for ($i = 0; $i < $maxIteration; $i++) {
    $progress->display('iteration #' . $i);
}




declare(strict_types=1);

namespace Examples;

use Eureka\Component\Console\Output\NullOutput;use Eureka\Component\Console\Output\StreamOutput;use Eureka\Component\Console\Terminal\Terminal;

$output   = new StreamOutput(\STDOUT, false);
$terminal = new Terminal(new NullOutput());

//~ Get Terminal sizes
$output->writeln("{$terminal->getWidth()}x{$terminal->getHeight()}");

//~ Clear terminal
$terminal->clear();

//~ Get cursor and manipulate it
$terminal->cursor()->down();