Skip to content

Commit e210c8f

Browse files
committed
fix(argument): default values for variadic args
1 parent 543782e commit e210c8f

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/Input/Argument.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,21 @@ class Argument extends Parameter
1717
*/
1818
protected function parse(string $arg)
1919
{
20-
$this->required = $arg[0] === '<';
21-
$this->variadic = \strpos($arg, '...') !== false;
22-
$this->name = $name = \str_replace(['<', '>', '[', ']', '.'], '', $arg);
20+
$this->name = $name = \str_replace(['<', '>', '[', ']', '.'], '', $arg);
2321

24-
// Format is "name:default+value1,default+value2" ('+'' => ' ')!
22+
// Format is "name:default+value1,default+value2" ('+' => ' ')!
2523
if (\strpos($name, ':') !== false) {
2624
$name = \str_replace('+', ' ', $name);
2725
list($this->name, $this->default) = \explode(':', $name, 2);
2826
}
27+
28+
$this->prepDefault();
2929
}
3030

31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function default()
31+
protected function prepDefault()
3532
{
36-
if (!$this->variadic) {
37-
return $this->default;
33+
if ($this->variadic && $this->default && !\is_array($this->default)) {
34+
$this->default = \explode(',', $this->default, 2);
3835
}
39-
40-
return null === $this->default ? [] : \explode(',', $this->default);
4136
}
4237
}

src/Input/Parser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,10 @@ protected function parseArgs(string $arg)
8484
return $this->set(null, $arg);
8585
}
8686

87-
$name = $argument->attributeName();
88-
$variadic = $argument->variadic();
89-
90-
$this->set($name, $argument->filter($arg), $variadic);
87+
$this->setValue($argument, $arg);
9188

9289
// Otherwise we will always collect same arguments again!
93-
if (!$variadic) {
90+
if (!$argument->variadic()) {
9491
\array_shift($this->_arguments);
9592
}
9693
}
@@ -186,7 +183,7 @@ protected function set($key, $value, bool $variadic = false): bool
186183
if (null === $key) {
187184
$this->_values[] = $value;
188185
} elseif ($variadic) {
189-
$this->_values[$key] = \array_merge($this->_values[$key], $value);
186+
$this->_values[$key] = \array_merge($this->_values[$key], (array) $value);
190187
} else {
191188
$this->_values[$key] = $value;
192189
}

0 commit comments

Comments
 (0)