ConsoleHelper

Last updated: April 26th, 2020
Release:

The ConsoleHelper class provides some general functionality that is commonly required when dealing with command line (terminal) input/output.

Methods

public ConsoleHelper __construct(array $argv = null, boolean $forceCli = false) Instantiates a new ConsoleHelper object with the option to provide arguments and force 'CLI' mode public boolean compareArg(string $key, string $value, boolean $caseInsensitive = false) Compares an argument by key optionally without case sensitivity public boolean compareArgAt(integer $index, string $value, boolean $caseInsensitive = false) Compares an argument at the given index optionally without case sensitivity public null|string get(integer $characters = 1) Retrieves the given number of characters from STDIN public string getLine() Retrieves an entire line from STDIN public mixed getParameterWithDefault(string $short, string $long, mixed $default = null, boolean $caseInsensitive = false) Attempts to retrieve an argument by short or long key name, returning a default value if not found public ReturnHelper getQueriedInput(string $query, mixed $defaultValue, string $errorMessage, integer $maxTries = 5, null|callable $validation = null, null|callable $sanitation = null) Queries a user repeatedly for input public mixed getSelf() Returns the script being called according to the passed arguments (first arg in $argv) public boolean hasArg(string $key, boolean $caseInsensitive = false) Checks if the given key exists in the argument list public boolean hasShortLongArg(string $short, string $long, boolean $caseInsensitive = false) Checks if the given key exists in the argument list public boolean isCLI() Returns whether or not the PHP invocation is via CLI or is emulating CLI public boolean isNaturalCLI() Returns whether or not the PHP invocation is via CLI, ignoring any emulation public integer numArgs() Returns the number of arguments public array parameters(boolean $asAssociative = false, boolean $caseSensitive = false) Returns the argument collection protected array parseParams(array $args, boolean $caseInsensitive = false) Parses a collection of arguments into an organized collection public void put(string $buf Outputs the buffer to STDIN public void putLine(string $buf Outputs the buffer to STDIN followed by a newline

Queried Input

The ConsoleHelper::getQueriedInput() method encapsulates a common scenario of having to prompt an end-user for input that is then validated/sanitized. For example, if you need to ask the user for their name:


use Stoic\Utilities\ConsoleHelper;

$ch = new ConsoleHelper($argv);
$name = $ch->qetQueriedInput(
	"Please enter your name", // $query
	null, // $defaultValue
	"You didn't provide a name", // $errorMessage
	3, // $maxTries
	function ($input) { return !empty($input) && strlen($input) > 3; }, // validation callable
	function ($input) { return trim($input); } // sanitation callable
);

/*
	* Will prompt the user up to 3 times to enter their non-empty 4+ character long name...
	*
	*  Please enter your name: Andrew
	*  Hello, Andrew!
	*
	*/

if ($name->isGood()) {
	$ch->putLine("Hello, {$name->getResults()[0]}!");
}

Further Reading

Next Up

Continue to read about FileHelper, or visit the Table of Contents.