PHP code example of webfiori / cli

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

    

webfiori / cli example snippets



WebFiori\Cli\Command;
use WebFiori\Cli\Runner;

class GreetCommand extends Command {
    public function __construct() {
        parent::__construct('greet', [], 'Greet the user');
    }

    public function exec(): int {
        $this->println("Hello from WebFiori CLI!");
        return 0;
    }
}

$runner = new Runner();
$runner->register(new GreetCommand());
exit($runner->start());


use WebFiori\Cli\Command;
use WebFiori\Cli\Option;

class PersonalGreetCommand extends Command {
    public function __construct() {
        parent::__construct('greet-person', [
            '--name' => [
                Option::OPTIONAL => false,
                Option::DESCRIPTION => 'Name of the person to greet'
            ],
            '--title' => [
                Option::OPTIONAL => true,
                Option::DEFAULT => 'Friend',
                Option::DESCRIPTION => 'Title to use (Mr, Ms, Dr, etc.)'
            ]
        ], 'Greet a specific person');
    }

    public function exec(): int {
        $name = $this->getArgValue('--name');
        $title = $this->getArgValue('--title');
        
        $this->println("Hello %s %s!", $title, $name);
        return 0;
    }
}


use WebFiori\Cli\Runner;

// Register multiple commands
$runner = new Runner();
$runner->register(new GreetCommand());
$runner->register(new PersonalGreetCommand());
$runner->register(new FileProcessCommand());
$runner->register(new DatabaseCommand());

// Set application info
$runner->setAppName('My CLI App');
$runner->setAppVersion('1.0.0');

exit($runner->start());

use WebFiori\Cli\Streams\FileInputStream;
use WebFiori\Cli\Streams\FileOutputStream;

// Read from file instead of stdin
$command->setInputStream(new FileInputStream('input.txt'));

// Write to file instead of stdout
$command->setOutputStream(new FileOutputStream('output.txt'));

public function exec(): int {
    $this->println("This is %s text", 'normal');
    $this->println("This is {{bold}}bold{{/bold}} text");
    $this->println("This is {{red}}red{{/red}} text");
    $this->println("This is {{bg-blue}}{{white}}white on blue{{/white}}{{/bg-blue}} text");
    return 0;
}

use WebFiori\Cli\Progress\ProgressBar;

public function exec(): int {
    $items = range(1, 100);
    
    $this->withProgressBar($items, function($item, $bar) {
        // Process each item
        usleep(50000); // Simulate work
        $bar->setMessage("Processing item {$item}");
    });
    
    return 0;
}

public function exec(): int {
    $data = [
        ['Ahmed Hassan', 30, 'Cairo'],
        ['Sarah Johnson', 25, 'Los Angeles']
    ];
    $headers = ['Name', 'Age', 'City'];
    
    $this->table($data, $headers);
    
    return 0;
}

$runner = new Runner();
$runner->addDiscoveryPath(__DIR__ . '/commands');
$runner->discoverCommands();

use WebFiori\Cli\Discovery\CommandDiscovery;
use WebFiori\Cli\Discovery\CommandCache;

$discovery = new CommandDiscovery(new CommandCache(enabled: false));
$discovery->addSearchPath(__DIR__ . '/commands');
$discovery->setFactory(fn($class) => $container->get($class));

$runner->setCommandDiscovery($discovery);
$runner->discoverCommands();
bash
# Install via Composer
composer  "
\Cli\Runner;

class HelloCommand extends Command {
    public function __construct() {
        parent::__construct('hello', [], 'Say hello to the world');
    }
    public function exec(): int {
        \$this->println('Hello, World!');
        return 0;
    }
}

\$runner = new Runner();
\$runner->register(new HelloCommand());
exit(\$runner->start());
" hello
bash
php app.php greet
# Output: Hello from WebFiori CLI!
bash
php app.php greet-person --name=John --title=Mr
# Output: Hello Mr John!

php app.php greet-person --name=Sarah
# Output: Hello Friend Sarah!
 php

//File 'src/SampleCommand.php'
use WebFiori\Cli\Command;

class SampleCommand extends Command {

    public function __construct(){
        parent::__construct('say-hi');
    }

    public function exec(): int {
        $this->println("Hi People!");
        return 0;
    }

}

 php
// File src/main.php
;
use SampleCommand;


$runner = new Runner();
$runner->register(new SampleCommand());
exit($runner->start());
 bash
php main.php say-hi
 php

//File 'src/SampleCommand.php'
use WebFiori\Cli\Command;
use WebFiori\Cli\Option;

class SampleCommand extends Command {

    public function __construct(){
        parent::__construct('say-hi', [
            '--person-name' => [
                Option::OPTIONAL => true
            ]
        ]);
    }

    public function exec(): int {
        $this->println("Hi People!");
        return 0;
    }

}

 php

//File 'src/SampleCommand.php'
use WebFiori\Cli\Command;
use WebFiori\Cli\Option;

class SampleCommand extends Command {

    public function __construct(){
        parent::__construct('say-hi', [
            '--person-name' => [
                Option::OPTIONAL => true
            ]
        ]);
    }

    public function exec(): int {
        $personName = $this->getArgValue('--person-name');
        
        if ($personName !== null) {
            $this->println("Hi %s!", $personName);
        } else {
            $this->println("Hi People!");
        }
        
        return 0;
    }

}

 bash
php main.php -i
 php

//File 'src/SampleCommand.php'
use WebFiori\Cli\Command;
use WebFiori\Cli\Option;

class GreetingsCommand extends Command {

    public function __construct() {
        parent::__construct('hello', [
            '--person-name' => [
                Option::DESCRIPTION => 'Name of someone to greet.',
                Option::OPTIONAL => true
            ]
        ], 'A command to show greetings.');
    }

    public function exec(): int {
        $name = $this->getArgValue('--person-name');

        if ($name === null) {
            $this->println("Hello World!");
        } else {
            $this->println("Hello %s!", $name);
        }

        return 0;
    }
}

 bash
php main.php help 
 bash
php main.php help --command-name=hello
 php
namespace tests\cli;

use WebFiori\Cli\CommandTestCase;

class HelloCommandTest extends CommandTestCase {
    /**
     * @test
     */
    public function test00() {
        
        //Verify test results
        
        $this->assertEquals([
            "Hello World!\n"
        ], $this->executeSingleCommand(new HelloWorldCommand()));
        $this->assertEquals(0, $this->getExitCode());
    }
}