Skip to content

Commit 9616698

Browse files
committed
Added filters, cache, help
1 parent 6c2a2d3 commit 9616698

File tree

2 files changed

+134
-28
lines changed

2 files changed

+134
-28
lines changed

src/CliArgs/CliArgs.php

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
class CliArgs
1414
{
15+
const FILTER_INT = 'int';
16+
const FILTER_FLOAT = 'float';
17+
const FILTER_BOOL = 'bool';
18+
const FILTER_JSON = 'json';
19+
const FILTER_HELP = 'help';
1520

1621
/**
1722
* @var array|null
@@ -28,6 +33,11 @@ class CliArgs
2833
*/
2934
protected $arguments;
3035

36+
/**
37+
* @var array
38+
*/
39+
protected $cache = [];
40+
3141
/**
3242
* @param array $config
3343
*/
@@ -36,9 +46,13 @@ public function __construct(array $config = null)
3646
$this->setConfig($config);
3747
}
3848

49+
/**
50+
* @param array|null $config
51+
*/
3952
public function setConfig(array $config = null)
4053
{
4154
$this->config = $config;
55+
$this->cache = [];
4256
if (!$config) {
4357
$this->aliases = null;
4458
return;
@@ -73,14 +87,75 @@ public function getArg($arg)
7387
if (!$cfg = $this->getArgFromConfig($arg)) {
7488
return null;
7589
}
90+
if (array_key_exists($arg, $this->cache)) {
91+
return $this->cache[$arg];
92+
}
7693
$arguments = $this->getArguments();
94+
7795
if (isset($cfg['long']) && isset($arguments[$cfg['long']])) {
78-
return $arguments[$cfg['long']];
96+
$value = $arguments[$cfg['long']];
97+
} elseif (isset($cfg['short']) && isset($arguments[$cfg['short']])) {
98+
$value = $arguments[$cfg['short']];
99+
} elseif (isset($cfg['default'])) {
100+
return $cfg['default'];
101+
} else {
102+
return null;
79103
}
80-
if (isset($cfg['short']) && isset($arguments[$cfg['short']])) {
81-
return $arguments[$cfg['short']];
104+
105+
if (isset($cfg['filter'])) {
106+
$value = $this->filterValue($cfg['filter'], $value, isset($cfg['default']) ? $cfg['default'] : null);
107+
}
108+
109+
if (isset($cfg['long'])) {
110+
$this->cache[$cfg['long']] = $value;
82111
}
83-
return isset($cfg['default']) ? $cfg['default'] : null;
112+
if (isset($cfg['short'])) {
113+
$this->cache[$cfg['short']] = $value;
114+
}
115+
116+
return $value;
117+
}
118+
119+
/**
120+
* @param mixed $filter
121+
* @param mixed $value
122+
* @param mixed|null $default
123+
* @return mixed|null
124+
*/
125+
protected function filterValue($filter, $value, $default = null)
126+
{
127+
if (is_string($filter)) {
128+
switch ($filter) {
129+
case self::FILTER_BOOL:
130+
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
131+
132+
case self::FILTER_INT:
133+
return (int)$value;
134+
135+
case self::FILTER_FLOAT:
136+
return (float)$value;
137+
138+
case self::FILTER_JSON:
139+
return json_decode($value, true);
140+
141+
case self::FILTER_HELP:
142+
return $this->getHelp($value);
143+
144+
default:
145+
if (preg_match($filter, $value)
146+
&& preg_last_error() == PREG_NO_ERROR) {
147+
return $value;
148+
}
149+
}
150+
return $default;
151+
}
152+
if (is_array($filter)) {
153+
return in_array($value, $filter, true) ? $value : $default;
154+
}
155+
if (is_callable($filter)) {
156+
return $filter($value, $default);
157+
}
158+
return $default;
84159
}
85160

86161
/**
@@ -95,6 +170,22 @@ protected function getArgFromConfig($arg)
95170
return null;
96171
}
97172

173+
/**
174+
* @param mixed $value
175+
* @return string mixed
176+
*/
177+
protected function getHelp($value)
178+
{
179+
$lines = [];
180+
foreach ($this->config as $key => $cfg) {
181+
$lines[] = [
182+
'--' . $key . (isset($cfg['short']) ? ' or -' . $cfg['short'] : ''),
183+
isset($cfg['help']) ? $cfg['help'] : '',
184+
];
185+
}
186+
return $value;
187+
}
188+
98189
/**
99190
* @param array $argv
100191
* @param mixed $default

src/run.php

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,52 @@
1414

1515
$config = [
1616
'help' => [
17-
'short' => 'h',
18-
'info' => 'some text',
19-
'filter' => 'bool',
20-
],
17+
'short' => 'h',
18+
'help' => 'Show help',
19+
'filter' => 'help',
20+
],
2121
'count' => [
22-
'short' => 'c',
23-
'info' => 'count of bla',
24-
'default' => 0,
25-
'filter' => 'int'
26-
],
22+
'short' => 'c',
23+
'help' => 'count of bla',
24+
'default' => 0,
25+
'filter' => 'int'
26+
],
2727
'user-id' => [
28-
'long' => 'user-id',
29-
'info' => 'Id of user',
30-
'default' => 0,
31-
'filter' => 'int',
32-
],
28+
'long' => 'user-id',
29+
'help' => 'Id of user',
30+
'default' => 0,
31+
'filter' => 'int',
32+
],
3333
'type' => [
34-
'short' => 't',
35-
'info' => 'type please',
36-
'default' => 'a',
37-
'filter' => ['a', 'b', 'c'],
38-
],
34+
'short' => 't',
35+
'help' => 'type please',
36+
'default' => 'a',
37+
'filter' => ['a', 'b', 'c'],
38+
],
3939
'alias' => [
4040
'short' => 'a',
41-
'info' => 'type please',
41+
'help' => 'type please',
4242
'default' => 'a',
4343
'filter' => '/^a\d{2}$/',
4444
],
45+
'json' => [
46+
'short' => 'j',
47+
'help' => 'type please',
48+
'default' => 'a',
49+
'filter' => 'json',
50+
],
51+
'func' => [
52+
'short' => 'f',
53+
'help' => 'type please',
54+
'default' => null,
55+
'filter' => function($val, $default) { return $val * 2; },
56+
],
57+
'key' => [
58+
'short' => 'k',
59+
'default' => null,
60+
],
4561
];
4662
$CliArgs = new CliArgs($config);
47-
var_dump('c', $CliArgs->getArg('c'));
48-
var_dump('count', $CliArgs->getArg('count'));
49-
var_dump('type', $CliArgs->getArg('type'));
50-
var_dump('alias', $CliArgs->getArg('alias'));
63+
$key = $CliArgs->getArg('key');
64+
var_dump($key, $CliArgs->getArg($key));
65+

0 commit comments

Comments
 (0)