Skip to content

Commit f5c6bfd

Browse files
authored
Merge pull request #29 from adhocore/develop
fix(option): default values for variadic options
2 parents 73adadc + e210c8f commit f5c6bfd

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
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/Parameter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public function variadic(): bool
139139
*/
140140
public function default()
141141
{
142+
if ($this->variadic()) {
143+
return (array) $this->default;
144+
}
145+
142146
return $this->default;
143147
}
144148

src/Input/Parser.php

Lines changed: 4 additions & 7 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
}
@@ -169,7 +166,7 @@ protected function setValue(Parameter $parameter, string $value = null): bool
169166
$name = $parameter->attributeName();
170167
$value = $this->_normalizer->normalizeValue($parameter, $value);
171168

172-
return $this->set($name, $value);
169+
return $this->set($name, $value, $parameter->variadic());
173170
}
174171

175172
/**
@@ -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][] = $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)