Skip to content

Commit

Permalink
[help] Improve options display. (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Jan 18, 2018
1 parent 07a5aa8 commit 72833b3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Drupal\Console\Core\EventSubscriber\DefaultValueEventListener;
use Drupal\Console\Core\EventSubscriber\ShowGenerateChainListener;
use Drupal\Console\Core\EventSubscriber\ShowTipsListener;
Expand Down Expand Up @@ -170,6 +171,18 @@ public function doRun(InputInterface $input, OutputInterface $output)
);
}

$namespaces = $this->getNamespaces();
if (in_array($this->commandName, $namespaces)) {
$input = new ArrayInput(
[
'command' => 'list',
'namespace' => $this->commandName
]
);
$this->commandName = 'list';
$isValidCommand = true;
}

if (!$isValidCommand) {
$io->error(
sprintf(
Expand Down
4 changes: 3 additions & 1 deletion src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
$input->setOption('format', 'xml');
}
$commandName = $input->getFirstArgument()?$input->getFirstArgument():'help';
$helper = new DescriptorHelper();
$helper->describe(
$io,
Expand All @@ -62,7 +63,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'namespace' => $input->getArgument('namespace'),
'translator' => $this->getApplication()->getTranslator()
'translator' => $this->getApplication()->getTranslator(),
'command' => $this->getApplication()->find($commandName)
]
);
}
Expand Down
51 changes: 45 additions & 6 deletions src/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ protected function describeInputOption(InputOption $option, array $options = [])
*/
protected function describeInputDefinition(InputDefinition $definition, array $options = [])
{
$command_name = null;
if (array_key_exists('command', $options)) {
$command_name = $options['command']->getName();
}

$totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
foreach ($definition->getArguments() as $argument) {
$totalWidth = max($totalWidth, strlen($argument->getName()));
}

if ($definition->getArguments()) {
$this->writeText($options['translator']->trans('commands.list.messages.arguments'), $options);
$this->writeText("\n");
Expand All @@ -116,7 +122,17 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
if ($definition->getOptions()) {
$laterOptions = [];
$this->writeText($options['translator']->trans('commands.list.messages.options'), $options);

$exitOptionName = 'help';
if ($command_name === $exitOptionName) {
$exitOptionName = '';
}

foreach ($definition->getOptions() as $option) {
if ($option->getName() === $exitOptionName) {
break;
}

if (strlen($option->getShortcut()) > 1) {
$laterOptions[] = $option;
continue;
Expand Down Expand Up @@ -153,6 +169,7 @@ protected function describeCommand(Command $command, array $options = [])
$command->getSynopsis(true);
$command->getSynopsis(false);
$command->mergeApplicationDefinition(false);

$this->writeText($command->trans('commands.list.messages.usage'), $options);
foreach (array_merge([$command->getSynopsis(true)], $command->getAliases(), $command->getUsages()) as $key => $usage) {
if ($key > 0) {
Expand All @@ -165,6 +182,11 @@ protected function describeCommand(Command $command, array $options = [])
$definition = $command->getNativeDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->writeText("\n");
$options =
array_merge(
$options,
[ 'command' => $command ]
);
$this->describeInputDefinition($definition, $options);
$this->writeText("\n");
}
Expand Down Expand Up @@ -208,12 +230,29 @@ protected function describeApplication(Application $application, array $options
if ('' != $help = $application->getHelp()) {
$this->writeText("$help\n\n", $options);
}
$this->writeText($application->trans('commands.list.messages.usage'), $options);
$this->writeText($application->trans('commands.list.messages.usage-details'), $options);
$options['application'] = $application;
$this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
$this->writeText("\n");
$this->writeText("\n");
if (empty($describedNamespace)) {
$this->writeText(
$application->trans('commands.list.messages.usage'),
$options
);
$this->writeText(
$application->trans(
'commands.list.messages.usage-details'
),
$options
);
$options['application'] = $application;

$this->describeInputDefinition(
new InputDefinition(
$application->getDefinition()->getOptions()
),
$options
);
$this->writeText("\n");
$this->writeText("\n");
}

$width = $this->getColumnWidth($description->getCommands()) + 4;
if ($describedNamespace) {
$this->writeText(sprintf($application->trans('commands.list.messages.comment'), $describedNamespace), $options);
Expand Down

0 comments on commit 72833b3

Please sign in to comment.