You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #40322 [Console] improve exception message when required argument is added after an optional one (marbul)
This PR was merged into the 5.3-dev branch.
Discussion
----------
[Console] improve exception message when required argument is added after an optional one
| Q | A
| ------------- | ---
| Branch? | 5.x <!-- see below -->
| Bug fix? | no
| New feature? | wouldn't say so, rather DX improvement <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets | Fix#40302 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
| Doc PR | n/a
Hello, this is my first contribution to Symfony Framework. It's time to pull my weight.
About the issue:
I did improve an error message to include passed argument's name and the latest optional argument's name.

An author also mentioned "But which command?", however this is shown twice as we can see on the screenshot above so I skipped that.
Commits
-------
9bddbbd#40302 improve exception message when required argument is added after an optional one
Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/InputDefinition.php
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -30,8 +30,8 @@ class InputDefinition
30
30
{
31
31
private$arguments;
32
32
private$requiredCount;
33
-
private$hasAnArrayArgument = false;
34
-
private$hasOptional;
33
+
private$lastArrayArgument;
34
+
private$lastOptionalArgument;
35
35
private$options;
36
36
private$negations;
37
37
private$shortcuts;
@@ -72,8 +72,8 @@ public function setArguments(array $arguments = [])
72
72
{
73
73
$this->arguments = [];
74
74
$this->requiredCount = 0;
75
-
$this->hasOptional = false;
76
-
$this->hasAnArrayArgument = false;
75
+
$this->lastOptionalArgument = null;
76
+
$this->lastArrayArgument = null;
77
77
$this->addArguments($arguments);
78
78
}
79
79
@@ -100,22 +100,22 @@ public function addArgument(InputArgument $argument)
100
100
thrownewLogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
101
101
}
102
102
103
-
if ($this->hasAnArrayArgument) {
104
-
thrownewLogicException('Cannot add an argument after an array argument.');
103
+
if (null !== $this->lastArrayArgument) {
104
+
thrownewLogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName()));
105
105
}
106
106
107
-
if ($argument->isRequired() && $this->hasOptional) {
108
-
thrownewLogicException('Cannot add a required argument after an optional one.');
107
+
if ($argument->isRequired() && null !== $this->lastOptionalArgument) {
108
+
thrownewLogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName()));
$this->expectExceptionMessage('Cannot add a required argument after an optional one.');
116
114
$this->initializeArguments();
115
+
$this->expectException(\LogicException::class);
116
+
$this->expectExceptionMessage(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $this->foo2->getName(), $this->foo->getName()));
0 commit comments