From dfab7580378b8bc4c5595bbd1a41c666265168dc Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:21 -0800 Subject: [PATCH 01/14] Update InitAppCommand.php --- bin/InitAppCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bin/InitAppCommand.php b/bin/InitAppCommand.php index 85a689b..2a1dec4 100644 --- a/bin/InitAppCommand.php +++ b/bin/InitAppCommand.php @@ -1,7 +1,7 @@ create(true); file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data); return true; } @@ -71,8 +72,8 @@ private function createAppClass(string $appPath, string $dirName) { $file->append("\$runner->setDefaultCommand('help');\n\n"); $file->append("//Start your application.\n"); $file->append("exit(\$runner->start());\n\n"); - - $file->write(false, true); + $file->create(true); + $file->write(false); return true; } $this->warning('File app.php already exist!'); From 4dfd90606870d13e16033d910385e1740adfd94a Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:28 -0800 Subject: [PATCH 02/14] Update HelloWorldCommand.php --- example/app/HelloWorldCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/example/app/HelloWorldCommand.php b/example/app/HelloWorldCommand.php index 755381e..1b98f79 100644 --- a/example/app/HelloWorldCommand.php +++ b/example/app/HelloWorldCommand.php @@ -1,13 +1,14 @@ [ - 'description' => 'Name of someone to greet.', - 'optional' => true + Option::DESCRIPTION => 'Name of someone to greet.', + Option::OPTIONAL => true ] ], 'A command to show greetings.'); } From 8e9f6ba3b3d3860069143a16bff6ce5d62587ae3 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:34 -0800 Subject: [PATCH 03/14] Update bootstrap.php --- tests/bootstrap.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7cb4367..48b1e17 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -35,10 +35,11 @@ require_once $baseDir.DS.'streams'.DS.'InputStream.php'; require_once $baseDir.DS.'streams'.DS.'OutputStream.php'; require_once $baseDir.DS.'CLICommand.php'; -require_once $baseDir.DS.'CommandArgument.php'; +require_once $baseDir.DS.'Argument.php'; require_once $baseDir.DS.'Formatter.php'; require_once $baseDir.DS.'KeysMap.php'; require_once $baseDir.DS.'Runner.php'; +require_once $baseDir.DS.'Option.php'; require_once $baseDir.DS.'InputValidator.php'; require_once $baseDir.DS.'streams'.DS.'ArrayInputStream.php'; require_once $baseDir.DS.'streams'.DS.'ArrayOutputStream.php'; From b1634b41654547902f55828231060030bbe2ef1d Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:40 -0800 Subject: [PATCH 04/14] Update CLICommandTest.php --- tests/webfiori/tests/cli/CLICommandTest.php | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/webfiori/tests/cli/CLICommandTest.php b/tests/webfiori/tests/cli/CLICommandTest.php index f0271f6..2555014 100644 --- a/tests/webfiori/tests/cli/CLICommandTest.php +++ b/tests/webfiori/tests/cli/CLICommandTest.php @@ -2,7 +2,7 @@ namespace webfiori\tests\cli; use PHPUnit\Framework\TestCase; -use webfiori\cli\CommandArgument; +use webfiori\cli\Argument; use webfiori\cli\exceptions\IOException; use webfiori\cli\InputValidator; use webfiori\cli\Runner; @@ -41,7 +41,7 @@ public function testSussess00() { */ public function testSussess01() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -66,7 +66,7 @@ public function testInfo00() { */ public function testInfo01() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -91,7 +91,7 @@ public function testWarning00() { */ public function testWarning01() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -116,7 +116,7 @@ public function testError00() { */ public function testError01() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -264,7 +264,7 @@ public function testSelect04() { */ public function testSelect05() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -395,7 +395,7 @@ public function testConfirm02() { */ public function testConfirm03() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -415,7 +415,7 @@ public function testConfirm03() { */ public function testConfirm04() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -432,7 +432,7 @@ public function testConfirm04() { */ public function testConfirm05() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -463,7 +463,7 @@ public function testGetInput00() { */ public function testGetInput01() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -483,7 +483,7 @@ public function testGetInput01() { */ public function testGetInput02() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -501,7 +501,7 @@ public function testGetInput02() { */ public function testGetInput03() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -519,7 +519,7 @@ public function testGetInput03() { */ public function testGetInput04() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -546,7 +546,7 @@ public function testGetInput04() { */ public function testGetInput05() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); @@ -573,7 +573,7 @@ public function testGetInput05() { */ public function testGetInput06() { $command = new TestCommand('cool'); - $ansiArg = new CommandArgument('--ansi'); + $ansiArg = new Argument('--ansi'); $ansiArg->setValue(''); $command->addArgument($ansiArg); $command->setOutputStream(new ArrayOutputStream()); From 1b0e0f0db47654dfa9f26336a0ff24526218f41c Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:52 -0800 Subject: [PATCH 05/14] Update CommandArgumentTest.php --- .../tests/cli/CommandArgumentTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/webfiori/tests/cli/CommandArgumentTest.php b/tests/webfiori/tests/cli/CommandArgumentTest.php index d9fdf52..0928733 100644 --- a/tests/webfiori/tests/cli/CommandArgumentTest.php +++ b/tests/webfiori/tests/cli/CommandArgumentTest.php @@ -2,7 +2,7 @@ namespace webfiori\tests\cli; use PHPUnit\Framework\TestCase; -use webfiori\cli\CommandArgument; +use webfiori\cli\Argument; use webfiori\cli\Runner; /** * Description of CommandArgumentTest @@ -17,7 +17,7 @@ public function extractValueTest00() { $_SERVER['argv'] = [ 'name=ibrahim' ]; - $this->assertEquals('ibrahim', CommandArgument::extractValue('name')); + $this->assertEquals('ibrahim', Argument::extractValue('name')); } /** * @test @@ -30,7 +30,7 @@ public function extractValueTest01() { $r->setArgsVector([ 'name=ali' ]); - $this->assertEquals('ali', CommandArgument::extractValue('name', $r)); + $this->assertEquals('ali', Argument::extractValue('name', $r)); } /** * @test @@ -40,14 +40,14 @@ public function extractValueTest03() { 'name="ibrahim Ali"', "last-name='bin'" ]; - $this->assertEquals('ibrahim Ali', CommandArgument::extractValue('name')); - $this->assertEquals('bin', CommandArgument::extractValue('last-name')); + $this->assertEquals('ibrahim Ali', Argument::extractValue('name')); + $this->assertEquals('bin', Argument::extractValue('last-name')); } /** * @test */ public function test00() { - $arg = new CommandArgument(); + $arg = new Argument(); $this->assertNull($arg->getValue()); $this->assertEquals('', $arg->getDefault()); $this->assertEquals('', $arg->getDescription()); @@ -68,14 +68,14 @@ public function test00() { * @test */ public function test01() { - $arg = new CommandArgument(''); + $arg = new Argument(''); $this->assertEquals('arg', $arg->getName()); } /** * @test */ public function test02() { - $arg = new CommandArgument('--config'); + $arg = new Argument('--config'); $this->assertNull($arg->getValue()); $this->assertEquals('', $arg->getDefault()); $this->assertEquals('', $arg->getDescription()); @@ -88,7 +88,7 @@ public function test02() { * @test */ public function testSetName() { - $arg = new CommandArgument(' '); + $arg = new Argument(' '); $this->assertEquals('arg', $arg->getName()); $this->assertTrue($arg->setName('my-val')); $this->assertEquals('my-val', $arg->getName()); @@ -101,7 +101,7 @@ public function testSetName() { * @test */ public function testSetValue00() { - $arg = new CommandArgument(); + $arg = new Argument(); $this->assertNull($arg->getValue()); $arg->setValue(''); $this->assertEquals('', $arg->getValue()); @@ -112,7 +112,7 @@ public function testSetValue00() { * @test */ public function testSetValue01() { - $arg = new CommandArgument(); + $arg = new Argument(); $this->assertNull($arg->getValue()); $arg->addAllowedValue('Super'); $this->assertFalse($arg->setValue('')); From aa20a5077054ecfd9f893952298e745e38b79b06 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:45:59 -0800 Subject: [PATCH 06/14] Update RunnerTest.php --- tests/webfiori/tests/cli/RunnerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/webfiori/tests/cli/RunnerTest.php b/tests/webfiori/tests/cli/RunnerTest.php index 412c345..e67c589 100644 --- a/tests/webfiori/tests/cli/RunnerTest.php +++ b/tests/webfiori/tests/cli/RunnerTest.php @@ -11,7 +11,7 @@ use webfiori\cli\commands\HelpCommand; use webfiori\tests\cli\testCommands\WithExceptionCommand; use webfiori\tests\cli\testCommands\Command01; -use webfiori\cli\CommandArgument; +use webfiori\cli\Argument; /** * Description of RunnerTest * @@ -48,7 +48,7 @@ public function testRunner00() { $this->assertNull($runner->getDefaultCommand()); $this->assertNull($runner->getActiveCommand()); - $argObj = new CommandArgument('--ansi'); + $argObj = new Argument('--ansi'); $this->assertFalse($runner->addArgument($argObj)); $this->assertTrue($runner->addArg('global-arg', [ From e6cb605ea1b82acbea09ccccdf309bf020b3877c Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:07 -0800 Subject: [PATCH 07/14] Update Command01.php --- tests/webfiori/tests/cli/testCommands/Command01.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/webfiori/tests/cli/testCommands/Command01.php b/tests/webfiori/tests/cli/testCommands/Command01.php index b11e24d..a4c8137 100644 --- a/tests/webfiori/tests/cli/testCommands/Command01.php +++ b/tests/webfiori/tests/cli/testCommands/Command01.php @@ -2,7 +2,7 @@ namespace webfiori\tests\cli\testCommands; use webfiori\cli\CLICommand; -use webfiori\cli\CommandArgument; +use webfiori\cli\Argument; class Command01 extends CLICommand { public function __construct() { @@ -10,7 +10,7 @@ public function __construct() { 'arg-1' => [ ], - new CommandArgument('arg-2'), + new Argument('arg-2'), 'arg-3' => [ 'default' => 'Hello' ] From e481243aacf092976f113be54d7900e3c261c64e Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:14 -0800 Subject: [PATCH 08/14] Create Argument.php --- webfiori/cli/Argument.php | 277 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 webfiori/cli/Argument.php diff --git a/webfiori/cli/Argument.php b/webfiori/cli/Argument.php new file mode 100644 index 0000000..611700d --- /dev/null +++ b/webfiori/cli/Argument.php @@ -0,0 +1,277 @@ +setName($name)) { + $this->name = 'arg'; + } + $this->isOptional = $optional; + $this->allowedValues = []; + $this->default = ''; + $this->description = $description; + } + /** + * Adds a value to the set of allowed argument values. + * + * @param string $val A string that represents the value. + * + * @return Argument The method will return the same instance at which + * the method was called on. + */ + public function addAllowedValue(string $val) : Argument { + $trim = trim($val); + + if (!in_array($trim, $this->getAllowedValues())) { + $this->allowedValues[] = $trim; + } + + return $this; + } + /** + * Creates an instance of the class provided its name and a set of options. + * + * @param string $name The name of the command such as 'help' + * + * @param array $options An associative array of options which is used to + * configure created instance. Supported options are: + *
    + *
  • optional: A boolean. if set to true, it means that the argument + * is optional and can be ignored when running the command.
  • + *
  • default: An optional default value for the argument + * to use if it is not provided and is optional.
  • + *
  • description: A description of the argument which + * will be shown if the command 'help' is executed.
  • + *
  • values: A set of values that the argument can have. If provided, + * only the values on the list will be allowed. Note that if null or empty string + * is in the array, it will be ignored. Also, if boolean values are + * provided, true will be converted to the string 'y' and false will + * be converted to the string 'n'.
  • + *
+ * + * @return Argument|null If the instance is created, the method will + * return it as an object. Other than that, null is returned. + */ + public static function create(string $name, array $options) { + if (strlen($name) == 0) { + return null; + } + $arg = new Argument($name); + + if ($arg->getName() == 'arg') { + return null; + } + + if (isset($options[Option::OPTIONAL])) { + $arg->setIsOptional($options[Option::OPTIONAL]); + } + $desc = isset($options[Option::DESCRIPTION]) ? trim($options[Option::DESCRIPTION]) : ''; + + if (strlen($desc) != 0) { + $arg->setDescription($desc); + } else { + $arg->setDescription(''); + } + $allowedValues = $options[Option::VALUES] ?? []; + + foreach ($allowedValues as $val) { + $arg->addAllowedValue($val); + } + + + if (isset($options[Option::DEFAULT]) && gettype($options[Option::DEFAULT]) == 'string') { + $arg->setDefault($options[Option::DEFAULT]); + } + + return $arg; + } + /** + * Extract the value of an argument give its name. + * + * @param string $argName The name of the argument as provided + * in the terminal. + * + * @return string|null If the argument is provided and its value is set, + * the method will return its value. If provided without any value, + * the method will return empty string. If not provided, null is returned. + */ + public static function extractValue(string $argName, Runner $runner = null) { + $trimmedOptName = trim($argName); + + if ($runner !== null) { + $argsV = $runner->getArgsVector(); + } else { + $argsV = $_SERVER['argv']; + } + + foreach ($argsV as $option) { + $optionClean = filter_var($option); + $optExpl = explode('=', $optionClean); + $optionNameFromCLI = $optExpl[0]; + + if ($optionNameFromCLI == $trimmedOptName) { + if (count($optExpl) == 2) { + return trim(trim(trim($optExpl[1],'"'), "'")); + } else { + //If arg is provided, set its value empty string + return ''; + } + } + } + + return null; + } + /** + * Returns an array that contains all allowed argument values. + * + * @return array An array that contains all allowed argument values. + */ + public function getAllowedValues() : array { + return $this->allowedValues; + } + /** + * Returns the default value of the argument. + * + * @return string The default value of the argument. Default return value is + * empty string. + */ + public function getDefault() : string { + return $this->default; + } + /** + * Returns a string that represents the description of the argument. + * + * The value is used by the command 'help' to show argument help. + * + * @return string A string that represents the description of the argument. + * Default is empty string. + */ + public function getDescription() : string { + return $this->description; + } + /** + * Returns the name of the argument. + * + * + * @return string The name of the argument. Default return value is 'arg'. + */ + public function getName() : string { + return $this->name; + } + /** + * Returns the value of the argument as provided in the terminal. + * + * @return string|null If set, the method will return its value as string. + * If not set, null is returned. Note that if the argument is provided in + * terminal but its value is not set, the returned value will be empty + * string. + */ + public function getValue() { + return $this->value; + } + /** + * Checks if the argument is optional or not. + * + * @return bool If the argument is set as optional, the method will return + * true. False if not optional. Default is false. + */ + public function isOptional() : bool { + return $this->isOptional; + } + /** + * Reset the value of the argument and set it to null. + */ + public function resetValue() { + $this->value = null; + } + /** + * Sets a string as default value for the argument. + * + * @param string $default A string that will be set as default value if the + * argument is not provided in terminal. Note that the value will be trimmed. + */ + public function setDefault(string $default) { + $this->default = trim($default); + } + /** + * Sets the description of the argument. + * + * The value is used by the command 'help' to show argument help. + * + * @param string $desc A string that represents the description of the argument. + */ + public function setDescription(string $desc) { + $this->description = trim($desc); + } + /** + * Make the argument as optional argument or mandatory. + * + * @param bool $optional True to make it optional. False to make it mandatory. + */ + public function setIsOptional(bool $optional) { + $this->isOptional = $optional; + } + /** + * Sets the name of the argument. + * + * @param string $name A string such as '--config' or similar. It must be + * non-empty string and have no spaces. + * + * @return boolean If set, the method will return true. False otherwise. + */ + public function setName(string $name) : bool { + $trimmed = trim($name); + + if (strlen($trimmed) == 0 || strpos($trimmed, ' ') !== false) { + return false; + } + $this->name = $trimmed; + + return true; + } + /** + * Sets the value of the argument. + * + * Note that the method will return false only if the argument can have a + * fixed set of values and provided value is not one of them. + * + * @param string $val The value to set. Note that spaces in the provided value + * will be trimmed. + * + * @return bool If the value of the argument is set, the method will return + * true. If not, the method will return false. + */ + public function setValue(string $val) : bool { + $allowed = $this->getAllowedValues(); + + if (count($allowed) == 0 || in_array($val, $allowed)) { + $this->value = trim($val); + + return true; + } + + return false; + } +} From 711e88691e99f2afb25eecab4cf25e0abe8030e5 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:19 -0800 Subject: [PATCH 09/14] Delete CommandArgument.php --- webfiori/cli/CommandArgument.php | 272 ------------------------------- 1 file changed, 272 deletions(-) delete mode 100644 webfiori/cli/CommandArgument.php diff --git a/webfiori/cli/CommandArgument.php b/webfiori/cli/CommandArgument.php deleted file mode 100644 index b037e95..0000000 --- a/webfiori/cli/CommandArgument.php +++ /dev/null @@ -1,272 +0,0 @@ -setName($name)) { - $this->name = 'arg'; - } - $this->isOptional = $optional; - $this->allowedValues = []; - $this->default = ''; - $this->description = $description; - } - /** - * Adds a value to the set of allowed argument values. - * - * @param string $val A string that represents the value. - */ - public function addAllowedValue(string $val) { - $trim = trim($val); - - if (!in_array($trim, $this->getAllowedValues())) { - $this->allowedValues[] = $trim; - } - } - /** - * Creates an instance of the class provided its name and a set of options. - * - * @param string $name The name of the command such as 'help' - * - * @param array $options An associative array of options which is used to - * configure created instance. Supported options are: - *
    - *
  • optional: A boolean. if set to true, it means that the argument - * is optional and can be ignored when running the command.
  • - *
  • default: An optional default value for the argument - * to use if it is not provided and is optional.
  • - *
  • description: A description of the argument which - * will be shown if the command 'help' is executed.
  • - *
  • values: A set of values that the argument can have. If provided, - * only the values on the list will be allowed. Note that if null or empty string - * is in the array, it will be ignored. Also, if boolean values are - * provided, true will be converted to the string 'y' and false will - * be converted to the string 'n'.
  • - *
- * - * @return CommandArgument|null If the instance is created, the method will - * return it as an object. Other than that, null is returned. - */ - public static function create(string $name, array $options) { - if (strlen($name) == 0) { - return null; - } - $arg = new CommandArgument($name); - - if ($arg->getName() == 'arg') { - return null; - } - - if (isset($options['optional'])) { - $arg->setIsOptional($options['optional']); - } - $desc = isset($options['description']) ? trim($options['description']) : ''; - - if (strlen($desc) != 0) { - $arg->setDescription($desc); - } else { - $arg->setDescription(''); - } - $allowedValues = $options['values'] ?? []; - - foreach ($allowedValues as $val) { - $arg->addAllowedValue($val); - } - - - if (isset($options['default']) && gettype($options['default']) == 'string') { - $arg->setDefault($options['default']); - } - - return $arg; - } - /** - * Extract the value of an argument give its name. - * - * @param string $argName The name of the argument as provided - * in the terminal. - * - * @return string|null If the argument is provided and its value is set, - * the method will return its value. If provided without any value, - * the method will return empty string. If not provided, null is returned. - */ - public static function extractValue(string $argName, Runner $runner = null) { - $trimmedOptName = trim($argName); - - if ($runner !== null) { - $argsV = $runner->getArgsVector(); - } else { - $argsV = $_SERVER['argv']; - } - - foreach ($argsV as $option) { - $optionClean = filter_var($option); - $optExpl = explode('=', $optionClean); - $optionNameFromCLI = $optExpl[0]; - - if ($optionNameFromCLI == $trimmedOptName) { - if (count($optExpl) == 2) { - return trim(trim(trim($optExpl[1],'"'), "'")); - } else { - //If arg is provided, set its value empty string - return ''; - } - } - } - - return null; - } - /** - * Returns an array that contains all allowed argument values. - * - * @return array An array that contains all allowed argument values. - */ - public function getAllowedValues() : array { - return $this->allowedValues; - } - /** - * Returns the default value of the argument. - * - * @return string The default value of the argument. Default return value is - * empty string. - */ - public function getDefault() : string { - return $this->default; - } - /** - * Returns a string that represents the description of the argument. - * - * The value is used by the command 'help' to show argument help. - * - * @return string A string that represents the description of the argument. - * Default is empty string. - */ - public function getDescription() : string { - return $this->description; - } - /** - * Returns the name of the argument. - * - * - * @return string The name of the argument. Default return value is 'arg'. - */ - public function getName() : string { - return $this->name; - } - /** - * Returns the value of the argument as provided in the terminal. - * - * @return string|null If set, the method will return its value as string. - * If not set, null is returned. Note that if the argument is provided in - * terminal but its value is not set, the returned value will be empty - * string. - */ - public function getValue() { - return $this->value; - } - /** - * Checks if the argument is optional or not. - * - * @return bool If the argument is set as optional, the method will return - * true. False if not optional. Default is false. - */ - public function isOptional() : bool { - return $this->isOptional; - } - /** - * Reset the value of the argument and set it to null. - */ - public function resetValue() { - $this->value = null; - } - /** - * Sets a string as default value for the argument. - * - * @param string $default A string that will be set as default value if the - * argument is not provided in terminal. Note that the value will be trimmed. - */ - public function setDefault(string $default) { - $this->default = trim($default); - } - /** - * Sets the description of the argument. - * - * The value is used by the command 'help' to show argument help. - * - * @param string $desc A string that represents the description of the argument. - */ - public function setDescription(string $desc) { - $this->description = trim($desc); - } - /** - * Make the argument as optional argument or mandatory. - * - * @param bool $optional True to make it optional. False to make it mandatory. - */ - public function setIsOptional(bool $optional) { - $this->isOptional = $optional; - } - /** - * Sets the name of the argument. - * - * @param string $name A string such as '--config' or similar. It must be - * non-empty string and have no spaces. - * - * @return boolean If set, the method will return true. False otherwise. - */ - public function setName(string $name) : bool { - $trimmed = trim($name); - - if (strlen($trimmed) == 0 || strpos($trimmed, ' ') !== false) { - return false; - } - $this->name = $trimmed; - - return true; - } - /** - * Sets the value of the argument. - * - * Note that the method will return false only if the argument can have a - * fixed set of values and provided value is not one of them. - * - * @param string $val The value to set. Note that spaces in the provided value - * will be trimmed. - * - * @return bool If the value of the argument is set, the method will return - * true. If not, the method will return false. - */ - public function setValue(string $val) : bool { - $allowed = $this->getAllowedValues(); - - if (count($allowed) == 0 || in_array($val, $allowed)) { - $this->value = trim($val); - - return true; - } - - return false; - } -} From d9ca694be5c612b444fbdb1b40999dedea6023ac Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:26 -0800 Subject: [PATCH 10/14] Update CLICommand.php --- webfiori/cli/CLICommand.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/webfiori/cli/CLICommand.php b/webfiori/cli/CLICommand.php index f24635c..a1baf01 100644 --- a/webfiori/cli/CLICommand.php +++ b/webfiori/cli/CLICommand.php @@ -117,7 +117,7 @@ public function __construct(string $commandName, array $args = [], string $descr * */ public function addArg(string $name, array $options = []) : bool { - $toAdd = CommandArgument::create($name, $options); + $toAdd = Argument::create($name, $options); if ($toAdd === null) { return false; @@ -150,7 +150,7 @@ public function addArgs(array $arr) { $this->commandArgs = []; foreach ($arr as $optionName => $options) { - if ($options instanceof CommandArgument) { + if ($options instanceof Argument) { $this->addArgument($options); } else { $this->addArg($optionName, $options); @@ -160,13 +160,13 @@ public function addArgs(array $arr) { /** * Adds new command argument. * - * @param CommandArgument $arg The argument that will be added. + * @param Argument $arg The argument that will be added. * * @return bool If the argument is added, the method will return true. * If not, false is returned. The argument will not be added only if an argument * which has same name is added. */ - public function addArgument(CommandArgument $arg) : bool { + public function addArgument(Argument $arg) : bool { if (!$this->hasArg($arg->getName())) { $this->commandArgs[] = $arg; @@ -352,7 +352,7 @@ public abstract function exec() : int; * * @param string $name The name of command argument. * - * @return CommandArgument|null If the command has an argument with the + * @return Argument|null If the command has an argument with the * given name, it will be returned. Other than that, null is returned. */ public function getArg(string $name) { @@ -410,7 +410,7 @@ public function getArgValue(string $optionName) { return $arg->getValue(); } - return CommandArgument::extractValue($trimmedOptName, $this->getOwner()); + return Argument::extractValue($trimmedOptName, $this->getOwner()); } return null; From 287b9187ff55104cbb31b2c45d43c8ad87dddcf4 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:31 -0800 Subject: [PATCH 11/14] Update HelpCommand.php --- webfiori/cli/commands/HelpCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webfiori/cli/commands/HelpCommand.php b/webfiori/cli/commands/HelpCommand.php index 772bc7a..a0e2c07 100644 --- a/webfiori/cli/commands/HelpCommand.php +++ b/webfiori/cli/commands/HelpCommand.php @@ -2,7 +2,7 @@ namespace webfiori\cli\commands; use webfiori\cli\CLICommand; -use webfiori\cli\CommandArgument; +use webfiori\cli\Argument; /** * A class that implements a basic help command. @@ -75,7 +75,7 @@ private function getMaxCommandNameLen() : int { return $len; } - private function printArg(CommandArgument $argObj, $spaces = 25) { + private function printArg(Argument $argObj, $spaces = 25) { $this->prints(" %".$spaces."s:", $argObj->getName(), [ 'bold' => true, 'color' => 'yellow' From 197dbf91076f8a4ea96c22bfeea6f1cb9c465295 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:46:36 -0800 Subject: [PATCH 12/14] Create Option.php --- webfiori/cli/Option.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 webfiori/cli/Option.php diff --git a/webfiori/cli/Option.php b/webfiori/cli/Option.php new file mode 100644 index 0000000..2bb5b04 --- /dev/null +++ b/webfiori/cli/Option.php @@ -0,0 +1,30 @@ + Date: Thu, 15 Feb 2024 14:46:42 -0800 Subject: [PATCH 13/14] Update Runner.php --- webfiori/cli/Runner.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/webfiori/cli/Runner.php b/webfiori/cli/Runner.php index a360cec..91fea0e 100644 --- a/webfiori/cli/Runner.php +++ b/webfiori/cli/Runner.php @@ -78,8 +78,8 @@ public function __construct() { $this->afterRunPool = []; $this->addArg('--ansi', [ - 'optional' => true, - 'description' => 'Force the use of ANSI output.' + Option::OPTIONAL => true, + Option::DESCRIPTION => 'Force the use of ANSI output.' ]); $this->setBeforeStart(function (Runner $r) { @@ -122,7 +122,7 @@ public function __construct() { * @since 1.0 */ public function addArg(string $name, array $options = []) : bool { - $toAdd = CommandArgument::create($name, $options); + $toAdd = Argument::create($name, $options); if ($toAdd === null) { return false; @@ -136,12 +136,12 @@ public function addArg(string $name, array $options = []) : bool { * Global arguments are set of arguments that will be added automatically * to any command which is registered by the runner. * - * @param CommandArgument $arg An object that holds argument info. + * @param Argument $arg An object that holds argument info. * * @return bool If the argument is added, the method will return true. * Other than that, false is returned. */ - public function addArgument(CommandArgument $arg) : bool { + public function addArgument(Argument $arg) : bool { if (!$this->hasArg($arg->getName())) { $this->globalArgs[] = $arg; From ae7c9ed5e739d8f45181b79905e2c40e1fe43ce9 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 15 Feb 2024 14:55:57 -0800 Subject: [PATCH 14/14] Run CS Fixer --- bin/InitAppCommand.php | 42 +++++++++++++++------------ bin/app.php | 10 +++---- webfiori/cli/Argument.php | 2 +- webfiori/cli/CLICommand.php | 4 +-- webfiori/cli/Option.php | 11 ++++--- webfiori/cli/Runner.php | 20 ++++++------- webfiori/cli/commands/HelpCommand.php | 2 +- 7 files changed, 48 insertions(+), 43 deletions(-) diff --git a/bin/InitAppCommand.php b/bin/InitAppCommand.php index 2a1dec4..5ed5b00 100644 --- a/bin/InitAppCommand.php +++ b/bin/InitAppCommand.php @@ -1,7 +1,7 @@ getArgValue('--dir'); $entry = $this->getArgValue('--entry'); + if ($entry === null) { $entry = $dirName; } - - + + if (defined('ROOT_DIR')) { $appPath = ROOT_DIR.DIRECTORY_SEPARATOR.$dirName; } else { $appPath = substr(__DIR__, 0, strlen(__DIR__) - strlen('vendor\webfiori\cli\bin')).$dirName; } - + try { $this->createAppClass($appPath, $dirName); $this->createEntryPoint($appPath, $dirName, $entry); $this->success('App created successfully.'); + return 0; } catch (Exception $ex) { $this->error('Unable to initialize due to an exception:'); $this->println($ex->getCode().' - '.$ex->getMessage()); + return -1; } } - private function createEntryPoint(string $appPath, string $dir, string $eName) { - $this->println('Creating "'.$dir.'/'.$eName.'"...'); - $file = new File($eName, $appPath); - if (!$file->isExist()) { - $data = "#!/usr/bin/env php\n" - ."create(true); - file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data); - return true; - } - $this->warning('File '.$eName.' already exist!'); - } private function createAppClass(string $appPath, string $dirName) { $this->println('Creating "'.$dirName.'/app.php"...'); $file = new File($appPath.DIRECTORY_SEPARATOR.'app.php'); + if (!$file->isExist()) { $file->append("append("namespace $dirName;\n\n"); @@ -74,8 +64,24 @@ private function createAppClass(string $appPath, string $dirName) { $file->append("exit(\$runner->start());\n\n"); $file->create(true); $file->write(false); + return true; } $this->warning('File app.php already exist!'); } + private function createEntryPoint(string $appPath, string $dir, string $eName) { + $this->println('Creating "'.$dir.'/'.$eName.'"...'); + $file = new File($eName, $appPath); + + if (!$file->isExist()) { + $data = "#!/usr/bin/env php\n" + ."create(true); + file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data); + + return true; + } + $this->warning('File '.$eName.' already exist!'); + } } diff --git a/bin/app.php b/bin/app.php index e32397c..814187e 100644 --- a/bin/app.php +++ b/bin/app.php @@ -1,13 +1,13 @@ register(new HelpCommand()); -$runner->register(new InitAppCommand()); -$runner->setDefaultCommand('help'); -exit($runner->start()); \ No newline at end of file +exit($runner->register(new HelpCommand()) + ->register(new InitAppCommand()) + ->setDefaultCommand('help') + ->start()); diff --git a/webfiori/cli/Argument.php b/webfiori/cli/Argument.php index 611700d..161140c 100644 --- a/webfiori/cli/Argument.php +++ b/webfiori/cli/Argument.php @@ -47,7 +47,7 @@ public function addAllowedValue(string $val) : Argument { if (!in_array($trim, $this->getAllowedValues())) { $this->allowedValues[] = $trim; } - + return $this; } /** diff --git a/webfiori/cli/CLICommand.php b/webfiori/cli/CLICommand.php index a1baf01..9dc6563 100644 --- a/webfiori/cli/CLICommand.php +++ b/webfiori/cli/CLICommand.php @@ -210,7 +210,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom $this->moveCursorLeft($numberOfCols + 1); } } - + return $this; } /** @@ -224,7 +224,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom */ public function clearConsole() : CLICommand { $this->prints("\ec"); - + return $this; } /** diff --git a/webfiori/cli/Option.php b/webfiori/cli/Option.php index 2bb5b04..bfb5e3c 100644 --- a/webfiori/cli/Option.php +++ b/webfiori/cli/Option.php @@ -1,5 +1,4 @@ commands[$cliCommand->getName()] = $cliCommand; - + return $this; } /** @@ -348,7 +348,7 @@ public function reset() : Runner { $this->inputStream = new StdIn(); $this->outputStream = new StdOut(); $this->commands = []; - + return $this; } /** @@ -444,7 +444,7 @@ public function setActiveCommand(CLICommand $c = null) : Runner { $this->getActiveCommand()->setInputStream($this->getInputStream()); $this->getActiveCommand()->setOwner($this); } - + return $this; } /** @@ -467,7 +467,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner { 'func' => $func, 'params' => $params ]; - + return $this; } /** @@ -488,7 +488,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner { */ public function setArgsVector(array $argsVector) : Runner { $this->argsV = $argsVector; - + return $this; } /** @@ -505,7 +505,7 @@ public function setArgsVector(array $argsVector) : Runner { */ public function setBeforeStart(callable $func) : Runner { $this->beforeStartPool[] = $func; - + return $this; } /** @@ -524,7 +524,7 @@ public function setDefaultCommand(string $commandName) : Runner { if ($c !== null) { $this->defaultCommand = $c; } - + return $this; } /** @@ -547,7 +547,7 @@ public function setDefaultCommand(string $commandName) : Runner { public function setInputs(array $inputs = []) : Runner { $this->setInputStream(new ArrayInputStream($inputs)); $this->setOutputStream(new ArrayOutputStream()); - + return $this; } @@ -561,7 +561,7 @@ public function setInputs(array $inputs = []) : Runner { */ public function setInputStream(InputStream $stream) : Runner { $this->inputStream = $stream; - + return $this; } /** @@ -574,7 +574,7 @@ public function setInputStream(InputStream $stream) : Runner { */ public function setOutputStream(OutputStream $stream) : Runner { $this->outputStream = $stream; - + return $this; } /** diff --git a/webfiori/cli/commands/HelpCommand.php b/webfiori/cli/commands/HelpCommand.php index a0e2c07..ecce842 100644 --- a/webfiori/cli/commands/HelpCommand.php +++ b/webfiori/cli/commands/HelpCommand.php @@ -1,8 +1,8 @@