Skip to content

Commit

Permalink
Merge pull request #16 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Chaining on the Class `Runner`
  • Loading branch information
usernane authored Jul 8, 2023
2 parents ade9dd2 + 0ab536c commit 99f2242
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 54 deletions.
55 changes: 11 additions & 44 deletions webfiori/cli/CLICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,34 @@
*
* @author Ibrahim
*
* @version 1.0.1
*/
abstract class CLICommand {
/**
* An associative array that contains extra options that can be added to
* the command.
* @var array
* @since 1.0
*/
private $commandArgs;
/**
* The name of the command such as 'help'.
* @var string
* @since 1.0
*/
private $commandName;
/**
* A description of how to use the command or what it does.
* @var string
* @since 1.0
*/
private $description;
/**
*
* @var InputStream
*
* @since 1.0.1
*/
private $inputStream;
/**
*
* @var OutputStream
*
* @since 1.0.1
*/
private $outputStream;
private $owner;
Expand Down Expand Up @@ -78,7 +72,6 @@ abstract class CLICommand {
*
* @param string $description A string that describes what does the job
* do. The description will appear when the command 'help' is executed.
* @since 1.0
*/
public function __construct(string $commandName, array $args = [], string $description = '') {
if (!$this->setName($commandName)) {
Expand Down Expand Up @@ -122,7 +115,6 @@ public function __construct(string $commandName, array $args = [], string $descr
* @return bool If the argument is added, the method will return true.
* Other than that, the method will return false.
*
* @since 1.0
*/
public function addArg(string $name, array $options = []) : bool {
$toAdd = CommandArgument::create($name, $options);
Expand Down Expand Up @@ -196,9 +188,11 @@ public function addArgument(CommandArgument $arg) : bool {
* @param bool $beforeCursor If set to true, the characters which
* are before the cursor will be cleared. Default is true.
*
* @since 1.0
* @return CLICommand The method will return the instance at which the
* method is called on.
*
*/
public function clear(int $numberOfCols = 1, bool $beforeCursor = true) {
public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICommand {
if ($numberOfCols >= 1) {
if ($beforeCursor) {
for ($x = 0 ; $x < $numberOfCols ; $x++) {
Expand All @@ -216,17 +210,22 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) {
$this->moveCursorLeft($numberOfCols + 1);
}
}

return $this;
}
/**
* Clears the whole content of the console.
*
* Note that support for this operation depends on terminal support for
* ANSI escape codes.
*
* @since 1.0
* @return CLICommand The method will return the instance at which the
* method is called on.
*/
public function clearConsole() {
public function clearConsole() : CLICommand {
$this->prints("\ec");

return $this;
}
/**
* Clears the line at which the cursor is in and move it back to the start
Expand All @@ -235,7 +234,6 @@ public function clearConsole() {
* Note that support for this operation depends on terminal support for
* ANSI escape codes.
*
* @since 1.0
*/
public function clearLine() {
$this->prints("\e[2K");
Expand All @@ -260,7 +258,6 @@ public function clearLine() {
* @return bool If the user choose 'y', the method will return true. If
* he chooses 'n', the method will return false.
*
* @since 1.0
*
*/
public function confirm(string $confirmTxt, bool $default = null) : bool {
Expand Down Expand Up @@ -305,7 +302,6 @@ public function confirm(string $confirmTxt, bool $default = null) : bool {
*
* @param string $message The message that will be shown.
*
* @since 1.0
*/
public function error(string $message) {
$this->printMsg($message, 'Error', 'light-red');
Expand All @@ -320,7 +316,6 @@ public function error(string $message) {
* Other than that, it will return a number which depends on the return value of
* the method 'CLICommand::exec()'.
*
* @since 1.0
*/
public function excCommand() : int {
$retVal = -1;
Expand Down Expand Up @@ -350,7 +345,6 @@ public function excCommand() : int {
* if the command is executed successfully and return -1 if the
* command did not execute successfully.
*
* @since 1.0
*/
public abstract function exec() : int;
/**
Expand Down Expand Up @@ -383,7 +377,6 @@ public function getArg(string $name) {
* <ul>
* Note that the last index might not be set.
*
* @since 1.0
*/
public function getArgs() : array {
return $this->commandArgs;
Expand All @@ -407,7 +400,6 @@ public function getArgsNames() : array {
* @return string|null If the value of the option is set, the method will
* return its value as string. If it is not set, the method will return null.
*
* @since 1.0
*/
public function getArgValue(string $optionName) {
$trimmedOptName = trim($optionName);
Expand All @@ -432,7 +424,6 @@ public function getArgValue(string $optionName) {
* @return string The description of the command. Default return value
* is '&lt;NO DESCRIPTION&gt;'
*
* @since 1.0
*/
public function getDescription() : string {
return $this->description;
Expand Down Expand Up @@ -460,7 +451,6 @@ public function getDescription() : string {
* Note that if the input has special characters or spaces at the
* beginning or the end, they will be trimmed.
*
* @since 1.0
*/
public function getInput(string $prompt, string $default = null, InputValidator $validator = null) {
$trimmed = trim($prompt);
Expand Down Expand Up @@ -496,7 +486,6 @@ public function getInput(string $prompt, string $default = null, InputValidator
* @return null|InputStream If the stream is set, it will be returned as
* an object. Other than that, the method will return null.
*
* @since 1.0.1
*/
public function getInputStream() : InputStream {
return $this->inputStream;
Expand All @@ -510,7 +499,6 @@ public function getInputStream() : InputStream {
* @return string The name of the command (such as 'v' or 'help'). Default
* return value is 'new-command'.
*
* @since 1.0
*/
public function getName() : string {
return $this->commandName;
Expand All @@ -521,7 +509,6 @@ public function getName() : string {
* @return null|OutputStream If the stream is set, it will be returned as
* an object. Other than that, the method will return null.
*
* @since 1.0.1
*/
public function getOutputStream() : OutputStream {
return $this->outputStream;
Expand All @@ -545,7 +532,6 @@ public function getOwner() {
* return true. If no argument which has the given name does exist, the method
* will return false.
*
* @since 1.0
*/
public function hasArg(string $argName) : bool {
foreach ($this->getArgs() as $arg) {
Expand All @@ -564,7 +550,6 @@ public function hasArg(string $argName) : bool {
*
* @param string $message The message that will be shown.
*
* @since 1.0
*/
public function info(string $message) {
$this->printMsg($message, 'Info', 'blue');
Expand All @@ -579,7 +564,6 @@ public function info(string $message) {
* @return bool If the argument is provided, the method will return
* true. Other than that, the method will return false.
*
* @since 1.0
*/
public function isArgProvided(string $argName) : bool {
$argObj = $this->getArg($argName);
Expand All @@ -600,7 +584,6 @@ public function isArgProvided(string $argName) : bool {
* @param int $lines The number of lines the cursor will be moved. Default
* value is 1.
*
* @since 1.0
*/
public function moveCursorDown(int $lines = 1) {
if ($lines >= 1) {
Expand All @@ -616,7 +599,6 @@ public function moveCursorDown(int $lines = 1) {
* @param int $numberOfCols The number of columns the cursor will be moved. Default
* value is 1.
*
* @since 1.0
*/
public function moveCursorLeft(int $numberOfCols = 1) {
if ($numberOfCols >= 1) {
Expand All @@ -632,7 +614,6 @@ public function moveCursorLeft(int $numberOfCols = 1) {
* @param int $numberOfCols The number of columns the cursor will be moved. Default
* value is 1.
*
* @since 1.0
*/
public function moveCursorRight(int $numberOfCols = 1) {
if ($numberOfCols >= 1) {
Expand All @@ -653,7 +634,6 @@ public function moveCursorRight(int $numberOfCols = 1) {
* @param int $col The number of column at which the cursor will be moved
* to. If not specified, 0 is used.
*
* @since 1.0
*/
public function moveCursorTo(int $line = 0, int $col = 0) {
if ($line > -1 && $col > -1) {
Expand All @@ -669,7 +649,6 @@ public function moveCursorTo(int $line = 0, int $col = 0) {
* @param int $lines The number of lines the cursor will be moved. Default
* value is 1.
*
* @since 1.0
*/
public function moveCursorUp(int $lines = 1) {
if ($lines >= 1) {
Expand All @@ -685,7 +664,6 @@ public function moveCursorUp(int $lines = 1) {
*
* @param array $array The array that will be printed.
*
* @since 1.0
*/
public function printList(array $array) {
for ($x = 0 ; $x < count($array) ; $x++) {
Expand All @@ -710,7 +688,6 @@ public function printList(array $array) {
* @param mixed $_ One or more extra arguments that can be supplied to the
* method. The last argument can be an array that contains text formatting options.
* for available options, check the method CLICommand::formatOutput().
* @since 1.0
*/
public function println(string $str = '', ...$_) {
$argsCount = count($_);
Expand Down Expand Up @@ -738,7 +715,6 @@ public function println(string $str = '', ...$_) {
* method. The last argument can be an array that contains text formatting options.
* for available options, check the method CLICommand::formatOutput().
*
* @since 1.0
*/
public function prints(string $str, ...$_) {
$argCount = count($_);
Expand All @@ -763,7 +739,6 @@ public function prints(string $str, ...$_) {
* @return string The method will return the string which was given as input
* in the input stream.
*
* @since 1.0
*/
public function read(int $bytes = 1) : string {
return $this->getInputStream()->read($bytes);
Expand Down Expand Up @@ -870,7 +845,6 @@ public function readInteger(string $prompt, int $default = null) : int {
* @return string The method will return the string which was taken from
* input stream without the end of line character.
*
* @since 1.0
*/
public function readln() : string {
return $this->getInputStream()->readLine();
Expand Down Expand Up @@ -950,7 +924,6 @@ public function removeArgument(string $name) : bool {
* @return string|null The method will return the value which is selected by
* the user. If choices array is empty, null is returned.
*
* @since 1.0
*/
public function select(string $prompt, array $choices, int $defaultIndex = -1) {
if (count($choices) != 0) {
Expand Down Expand Up @@ -991,7 +964,6 @@ public function select(string $prompt, array $choices, int $defaultIndex = -1) {
* <li>The given value is empty string or null.</li>
* </u>
*
* @since 1.0
*/
public function setArgValue(string $argName, string $argValue = ''): bool {
$trimmedArgName = trim($argName);
Expand Down Expand Up @@ -1031,7 +1003,6 @@ public function setDescription(string $str) : bool {
*
* @param InputStream $stream An instance that implements an input stream.
*
* @since 1.0.1
*/
public function setInputStream(InputStream $stream) {
$this->inputStream = $stream;
Expand All @@ -1048,7 +1019,6 @@ public function setInputStream(InputStream $stream) {
* @return bool If the name of the command is set, the method will return
* true. Other than that, the method will return false.
*
* @since 1.0
*/
public function setName(string $name) : bool {
$trimmed = trim($name);
Expand All @@ -1066,7 +1036,6 @@ public function setName(string $name) : bool {
*
* @param OutputStream $stream An instance that implements output stream.
*
* @since 1.0.1
*/
public function setOutputStream(OutputStream $stream) {
$this->outputStream = $stream;
Expand All @@ -1088,7 +1057,6 @@ public function setOwner(Runner $owner = null) {
*
* @param string $message The message that will be displayed.
*
* @since 1.0
*/
public function success(string $message) {
$this->printMsg($message, 'Success', 'light-green');
Expand All @@ -1101,7 +1069,6 @@ public function success(string $message) {
*
* @param string $message The message that will be shown.
*
* @since 1.0
*/
public function warning(string $message) {
$this->prints('Warning: ', [
Expand Down
Loading

0 comments on commit 99f2242

Please sign in to comment.