PHP code example of evo / cli

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

    

evo / cli example snippets


    //command line call (typically for the help doc)
    > php {pathto}example.php -h
    
    > php {pathto}example.php -o

	//get an instance of Cli, this is a singleton class
	public static function getInstance(): self;
	//set arguments with a config array
	public function fromConfig(array $conf): self;	
	//set an argument to accept
	public function setArgument(string $shortName, ?string $longName=null, string $doc='', array $options=[]): self;
	//(changed in 2.0) get a list of the argument set with setArgument
	public function getArguments() : array;
	//(added in 2.0) get a single argument set 
	public function getArgument(string $which): array;
	//(added in 2.0) convert an arguments name to the short version ( safe for leading hypen and short names)
	public function toShortName(string $long_name): string|false;
	//(added in 2.0) convert an arguments name to the long version ( safe for leading hypens and long names)
	public function toLongName(string $short_name): string|false;
	//set the allowed request types (for overriding)
	public function setAllowedRequestTypes(int $requestType): self;	
	//set the current request types (for overriding auto detection)
	public function setCurrentRequestType(int $requestType): self;	
	//get the current request type (as of version 1.0.2)
	public function getCurrentRequestType(): int;
	//set a request (for overriding)
	public function setRequest(array $request): self;
	//(added in 2.0) get the value of an argument from the request or null to get the request as an array
	public function getRequest(?string $which=null, mixed $default=null): mixed;
	//(added in 2.2) Use this callback method as the default to throw an evo\OutOfBoundsException for an unknown request key
	public static function throwUnknownRequestKey(): callable
	//(added in 2.0) is an argument set in the request or is the request itself set
	public function issetRequest(?string $which=null): bool
	//(added in 2.1) is an argument empty in the request or is the request itself empty
	public function isEmptyRequest(?string $which=null): bool
	//get a list of the allowed options (see options)
	public function getOptions(): array;
	//get the argument help doc as text
	public function getHelpDoc(): string;
	//output the argement help doc
	public function printHelpDoc(bool $exit=true): void;
	//(added in 2.2) Stream output instead of buffering it over HTTP
	public static function streamOutput(): void


	//instanciate	
	$Cli = Cli::getInstance();

	//instanciate	
	$Cli = Cli::getInstance();
	
	//setup a basic argument  (called -h or --help)
	$Cli->setArgument('h', 'help', 'Show this help document');

	//instanciate	
	$Cli = Cli::getInstance();
	//setup a basic argument  (called -h or --help)
	$Cli->setArgument('h', 'help', 'Show this help document');
	//setup an argument that only accepts foo as the input
	$Cli->setArgument('f', 'foo', 'This is just foo, and must always be foo', [
		'accept' => function($shortName, $value){
			if($value == 'foo') return true;
			return false;
		}
	]);
	
	$Cli->setArgument('i', 'input', 'This is input that 

	//instanciate	
	$Cli = Cli::getInstance();
	$Cli->fromConfig([
		[
			'shortName' => 'h',
			'longName' => 'help',
			'doc' => 'Show this help document'
		],[
			'shortName' => 'f',
			'longName' => 'foo',
			'doc' => 'This is just foo, and must always be foo',
			'options' => [
				$Cli::OPT_MUST_VALIDATE  => function($shortName, $value){
					if($value == 'foo') return true;
					return false;
				}
			]
		],[
			'shortName' => 'i',
			'longName' => 'input',
			'doc' => 'This is input that 


//example config.php
    return [['shortName' => 'h','longName' => 'help','doc' => 'Show this help document'],[...]];

 $config = requre 'config.php';
 Cli::getInstance()->fromConfig($config);

	$Cli = Cli::getInstance();
	//setup a basic argument  (called -h or --help)
	$Cli->setArgument('h', 'help', 'Show this help document');
    // ... other arguments ...
    // get an array all arguments (shortName as the keys), changed in 2.0.0,
    $args = $Cli->getArguments();
    //get a single argument (using the shortName), added in 2.0.0,
    $help = $Cli->getArgument('h');
    //get a single argument (using the longName), added in 2.0.0,
    $help = $Cli->getArgument('help');
    //get the request (this will contain only valid arguments)
    $foo = $Cli->geRequest();
	//get the request value of a single argement (default null)
    $foo = $Cli->geRequest('h');
	//get the request value of a single argement (default string foo)
    $foo = $Cli->geRequest('h', 'foo');

	$Cli = Cli::getInstance();
    //allow only $_GET
    $Cli->setAllowedRequestTypes(Cli::REQUEST_GET); 
     //allow only $_POST
    $Cli->setAllowedRequestTypes(Cli::REQUEST_POST);
     //allow only Command line (Default)
    $Cli->setAllowedRequestTypes(Cli::REQUEST_CLI);
    //allow both $_GET & $_POST
    $Cli->setAllowedRequestTypes(Cli::REQUEST_GET|Cli::REQUEST_POST);

	print_r(Cli::getInstance()->getOptions());
    //outputs
    Array (
        [accept] => Option must be a Closure, which must return true to accept a given value for argument
        [

    $Cli = Cli::getInstance();
    $Cli->setArgument('h', 'help', 'Show this help document');
    //... other arguments 
    if($Cli->getArgument('h')) $Cli->printHelpDoc(); //exits