$this->writeText('<comment>Usage:</comment>', $options);
foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
$this->writeText("\n");
- $this->writeText(' '.$usage, $options);
+ $this->writeText(' '.OutputFormatter::escape($usage), $options);
}
$this->writeText("\n");
$this->writeText("\n");
$this->writeText("\n");
- $width = $this->getColumnWidth($description->getCommands());
+ $commands = $description->getCommands();
+ $namespaces = $description->getNamespaces();
+ if ($describedNamespace && $namespaces) {
+ // make sure all alias commands are included when describing a specific namespace
+ $describedNamespaceInfo = reset($namespaces);
+ foreach ($describedNamespaceInfo['commands'] as $name) {
+ $commands[$name] = $description->getCommand($name);
+ }
+ }
+
+ // calculate max. width based on available commands per namespace
+ $width = $this->getColumnWidth(call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) {
+ return array_intersect($namespace['commands'], array_keys($commands));
+ }, $namespaces)));
if ($describedNamespace) {
$this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
$this->writeText('<comment>Available commands:</comment>', $options);
}
- // add commands by namespace
- $commands = $description->getCommands();
+ foreach ($namespaces as $namespace) {
+ $namespace['commands'] = array_filter($namespace['commands'], function ($name) use ($commands) {
+ return isset($commands[$name]);
+ });
+
+ if (!$namespace['commands']) {
+ continue;
+ }
- foreach ($description->getNamespaces() as $namespace) {
if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
$this->writeText("\n");
$this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
}
foreach ($namespace['commands'] as $name) {
- if (isset($commands[$name])) {
- $this->writeText("\n");
- $spacingWidth = $width - Helper::strlen($name);
- $command = $commands[$name];
- $commandAliases = $this->getCommandAliasesText($command);
- $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
- }
+ $this->writeText("\n");
+ $spacingWidth = $width - Helper::strlen($name);
+ $command = $commands[$name];
+ $commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : '';
+ $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
}
}
/**
* Formats command aliases to show them in the command description.
*
- * @param Command $command
- *
* @return string
*/
- private function getCommandAliasesText($command)
+ private function getCommandAliasesText(Command $command)
{
$text = '';
$aliases = $command->getAliases();
}
/**
- * @param Command[] $commands
+ * @param (Command|string)[] $commands
*
* @return int
*/
$widths = array();
foreach ($commands as $command) {
- $widths[] = Helper::strlen($command->getName());
- foreach ($command->getAliases() as $alias) {
- $widths[] = Helper::strlen($alias);
+ if ($command instanceof Command) {
+ $widths[] = Helper::strlen($command->getName());
+ foreach ($command->getAliases() as $alias) {
+ $widths[] = Helper::strlen($alias);
+ }
+ } else {
+ $widths[] = Helper::strlen($command);
}
}
- return max($widths) + 2;
+ return $widths ? max($widths) + 2 : 0;
}
/**
*
* @return int
*/
- private function calculateTotalWidthForOptions($options)
+ private function calculateTotalWidthForOptions(array $options)
{
$totalWidth = 0;
foreach ($options as $option) {