PHP code example of ride / lib-cli

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

    

ride / lib-cli example snippets




use ride\library\cli\command\AbstractCommand;

class MyCommand extends AbstractCommand {

    public function __construct() {
        parent::__construct('name', 'description');
        
        // Define a e is the rest of the command line.
        // There are no more arguments allowed after a dynamic one
        $this->addArgument('arg3', 'description', false, true);
        
        // Define a flag (always optional)
        $this->addFlag('flag', 'description');
    }
    
    public function execute() {
        // Get the defined input values
        $arg1 = $this->input->getArgument('arg1');
        $arg2 = $this->input->getArgument('arg2', 'default');
        $arg3 = $this->input->getArgument('arg3');
        $flag = $this->input->getFlag('flag');

        if ($this->input->isInteractive()) {
            // Interactive shell, read some input interactive
            $arg4 = $this->input->read($this->output, 'my prompt: ');
        }            
        
        // Write some output
        $this->output->write("output");
        $this->output->writeLine("output line");
        $this->output->writeError("error output");
        $this->output->writeErrorLine("error output line");
    }
    
}



use ride\library\cli\command\CommandContainer;
use ride\library\cli\input\PhpInput;
use ride\library\cli\output\PhpOutput;
use ride\library\cli\Cli;
use ride\library\cli\CommandInterpreter;

// Create a command container and add some commands to it
$commandContainer = new CommandContainer();
$commandContainer->addCommand(new MyCommand()); // the command from previous sample

// Create a command interpreter from the container
$commandInterpreter = new CommandInterpreter($commandContainer);

// Create your input and output interface
$input = new PhpInput(); // ReadlineInput for interactive shell with auto completion and history 
$output = new PhpOutput();

// and create and run the cli
$cli = new Cli($commandInterpreter);
$cli->setInput($input); // input for the commands
$cli->setOutput($output);

$cli->run($input); // input for the CLI, you can use ArgumentInput to parse the command line

// exit with a proper code
exit($cli->getExitCode());