PHP code example of weew / console

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

    

weew / console example snippets


$console = new Console();

class SampleCommand {
    public function setup(ICommand $command) {
        // describe command

        $command->setName('colors')
            ->setDescription('Shows a list of colors');

        $command->argument(ArgumentType::SINGLE, 'favorite_color');
        $command->argument(ArgumentType::SINGLE_OPTIONAL, '--only-light');
        $command->argument(ArgumentType::SINGLE_OPTIONAL, '--only-dark');
    }

    public function run(IInput $input, IOutput $output, IConsole $console) {
        // do your thang

        if ( ! $input->hasOption('--only-dark')) {
            $output->writeLine('<red>red</red>');
        }

        if ( ! $input->hasOption('--only-light')) {
            $output->writeLine('<blue>blue</blue>');
        }

        if ($input->hasArgument('favorite_color')) {
            $favoriteColor = $input->getArgument('favorite_color');
            $output->writeLine("The best of all colors is <keyword>$favoriteColor</keyword>");
        }
    }
}

$console->addCommand(SampleCommand::class);

// or

$console->addCommand(new SampleCommand());

$console->parseString('colors red --only-dark');

// or

$console->parseArgs(['colors', 'red', '--only-dark'];

// or

$console->parseArgv(['./file_name', 'colors', 'red', '--only-dark']);

$command->setParallel(false);

$input->getCommand();

$input->getArgument('argument');
$input->getOption('--option');

$input->readline();
$input->readChar();

$output->writeLine('<keyword>key: </keyword> value');
$output->write('some text);

$table = new TableWidget($input, $output);
$table
    ->setTitle('Table title')
    ->addRow('task1', 'x')
    ->addSection('Done tasks')
    ->addRow('task2', '✓');

$table->render();

$prompt = new PromptHelper($input, $output);
$response = $prompt->ask('Are you ready?');
$response = $prompt->prompt('What is your name');