Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60e6ccd

Browse files
committed
bug symfony#18864 [Console][DX] Fixed ambiguous error message when using a duplicate option shortcut (peterrehm)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes symfony#18864). Discussion ---------- [Console][DX] Fixed ambiguous error message when using a duplicate option shortcut | Q | A | ------------- | --- | Branch? | 2.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18856 | License | MIT | Doc PR | - I assume this should be merged into 2.3 as per @stof's comment. There is a race condition when you run a command which has a duplicate option shortcut. Simply changing the order so that Options are merged before the Arguments solves that race condition. ````php $this->setName('my:super:command') ->setAliases(['my:super:commandalias']) ->setDescription('Performs some irrelevant work.') ->addOption('survey', 'e', InputOption::VALUE_REQUIRED, 'My option with a shortcut.') ```` Gives the error message: ``` [Symfony\Component\Console\Exception\LogicException] An argument with name "command" already exists. ``` This happens as the first time the definition is merged happens here: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Application.php#L820 As this throws an error here: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Command/Command.php#L309 The commans are merged but not the options. Merging it then again when the command is run https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Command/Command.php#L217 throws an error due to the duplicate argument as the arguments already have been merged. This time the error message is not surpressed and will confuse the user. Changing the order should fix the issue for duplicate arguments as well as for duplicate options. Commits ------- 7cb7655 [Console][DX] Fixed ambiguous error message when using a duplicate option shortcut
2 parents 53b7236 + 7cb7655 commit 60e6ccd

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Symfony/Component/Console/Command/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,14 @@ public function mergeApplicationDefinition($mergeArgs = true)
287287
return;
288288
}
289289

290+
$this->definition->addOptions($this->application->getDefinition()->getOptions());
291+
290292
if ($mergeArgs) {
291293
$currentArguments = $this->definition->getArguments();
292294
$this->definition->setArguments($this->application->getDefinition()->getArguments());
293295
$this->definition->addArguments($currentArguments);
294296
}
295297

296-
$this->definition->addOptions($this->application->getDefinition()->getOptions());
297-
298298
$this->applicationDefinitionMerged = true;
299299
if ($mergeArgs) {
300300
$this->applicationDefinitionMergedWithArgs = true;

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,33 @@ public function testRunReturnsExitCodeOneForExceptionCodeZero()
645645
$this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0');
646646
}
647647

648+
/**
649+
* @expectedException \LogicException
650+
* @expectedExceptionMessage An option with shortcut "e" already exists.
651+
*/
652+
public function testAddingOptionWithDuplicateShortcut()
653+
{
654+
$dispatcher = new EventDispatcher();
655+
$application = new Application();
656+
$application->setAutoExit(false);
657+
$application->setCatchExceptions(false);
658+
$application->setDispatcher($dispatcher);
659+
660+
$application->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'Environment'));
661+
662+
$application
663+
->register('foo')
664+
->setAliases(['f'])
665+
->setDefinition(array(new InputOption('survey', 'e', InputOption::VALUE_REQUIRED, 'My option with a shortcut.')))
666+
->setCode(function (InputInterface $input, OutputInterface $output) {})
667+
;
668+
669+
$input = new ArrayInput(array('command' => 'foo'));
670+
$output = new NullOutput();
671+
672+
$application->run($input, $output);
673+
}
674+
648675
/**
649676
* @expectedException \LogicException
650677
* @dataProvider getAddingAlreadySetDefinitionElementData

0 commit comments

Comments
 (0)