Skip to content

Commit 404de3d

Browse files
committed
refactor(parser): use normalizer instead
1 parent 33aaae7 commit 404de3d

File tree

1 file changed

+10
-53
lines changed

1 file changed

+10
-53
lines changed

src/Input/Parser.php

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ahc\Cli\Input;
44

5+
use Ahc\Cli\Helper\Normalizer;
6+
57
/**
68
* Argv parser for the cli.
79
*
@@ -15,6 +17,9 @@ abstract class Parser
1517
/** @var string|null The last seen variadic option name */
1618
protected $_lastVariadic;
1719

20+
/** @var Normalizer */
21+
protected $_normalizer;
22+
1823
/** @var Option[] Registered options */
1924
private $_options = [];
2025

@@ -35,9 +40,11 @@ abstract class Parser
3540
*/
3641
public function parse(array $argv): self
3742
{
43+
$this->_normalizer = new Normalizer;
44+
3845
\array_shift($argv);
3946

40-
$argv = $this->normalize($argv);
47+
$argv = $this->_normalizer->normalizeArgs($argv);
4148
$count = \count($argv);
4249
$literal = false;
4350

@@ -58,37 +65,12 @@ public function parse(array $argv): self
5865
return $this;
5966
}
6067

61-
/**
62-
* Normalize argv args. Like splitting `-abc` and `--xyz=...`.
63-
*
64-
* @param array $args
65-
*
66-
* @return array
67-
*/
68-
protected function normalize(array $args): array
69-
{
70-
$normalized = [];
71-
72-
foreach ($args as $arg) {
73-
if (\preg_match('/^\-\w{2,}/', $arg)) {
74-
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
75-
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
76-
} elseif (\preg_match('/^\-\-\w{2,}\=/', $arg)) {
77-
$normalized = \array_merge($normalized, explode('=', $arg));
78-
} else {
79-
$normalized[] = $arg;
80-
}
81-
}
82-
83-
return $normalized;
84-
}
85-
8668
/**
8769
* Parse single arg.
8870
*
8971
* @param string $arg
9072
*
91-
* @return void
73+
* @return mixed
9274
*/
9375
protected function parseArgs(string $arg)
9476
{
@@ -183,36 +165,11 @@ abstract protected function emit(string $event, $value = null);
183165
protected function setValue(Parameter $parameter, string $value = null): bool
184166
{
185167
$name = $parameter->attributeName();
186-
$value = $this->prepareValue($parameter, $value);
168+
$value = $this->_normalizer->normalizeValue($parameter, $value);
187169

188170
return $this->set($name, $value);
189171
}
190172

191-
/**
192-
* Prepares value as per context and runs thorugh filter if possible.
193-
*
194-
* @param Parameter $parameter
195-
* @param string|null $value
196-
*
197-
* @return mixed
198-
*/
199-
protected function prepareValue(Parameter $parameter, string $value = null)
200-
{
201-
if (\is_bool($default = $parameter->default())) {
202-
return !$default;
203-
}
204-
205-
if ($parameter->variadic()) {
206-
return (array) $value;
207-
}
208-
209-
if (null === $value) {
210-
return $parameter->required() ? null : true;
211-
}
212-
213-
return $parameter->filter($value);
214-
}
215-
216173
/**
217174
* Set a raw value.
218175
*

0 commit comments

Comments
 (0)