PHP code example of swew / cli

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

    

swew / cli example snippets




namespace My\Cli\Commands;

use Swew\Cli\Command;

class ShowTimeCommand extends Command {
    const NAME = 'show:time {prefix}';

    const DESCRIPTION = 'The function shows the current time.';

    public function __invoke(): int
    {
        $prefix = $this->arg('prefix')->getValue();

        $time = $prefix . ' ' . date('H:i:s');

        $this->output->writeLn($time);

        return self::SUCCESS;
    }
}



namespace My\Cli;

use Swew\Cli\SwewCommander;
use My\Cli\Commands\ShowTimeCommand;

class MySuperCli extends SwewCommander
{
    protected array $commands = [
        ShowTimeCommand::class,
    ];
}


// index.php

Cli;

(new MySuperCli($argv))->run();

class SendEmail extends Command
{
    const NAME = 'send:email';

    const DESCRIPTION = 'Command to send email';

    public function __invoke(): int
    {
        // ...
        return self::SUCCESS;
    }
}

// Only command name, no arguments
const NAME = 'send:email';

// One  empty string
const NAME = 'send:email {--userId=}';
// Optional argument with default value is string
const NAME = 'send:email {--userId=1}';
// Optional argument, userId as array
const NAME = 'send:email {--userId=[]}';

/** Typing */
// Argument with typing
const NAME = 'send:email {--userId (int)}';

// Argument with typing
const NAME = 'send:email {--userId (float)}';

// Argument with typing, default userId === 0
const NAME = 'send:email {--userId= (int)}';

// Argument with typing, default userId === ''
const NAME = 'send:email {--userId= (str)}';

// Argument with typing, default userId === false
const NAME = 'send:email {--userId= (bool)}';

// Argument with typing, default:     userId === false
// if command: `send:email`           userId === true
// if command: `send:email --userId`  userId === true
const NAME = 'send:email {--userId= (bool)}';

// Alias for argument
const NAME = 'send:email {-S|--silent}';

/** Description */
const NAME = 'send:email {mail : The description, will be reflected in help}';

/** Example command with one argument in the full description */
const NAME = 'send:email {--count|-C=1          (int) : Count of mails}';
          // command | argument|alias|default | type | description

$this->output->write('Hello ');
$this->output->write('world', '<b>%s <br></>'); // formatting

$this->output->writeLn('Hello'); // write with new line
$this->output->writeLn('world', '<b>%s <br></>');

$this->output->info('Some good news');
$this->output->warn('A little attention');
$this->output->error('Something has gone wrong');

$this->output->clear(); // Reset terminal window

$this->output->width();
$this->output->height();

$this->output->write('<b><black><bgRed>Hello world</>');

// OR
$format = '<b><black><bgRed>%s</>';
$this->output->write('Hello world', $format);

$this->output->setAnsi(false);

$this->output->clearColor($text); // no color string

// Write a single blank line...
$this->output->newLine(); 
// Write three blank lines...
$this->output->newLine(3);

$this->output->getLink($url, $text);

public function __invoke(): int {
	$name = $this->output->ask('What is your name?');
	// ...
	return self::SUCCESS;
}

public function __invoke(): int {
	$name = $this->output->askYesNo('Is this the best CLI utility?', true);
	// ...
	return self::SUCCESS;
}

public function __invoke(): int {
	$password = $this->output->secret('What is the password?');
	// ...
	return self::SUCCESS;
}

public function __invoke(): int {
	/** @var string */
	$answer = $this->output->choice(
		'What is your name?',
		['Leo', 'Mike', 'Don', 'Raph'],
		$isRequired = true // optional
	);

    $this->output->writeLn($answer);

	// ...
	return self::SUCCESS;
}

public function __invoke(): int {
	/** @var array */
	$answer = $this->output->select(
		'What is your name?',
		['Leo', 'Mike', 'Don', 'Raph'],
		$selectedIndex = [], // optional
		$isRequired = true,  // optional
		$isMultiple = true,  // optional
	);

	// ...
	return self::SUCCESS;
}

$title = ['Name', 'Age', 'Weapon'];

$list = [
	['Leo', 22, 'Swords'],
	['Mike', 21.6, 'Nunchaks'],
	['Don', 21.9, 'Bo'],
	['Raphael', 21.5, 'Saii'],
];

$this->output->table($title, $list);

$bar = $this->output->createProgressBar(count($users)); 

$bar->start(); // show progress bar

foreach ($users as $user) {
	$this->someTask($user);

	$bar->increment(); // progress
}

$bar->finish(); // remove progressbar



wew\Cli\Terminal\Output;

$output = new Output();

$output->writeLn('Hello world!');


sh
php index.php 'show:time at'