Skip to content

Commit 6a4e57a

Browse files
authored
Merge pull request #22 from adhocore/develop
Develop
2 parents bca9518 + 404de3d commit 6a4e57a

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

src/Helper/Normalizer.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Ahc\Cli\Helper;
4+
5+
use Ahc\Cli\Input\Parameter;
6+
7+
/**
8+
* Internal value &/or argument normalizer. Has little to no usefulness as public api.
9+
*
10+
* @author Jitendra Adhikari <[email protected]>
11+
* @license MIT
12+
*
13+
* @link https://github.com/adhocore/cli
14+
*/
15+
class Normalizer
16+
{
17+
/**
18+
* Normalize argv args. Like splitting `-abc` and `--xyz=...`.
19+
*
20+
* @param array $args
21+
*
22+
* @return array
23+
*/
24+
public function normalizeArgs(array $args): array
25+
{
26+
$normalized = [];
27+
28+
foreach ($args as $arg) {
29+
if (\preg_match('/^\-\w{2,}/', $arg)) {
30+
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
31+
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
32+
} elseif (\preg_match('/^\-\-\w{2,}\=/', $arg)) {
33+
$normalized = \array_merge($normalized, explode('=', $arg));
34+
} else {
35+
$normalized[] = $arg;
36+
}
37+
}
38+
39+
return $normalized;
40+
}
41+
42+
/**
43+
* Normalizes value as per context and runs thorugh filter if possible.
44+
*
45+
* @param Parameter $parameter
46+
* @param string|null $value
47+
*
48+
* @return mixed
49+
*/
50+
public function normalizeValue(Parameter $parameter, string $value = null)
51+
{
52+
if (\is_bool($default = $parameter->default())) {
53+
return !$default;
54+
}
55+
56+
if ($parameter->variadic()) {
57+
return (array) $value;
58+
}
59+
60+
if (null === $value) {
61+
return $parameter->required() ? null : true;
62+
}
63+
64+
return $parameter->filter($value);
65+
}
66+
}

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)