diff --git a/src/Application.php b/src/Application.php index e63ad21..7eaff94 100644 --- a/src/Application.php +++ b/src/Application.php @@ -27,9 +27,14 @@ public function __construct( $this->arguments = $arguments; $this->commands = $commands; + $script = null; + if($arguments) { + $script = $arguments->getScript(); + } + $this->helpCommand = new HelpCommand( $this->description, - $arguments->getScript(), + $script, $this->commands ); $this->commands []= $this->helpCommand; @@ -112,6 +117,8 @@ public function run():void { } protected function findCommandByName(string $name):Command { + +// var_dump($this->arguments);die(); foreach($this->commands as $command) { if($command->getName() !== $name) { continue; diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index 809e3fc..366cadd 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -18,7 +18,7 @@ class HelpCommand extends Command { */ public function __construct( string $applicationDescription, - string $scriptName, + string $scriptName = null, array $applicationCommandList = [] ) { $this->applicationDescription = $applicationDescription; @@ -28,10 +28,13 @@ public function __construct( } public function run(ArgumentValueList $arguments = null): void { - $command = (string)$arguments->get( - "command", - self::ALL_COMMANDS - ); + $command = null; + if($arguments) { + $command = (string)$arguments->get( + "command", + self::ALL_COMMANDS + ); + } if($command === self::ALL_COMMANDS) { $output = $this->getHelpForAllCommands(); @@ -113,7 +116,7 @@ protected function getHelpForAllCommands():string { return $output; } - protected function getHelpForCommand(string $commandName):string { + protected function getHelpForCommand(string $commandName = null):string { $output = ""; $command = null; diff --git a/test/unit/Command/CommandTest.php b/test/unit/Command/CommandTest.php index bb867c1..36870f2 100644 --- a/test/unit/Command/CommandTest.php +++ b/test/unit/Command/CommandTest.php @@ -235,7 +235,7 @@ public function testGetUsageSingleRequiredNamedParameter() { public function testGetUsageMultipleRequiredParameter() { $command = new MultipleRequiredParameterCommand(); self::assertEquals( - "Usage: multiple-required-parameter-command id name --framework|-f rinky-dink --example", + "Usage: multiple-required-parameter-command id name --framework|-f FRAMEWORK --example", $command->getUsage() ); } @@ -243,7 +243,7 @@ public function testGetUsageMultipleRequiredParameter() { public function testGetUsageComboRequiredOptionalParameter() { $command = new ComboRequiredOptionalParameterCommand(); self::assertEquals( - "Usage: combo-required-optional-parameter-command id [name] --type|-t TYPE_VALUE [--verbose|-v]", + "Usage: combo-required-optional-parameter-command id [name] --type|-t TYPE [--verbose|-v]", $command->getUsage() ); } @@ -251,7 +251,7 @@ public function testGetUsageComboRequiredOptionalParameter() { public function testGetUsageAllParameterTypes() { $command = new AllParameterTypesCommand(); self::assertEquals( - "Usage: all-parameter-types-command id [name] --type|-t TYPE_VALUE [--log|-l LOG_PATH] [--verbose|-v]", + "Usage: all-parameter-types-command id [name] --type|-t TYPE [--log|-l LOG_PATH] [--verbose|-v]", $command->getUsage() ); } diff --git a/test/unit/Command/HelpCommandTest.php b/test/unit/Command/HelpCommandTest.php deleted file mode 100644 index 0015b5e..0000000 --- a/test/unit/Command/HelpCommandTest.php +++ /dev/null @@ -1,67 +0,0 @@ -method("getName") - ->willReturn("first"); - $applicationCommandList[0]->method("getDescription") - ->willReturn("The first command"); - $applicationCommandList[1]->method("getName") - ->willReturn("second"); - $applicationCommandList[1]->method("getDescription") - ->willReturn("The second command"); - $applicationCommandList[2]->method("getName") - ->willReturn("third"); - $applicationCommandList[2]->method("getDescription") - ->willReturn("The third command"); - - /** @var Stream|MockObject $stream */ - $stream = $this->createMock(Stream::class); - $buffer = [ - Stream::OUT => [], - Stream::ERROR => [], - ]; - $stream->method("write") - ->willReturnCallback(function($message, $streamName)use(&$buffer) { - $buffer[$streamName] []= $message; - }); - - $command = new HelpCommand( - "Test application", - $applicationCommandList - ); - - $command->setOutput($stream); - $command->run(); - - self::assertEmpty($buffer[Stream::ERROR]); - self::assertNotEmpty($buffer[Stream::OUT]); - $out = implode("", $buffer[Stream::OUT]); - - self::assertRegExp( - "/•\s+first\s+The first command/", - $out - ); - self::assertRegExp( - "/•\s+second\s+The second command/", - $out - ); - self::assertRegExp( - "/•\s+third\s+The third command/", - $out - ); - } -} \ No newline at end of file diff --git a/test/unit/Helper/Command/AllParameterTypesCommand.php b/test/unit/Helper/Command/AllParameterTypesCommand.php index b6980bd..e75c8fb 100644 --- a/test/unit/Helper/Command/AllParameterTypesCommand.php +++ b/test/unit/Helper/Command/AllParameterTypesCommand.php @@ -50,6 +50,7 @@ public function getOptionalParameterList():array { true, "log", "l", + "The path of your log file", "LOG_PATH" ), new Parameter( diff --git a/test/unit/Helper/Command/ComboRequiredOptionalParameterCommand.php b/test/unit/Helper/Command/ComboRequiredOptionalParameterCommand.php index 8695884..db3a4b9 100644 --- a/test/unit/Helper/Command/ComboRequiredOptionalParameterCommand.php +++ b/test/unit/Helper/Command/ComboRequiredOptionalParameterCommand.php @@ -38,7 +38,8 @@ public function getRequiredParameterList():array { new Parameter( true, "type", - "t" + "t", + "What type of thing you are using" ), ]; } diff --git a/test/unit/Helper/Command/MultipleRequiredParameterCommand.php b/test/unit/Helper/Command/MultipleRequiredParameterCommand.php index 1de4a0b..b0b5ae0 100644 --- a/test/unit/Helper/Command/MultipleRequiredParameterCommand.php +++ b/test/unit/Helper/Command/MultipleRequiredParameterCommand.php @@ -38,7 +38,7 @@ public function getRequiredParameterList():array { true, "framework", "f", - "rinky-dink" + "The name of your framework" ), new Parameter( false,