From 938012f0ea07ec80c77301e952ce0cc3ae03fdcc Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Mon, 5 Mar 2018 23:42:16 +0300 Subject: [PATCH] [Console] Improved rendering of optional arguments in command synopsis --- .../Component/Console/Input/InputDefinition.php | 14 +++++++------- .../Console/Tests/Input/InputDefinitionTest.php | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Console/Input/InputDefinition.php b/src/Symfony/Component/Console/Input/InputDefinition.php index d5b99ab396..6059b3170b 100644 --- a/src/Symfony/Component/Console/Input/InputDefinition.php +++ b/src/Symfony/Component/Console/Input/InputDefinition.php @@ -382,21 +382,21 @@ class InputDefinition $elements[] = '[--]'; } + $tail = ''; foreach ($this->getArguments() as $argument) { $element = '<'.$argument->getName().'>'; - if (!$argument->isRequired()) { - $element = '['.$element.']'; - } elseif ($argument->isArray()) { - $element = $element.' ('.$element.')'; - } - if ($argument->isArray()) { $element .= '...'; } + if (!$argument->isRequired()) { + $element = '['.$element; + $tail .= ']'; + } + $elements[] = $element; } - return implode(' ', $elements); + return implode(' ', $elements).$tail; } } diff --git a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php index 1374ddfdf8..6bebca585a 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php @@ -374,8 +374,9 @@ class InputDefinitionTest extends TestCase array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))), '', 'puts arguments in angle brackets'), array(new InputDefinition(array(new InputArgument('foo'))), '[]', 'puts optional arguments in square brackets'), - array(new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))), '[]...', 'uses an ellipsis for array arguments'), - array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))), ' ()...', 'uses parenthesis and ellipsis for required array arguments'), + array(new InputDefinition(array(new InputArgument('foo'), new InputArgument('bar'))), '[ []]', 'chains optional arguments inside brackets'), + array(new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))), '[...]', 'uses an ellipsis for array arguments'), + array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))), '...', 'uses an ellipsis for required array arguments'), array(new InputDefinition(array(new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED))), '[--foo] [--] ', 'puts [--] between options and arguments'), );