PHP code example of pointybeard / php-cli-lib

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

    

pointybeard / php-cli-lib example snippets


use CLILib\Message;

## Display a message to STDOUT
(new Message)
    ->message("This is my message")
    ->prependDate(true)
    ->dateFormat('G:i:s > ')
    ->appendNewLine(true)
    ->foreground("light green")
    ->background("red")
    ->display(STDOUT)
;

use CLILib\Prompt;
use CLILib\Message;

// Most basic usage
$name = Prompt::display(
    "Please enter your name"
);
## > Please enter your name:
## >

// Asking for a password
$password = Prompt::display(
    "Please enter your password", Prompt::FLAG_SILENT
);
## > Please enter your password:
## >

// Providing a Message object and default value
$message = (new Message)
    ->message("Enter database user name")
    ->prependDate(false)
    ->appendNewLine(false)
    ->foreground("light green")
    ->background("red")
;
$databaseUserName = Prompt::display(
    $message, null, "root",
);
## > Enter database user name [root]:
## >

// Using a validator to check the input
$user = Prompt::display(
    "Enter your user name", null, null, function($input){
        if(strlen(trim($input)) == 0) {
            (new Message
                ->message("Error: You must enter a user name!")
                ->prependDate(false)
                ->appendNewLine(true)
                ->foreground("red")
            ;
            return false;
        }
        return true;
    }
);
## > Enter your user name:
## >
## > Error: You must enter a user name!
## > Enter your user name:
## >

use CLILib\Argument;

// Load up the arguments from $argv. By default
// it will ignore the first item, which is the
// script name
$args = new Argument\Iterator();

// Instead of using $argv, send in an array
// of arguments. e.g. emulates "... -i --database blah"
$args = new Argument\Iterator(false, [
    '-i', '--database', 'blah'
]);

// Arguments can an entire string too [Added 1.0.1]
$args = new Argument\Iterator(false, [
    '-i --database blah'
]);

// Iterate over all the arguments
foreach($args as $a){
    printf("%s => %s" . PHP_EOL, $a->name(), $a->value());
}

// Find a specific argument by name
$args->find('i');

// Find also accepts an array of values, returning the first one that is valid
$args->find(['h', 'help', 'usage']);