Skip to content

Commit 5f4d8e9

Browse files
committed
bug symfony#20704 [Console] Fix wrong handling of multiline arg/opt descriptions (ogizanagi)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix wrong handling of multiline arg/opt descriptions | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#20237, symfony#13220 (comment) | License | MIT | Doc PR | N/A ### Before <img width="1072" alt="screenshot 2016-11-30 a 19 23 17" src="https://cloud.githubusercontent.com/assets/2211145/20765428/8b622304-b732-11e6-911b-b169e9aed5fd.PNG"> ### After <img width="1074" alt="screenshot 2016-11-30 a 19 23 46" src="https://cloud.githubusercontent.com/assets/2211145/20765432/9159a53e-b732-11e6-909f-ec8107c78fed.PNG"> @rquadling and @leofeyer deserve the credit for reporting the issue and suggesting the proper fix. I've only executed it. --- <details> <summary>Show code to reproduce:</summary> ```php <?php use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; require __DIR__.'/../vendor/autoload.php'; (new Application()) ->add(new class extends Command { protected function configure() { $description = "One of:" . array_reduce(['purge', 'truncate', 'delete', 'insert', 'select'], function ($value, $previous = '') { return "$value\n- $previous"; }); $this ->setName('execute') ->addArgument('action', InputArgument::REQUIRED, $description) ->addOption('another-one', 'a', InputOption::VALUE_OPTIONAL, $description) ; } }) ->getApplication() ->run(new ArgvInput()) ; ``` </details> Commits ------- 18fc6b5 [Console] Fix wrong handling of multiline arg/opt descriptions
2 parents 5d7f4e1 + 18fc6b5 commit 5f4d8e9

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/Symfony/Component/Console/Descriptor/TextDescriptor.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ protected function describeInputArgument(InputArgument $argument, array $options
3838
}
3939

4040
$totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
41-
$spacingWidth = $totalWidth - strlen($argument->getName()) + 2;
41+
$spacingWidth = $totalWidth - strlen($argument->getName());
4242

43-
$this->writeText(sprintf(' <info>%s</info>%s%s%s',
43+
$this->writeText(sprintf(' <info>%s</info> %s%s%s',
4444
$argument->getName(),
4545
str_repeat(' ', $spacingWidth),
46-
// + 17 = 2 spaces + <info> + </info> + 2 spaces
47-
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $argument->getDescription()),
46+
// + 4 = 2 spaces before <info>, 2 spaces after </info>
47+
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()),
4848
$default
4949
), $options);
5050
}
@@ -75,13 +75,13 @@ protected function describeInputOption(InputOption $option, array $options = arr
7575
sprintf('--%s%s', $option->getName(), $value)
7676
);
7777

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

80-
$this->writeText(sprintf(' <info>%s</info>%s%s%s%s',
80+
$this->writeText(sprintf(' <info>%s</info> %s%s%s%s',
8181
$synopsis,
8282
str_repeat(' ', $spacingWidth),
83-
// + 17 = 2 spaces + <info> + </info> + 2 spaces
84-
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $option->getDescription()),
83+
// + 4 = 2 spaces before <info>, 2 spaces after </info>
84+
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()),
8585
$default,
8686
$option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
8787
), $options);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<info>argument_name</info> multiline
2-
argument description
2+
argument description
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<info>-o, --option_name=OPTION_NAME</info> multiline
2-
option description
2+
option description

0 commit comments

Comments
 (0)