PHP code example of codeinc / console

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

    

codeinc / console example snippets



use CodeInc\Console\Console;

// the parameter specifies if the class should throw exceptions
$console = new Console(true);

/*
 * Asks a Yes / No question and returns a boolean
 */
// returns true or false
$console->askBool("Do you like chocolate?"); 

/*
 * Asks a questions expecting a string as an answer
 */
// returns the response or null if no response is provided
echo $console->askString("What is your city?"); 

// returns the response or throws an exception if no response is provided
echo $console->askString("What is your city?", false); 

/*
 * Asks a questions with a closed list of anwsers
 */
$colors = ["green", "red", "blue", "purple", "orange", "yellow"];

// returns chosen color or throws an exception if no response is provided
echo $console->askOptions("What is your favorite color?", $colors); 

// returns chosen color or null if no response is provided
echo $console->askOptions("What is your favorite color?", $colors, true); 


use CodeInc\Console\CommandLine;

// returns true if the script is running in CLI mode
CommandLine::isCLI(); 

// returns true if the current user is 'root'
CommandLine::isRoot(); 

// returns true if the current user is 'username'
CommandLine::isUser("username"); 

// returns the current user name or null is the user is unknow.
CommandLine::getUser();

// thow an exception if not in CLI mode
CommandLine::


use CodeInc\Console\Arguments;

// for the request 'myScript.php --param1=val1 --param2 -aDG'
$arguments = new Arguments();
$arguments->hasParameter("a"); // returns true
$arguments->hasParameter("D"); // returns true
$arguments->hasParameter("G"); // returns true
$arguments->hasParameter("z"); // returns false
$arguments->getArgumentValue("param1"); // returns 'val1'
$arguments->getArgumentValue("param2"); // returns null
$arguments->getArgumentValue("param3"); // returns false
$arguments->hasArgument("param2"); // returns true