Skip to content

Commit

Permalink
[Console] respect multi-character shortcuts
Browse files Browse the repository at this point in the history
The `TextDescriptor` assumed that shortcuts will only consume one space
when calculating the maximum width needed to display the option's
synopsis.
  • Loading branch information
xabbuh committed Jun 26, 2015
1 parent a0af4ef commit bd49f23
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Symfony/Component/Console/Descriptor/TextDescriptor.php
Expand Up @@ -40,7 +40,7 @@ protected function describeInputArgument(InputArgument $argument, array $options
$totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
$spacingWidth = $totalWidth - strlen($argument->getName()) + 2;

$this->writeText(sprintf(" <info>%s</info>%s%s%s",
$this->writeText(sprintf(' <info>%s</info>%s%s%s',
$argument->getName(),
str_repeat(' ', $spacingWidth),
// + 17 = 2 spaces + <info> + </info> + 2 spaces
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function describeInputOption(InputOption $option, array $options = arr

$spacingWidth = $totalWidth - strlen($synopsis) + 2;

$this->writeText(sprintf(" <info>%s</info>%s%s%s%s",
$this->writeText(sprintf(' <info>%s</info>%s%s%s%s',
$synopsis,
str_repeat(' ', $spacingWidth),
// + 17 = 2 spaces + <info> + </info> + 2 spaces
Expand Down Expand Up @@ -207,7 +207,7 @@ protected function describeApplication(Application $application, array $options
foreach ($namespace['commands'] as $name) {
$this->writeText("\n");
$spacingWidth = $width - strlen($name);
$this->writeText(sprintf(" <info>%s</info>%s%s", $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
$this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
}
}

Expand Down Expand Up @@ -266,7 +266,8 @@ private function calculateTotalWidthForOptions($options)
{
$totalWidth = 0;
foreach ($options as $option) {
$nameLength = 4 + strlen($option->getName()) + 2; // - + shortcut + , + whitespace + name + --
// "-" + shortcut + ", --" + name
$nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());

if ($option->acceptValue()) {
$valueLength = 1 + strlen($option->getName()); // = + value
Expand Down
Expand Up @@ -42,6 +42,7 @@ public static function getInputOptions()
'input_option_3' => new InputOption('option_name', 'o', InputOption::VALUE_REQUIRED, 'option description'),
'input_option_4' => new InputOption('option_name', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'option description', array()),
'input_option_5' => new InputOption('option_name', 'o', InputOption::VALUE_REQUIRED, "multiline\noption description"),
'input_option_6' => new InputOption('option_name', array('o', 'O'), InputOption::VALUE_REQUIRED, 'option with multiple shortcuts'),
);
}

Expand Down
@@ -0,0 +1 @@
{"name":"--option_name","shortcut":"-o|-O","accept_value":true,"is_value_required":true,"is_multiple":false,"description":"option with multiple shortcuts","default":null}
@@ -0,0 +1,9 @@
**option_name:**

* Name: `--option_name`
* Shortcut: `-o|-O`
* Accept value: yes
* Is value required: yes
* Is multiple: no
* Description: option with multiple shortcuts
* Default: `NULL`
@@ -0,0 +1 @@
<info>-o|O, --option_name=OPTION_NAME</info> option with multiple shortcuts
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<option name="--option_name" shortcut="-o" shortcuts="-o|-O" accept_value="1" is_value_required="1" is_multiple="0">
<description>option with multiple shortcuts</description>
<defaults/>
</option>

0 comments on commit bd49f23

Please sign in to comment.