From b1af6cab4e2ccd76757fa57f37ae176da74b0750 Mon Sep 17 00:00:00 2001 From: Simon Krull Date: Wed, 5 Jun 2024 16:33:02 +0200 Subject: [PATCH 1/4] FEATURE: Introduce help flag option for existing CLI commands --- Neos.Flow/Classes/Cli/RequestBuilder.php | 8 ++++++++ Neos.Flow/Classes/Command/HelpCommandController.php | 2 ++ 2 files changed, 10 insertions(+) diff --git a/Neos.Flow/Classes/Cli/RequestBuilder.php b/Neos.Flow/Classes/Cli/RequestBuilder.php index f017f23677..79c9925f49 100644 --- a/Neos.Flow/Classes/Cli/RequestBuilder.php +++ b/Neos.Flow/Classes/Cli/RequestBuilder.php @@ -118,6 +118,14 @@ public function build($commandLine): Request $request = new Request(); $request->setControllerObjectName(HelpCommandController::class); + // @TODO: Can this be implemented even better/cleaner? + if (in_array('--help', $commandLine)) { + $request->setControllerCommandName('help'); + $request->setArguments(['commandIdentifier' => $commandLine[0]]); + $request->setControllerObjectName(HelpCommandController::class); + return $request; + } + if (is_array($commandLine) === true) { $rawCommandLineArguments = $commandLine; } else { diff --git a/Neos.Flow/Classes/Command/HelpCommandController.php b/Neos.Flow/Classes/Command/HelpCommandController.php index 924c6670a8..6711fe6ff9 100644 --- a/Neos.Flow/Classes/Command/HelpCommandController.php +++ b/Neos.Flow/Classes/Command/HelpCommandController.php @@ -215,6 +215,8 @@ protected function displayHelpForCommand(Command $command) foreach ($optionDescriptions as $optionDescription) { $this->outputLine($optionDescription); } + $this->outputLine('--help'); + $this->outputLine('Shows information about the specific command and which parameters are available'); } if ($command->getDescription() !== '') { From 3d71e45ce29d0dc853547a0c057273dd9a0a3548 Mon Sep 17 00:00:00 2001 From: Simon Krull Date: Sun, 1 Sep 2024 18:26:39 +0200 Subject: [PATCH 2/4] TASK: check if $commandLine is an array --- Neos.Flow/Classes/Cli/RequestBuilder.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Neos.Flow/Classes/Cli/RequestBuilder.php b/Neos.Flow/Classes/Cli/RequestBuilder.php index 79c9925f49..678f7086c1 100644 --- a/Neos.Flow/Classes/Cli/RequestBuilder.php +++ b/Neos.Flow/Classes/Cli/RequestBuilder.php @@ -118,8 +118,7 @@ public function build($commandLine): Request $request = new Request(); $request->setControllerObjectName(HelpCommandController::class); - // @TODO: Can this be implemented even better/cleaner? - if (in_array('--help', $commandLine)) { + if (is_array($commandLine) && in_array('--help', $commandLine, true)) { $request->setControllerCommandName('help'); $request->setArguments(['commandIdentifier' => $commandLine[0]]); $request->setControllerObjectName(HelpCommandController::class); From b9fc7eafdd5a6898d297b0e4b1f57324df9ab1ac Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:23:36 +0100 Subject: [PATCH 3/4] TASK: Restructure `--help` evaluation - allows `flow --help` - restricts that no command controller uses `$help` as custom option - remove duplicate set `HelpCommandController` - centralise $firstArgument "parsing" - respect that $commandLine could be a string Does not handle `flow cache:list --help=true` as this might be bungus --- Neos.Flow/Classes/Cli/RequestBuilder.php | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Neos.Flow/Classes/Cli/RequestBuilder.php b/Neos.Flow/Classes/Cli/RequestBuilder.php index 678f7086c1..df1ee24411 100644 --- a/Neos.Flow/Classes/Cli/RequestBuilder.php +++ b/Neos.Flow/Classes/Cli/RequestBuilder.php @@ -118,13 +118,6 @@ public function build($commandLine): Request $request = new Request(); $request->setControllerObjectName(HelpCommandController::class); - if (is_array($commandLine) && in_array('--help', $commandLine, true)) { - $request->setControllerCommandName('help'); - $request->setArguments(['commandIdentifier' => $commandLine[0]]); - $request->setControllerObjectName(HelpCommandController::class); - return $request; - } - if (is_array($commandLine) === true) { $rawCommandLineArguments = $commandLine; } else { @@ -147,14 +140,23 @@ public function build($commandLine): Request } } } - if (count($rawCommandLineArguments) === 0) { + $firstArgument = count($rawCommandLineArguments) ? trim(array_shift($rawCommandLineArguments)) : null; + if ( + $firstArgument === null + || $firstArgument === '--help' + ) { $request->setControllerCommandName('helpStub'); return $request; } - $commandIdentifier = trim(array_shift($rawCommandLineArguments)); + if (in_array('--help', $rawCommandLineArguments, true)) { + $request->setControllerCommandName('help'); + $request->setArguments(['commandIdentifier' => $firstArgument]); + return $request; + } + try { - $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); + $command = $this->commandManager->getCommandByIdentifier($firstArgument); } catch (CommandException $exception) { $request->setArgument('exception', $exception); $request->setControllerCommandName('error'); @@ -195,6 +197,9 @@ protected function parseRawCommandLineArguments(array $rawCommandLineArguments, $requiredArguments = []; $optionalArguments = []; foreach ($commandMethodParameters as $parameterName => $parameterInfo) { + if ($parameterName === 'help') { + throw new \RuntimeException(sprintf('The option --help is reserved in %s::%s', $controllerObjectName, $commandMethodName), 1730715152); + } if ($parameterInfo['optional'] === false) { $requiredArguments[strtolower($parameterName)] = [ 'parameterName' => $parameterName, From 4188d0904f1344d6aa6705183b589233eb996200 Mon Sep 17 00:00:00 2001 From: Robert Lemke Date: Mon, 4 Nov 2024 18:07:43 +0100 Subject: [PATCH 4/4] TASK: Adjust console output for --help flag --- Neos.Flow/Classes/Command/HelpCommandController.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Neos.Flow/Classes/Command/HelpCommandController.php b/Neos.Flow/Classes/Command/HelpCommandController.php index 6711fe6ff9..e3eeadbdd3 100644 --- a/Neos.Flow/Classes/Command/HelpCommandController.php +++ b/Neos.Flow/Classes/Command/HelpCommandController.php @@ -124,7 +124,7 @@ protected function displayHelpIndex() $this->outputLine('* = compile time command'); $this->outputLine(); - $this->outputLine('See "%s help " for more information about a specific command.', [$this->getFlowInvocationString()]); + $this->outputLine('Use "%s [command] --help" for more information about a command.', [$this->getFlowInvocationString()]); $this->outputLine(); } @@ -212,11 +212,10 @@ protected function displayHelpForCommand(Command $command) if (count($optionDescriptions) > 0) { $this->outputLine(); $this->outputLine('OPTIONS:'); + $optionDescriptions[] = vsprintf(' %-20s %s', ['--help', 'Shows detailed information about this command']); foreach ($optionDescriptions as $optionDescription) { $this->outputLine($optionDescription); } - $this->outputLine('--help'); - $this->outputLine('Shows information about the specific command and which parameters are available'); } if ($command->getDescription() !== '') { @@ -262,7 +261,7 @@ public function errorCommand(CommandException $exception) } $this->outputLine(); $this->outputLine('Enter "%s help" for an overview of all available commands', [$this->getFlowInvocationString()]); - $this->outputLine('or "%s help " for a detailed description of the corresponding command.', [$this->getFlowInvocationString()]); + $this->outputLine('or "%s --help" for a detailed description of the corresponding command.', [$this->getFlowInvocationString()]); $this->quit(1); }