diff --git a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php index bc299216..91b1eb0c 100644 --- a/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php +++ b/tests/webfiori/framework/test/writers/CLICommandClassWriterTest.php @@ -2,6 +2,7 @@ namespace webfiori\framework\test\writers; use PHPUnit\Framework\TestCase; +use webfiori\cli\CLICommand; use webfiori\framework\writers\CLICommandClassWriter; class CLICommandClassWriterTest extends TestCase { @@ -41,11 +42,29 @@ public function test01() { $this->assertEquals('Lets-Do-It', $writer->getCommandName()); $this->assertEquals('', $writer->getDescription()); $this->assertEquals([], $writer->getArgs()); + $writer->setArgs([ + new \webfiori\cli\Argument('--do', 'A do arg', true) + ]); + $this->assertEquals('', $writer->getDescription()); + $writer->setCommandDescription('Random desc'); + $this->assertEquals('Random desc', $writer->getDescription()); $this->assertEquals([ 'webfiori\cli\CLICommand' ], $writer->getUseStatements()); $writer->writeClass(); - $this->assertTrue(class_exists($writer->getNamespace().'\\'.$writer->getName())); + $clazz = $writer->getNamespace().'\\'.$writer->getName(); + $this->assertTrue(class_exists($clazz)); $writer->removeClass(); + $clazzObj = new $clazz(); + $this->assertTrue($clazzObj instanceof CLICommand); + $this->assertEquals('Lets-Do-It', $clazzObj->getName()); + $this->assertEquals('Random desc', $clazzObj->getDescription()); + $this->assertEquals([ + '--do' + ], $clazzObj->getArgsNames()); + $arg = $clazzObj->getArg('--do'); + $this->assertTrue($arg instanceof \webfiori\cli\Argument); + $this->assertEquals('A do arg', $arg->getDescription()); + $this->assertTrue($arg->isOptional()); } } diff --git a/webfiori/framework/writers/CLICommandClassWriter.php b/webfiori/framework/writers/CLICommandClassWriter.php index 8b536fe3..511e9880 100644 --- a/webfiori/framework/writers/CLICommandClassWriter.php +++ b/webfiori/framework/writers/CLICommandClassWriter.php @@ -157,8 +157,8 @@ public function writeClassComment() { $topArr[] = ' * In addition, the command have the following args:'; $topArr[] = ' * '; } @@ -180,18 +180,19 @@ private function writeConstructor() { if (count($this->args) > 0) { $this->append(["parent::__construct('$this->name', ["], 2); - foreach ($this->args as $argArr) { - $this->append("'".$argArr['name']."' => [", 3); - - if (strlen($argArr['description']) != 0) { - $this->append("'description' => '".str_replace("'", "\'", $argArr['description'])."',", 4); + foreach ($this->args as $argObj) { + + $this->append("'".$argObj->getName()."' => [", 3); + + if (strlen($argObj->getDescription()) != 0) { + $this->append("'description' => '".str_replace("'", "\'", $argObj->getDescription())."',", 4); } - $this->append("'optional' => ".($argArr['optional'] === true ? 'true' : 'false').",", 4); + $this->append("'optional' => ".($argObj->isOptional() ? 'true' : 'false').",", 4); - if (count($argArr['values']) != 0) { + if (count($argObj->getAllowedValues()) != 0) { $this->append("'values' => [", 4); - foreach ($argArr['values'] as $val) { + foreach ($argObj->getAllowedValues() as $val) { $this->append("'".str_replace("'", "\'", $val)."',", 5); } $this->append("]", 4); @@ -200,7 +201,7 @@ private function writeConstructor() { } $this->append("], '".str_replace("'", "\'", $this->desc)."');", 2); } else { - $this->append("parent::__construct('$this->name', '".str_replace("'", "\'", $this->desc)."');", 2); + $this->append("parent::__construct('$this->name', [], '".str_replace("'", "\'", $this->desc)."');", 2); } $this->append('}', 1);