PHP code example of irfantoor / command

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

    

irfantoor / command example snippets



IrfanTOOR\Command;

$cmd = new Command(
    [
        'name' => 'hello1.php', 
        'description' => 'hello world! of command', 
        'handler' => function($cmd){
            $name = $cmd->read('your name: ', 'green');
            $cmd->writeln("Hello " . ucwords($name) . '!', "yellow");
        },
        'version' => '1.1'
    ]
);

$cmd->run();



rfanTOOR\Command;
use IrfanTOOR\Debug;
Debug::enable(1);

class HelloCommand extends Command
{
    function __construct()
    {
        parent::__construct([
            'name' => 'hello', 
            'description' => 'hello world! of command',
            'version' => '1.3',
        ]);
    }

    function init()
    {
        $this->addOption('g|greeting', 'Sets the greeting', 'Hello');
        $this->addArgument('name', 'Name to be greeted', self::ARGUMENT_OPTIONAL, 'World!');
    }

    function main()
    {
        $greeting = $this->getOption('greeting');
        $name = ucfirst($this->getArgument('name'));

        $this->writeln($greeting . " " . $name, "yellow");
    }
}

class CalCommand extends Command
{
    function __construct()
    {
        parent::__construct([
            'name' => 'cal', 
            'description' => 'prints a calendar', 
            'version' => '0.1'
        ]);
    }

    function main()
    {
        ob_start();
        system("cal");
        $output = ob_get_clean();
        $this->writeln($output, "yellow");
    }
}

$cmd = new Command([
    'name' => 'hello4',
    'description' => "Composit command",
    'version' => '1.4',
    'handler' => function($cmd) {
    }
]);

$cmd->addCommand('hello', HelloCommand::class);
$cmd->addCommand('cal', CalCommand::class);

$cmd->run();
sh
$ php hello1.php --help
$ php hello1.php -V
$ php hello1.php -v
sh
$ php hello4.php hello world!
$ php hello4.php hello -g=Hi irfan
$ php hello4.php hello --greeting="Hi there," buddy
sh
php hello4.php hello -g Hi irfan
# or
php hello4.php hello --greeting "Hay you! cheers" man
sh
$ php hello4.php -h
# http://localhost:8000/hello4.php?args=-h


$ php hello4.php hello -g Hi irfan

# args as string:
# http://localhost:8000/hello4.php?args=hello%20-g%20Hi%20irfan

# args as array
# http://localhost:8000/hello4.php?args[]=hello&args[]=-g&args[]=Hi&args[]=irfan

# args must be used as array, when args values has spaces e.g.:
# http://localhost:8000/hello4.php?args[]=hello&args[]=-g&args[]=Hay%20You!&args[]=young%20man