Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[help] Improve options display. #280

Merged
merged 1 commit into from Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Application.php
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
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
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