Skip to content

Commit

Permalink
Merge pull request #3367 from crydotsnake/feature/cli-command-help-flag
Browse files Browse the repository at this point in the history
FEATURE: Introduce --help flag option for existing CLI commands
  • Loading branch information
robertlemke authored Nov 4, 2024
2 parents e86a376 + 4188d09 commit 99c3682
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 15 additions & 3 deletions Neos.Flow/Classes/Cli/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,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');
Expand Down Expand Up @@ -188,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,
Expand Down
5 changes: 3 additions & 2 deletions Neos.Flow/Classes/Command/HelpCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function displayHelpIndex()

$this->outputLine('* = compile time command');
$this->outputLine();
$this->outputLine('See "%s help <commandidentifier>" 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();
}

Expand Down Expand Up @@ -212,6 +212,7 @@ protected function displayHelpForCommand(Command $command)
if (count($optionDescriptions) > 0) {
$this->outputLine();
$this->outputLine('<b>OPTIONS:</b>');
$optionDescriptions[] = vsprintf(' %-20s %s', ['--help', 'Shows detailed information about this command']);
foreach ($optionDescriptions as $optionDescription) {
$this->outputLine($optionDescription);
}
Expand Down Expand Up @@ -260,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 <commandIdentifier>" for a detailed description of the corresponding command.', [$this->getFlowInvocationString()]);
$this->outputLine('or "%s <commandIdentifier> --help" for a detailed description of the corresponding command.', [$this->getFlowInvocationString()]);
$this->quit(1);
}

Expand Down

0 comments on commit 99c3682

Please sign in to comment.