Skip to content

Commit 6c2a2d3

Browse files
committed
Adde config
1 parent 042d67c commit 6c2a2d3

File tree

2 files changed

+112
-13
lines changed

2 files changed

+112
-13
lines changed

src/CliArgs/CliArgs.php

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,87 @@
1212

1313
class CliArgs
1414
{
15+
1516
/**
16-
* @param string|array|null $argv
17-
* @param mixed $default
18-
* @return array
17+
* @var array|null
18+
*/
19+
protected $config;
20+
21+
/**
22+
* @var array
23+
*/
24+
protected $aliases;
25+
26+
/**
27+
* @var array|null
28+
*/
29+
protected $arguments;
30+
31+
/**
32+
* @param array $config
33+
*/
34+
public function __construct(array $config = null)
35+
{
36+
$this->setConfig($config);
37+
}
38+
39+
public function setConfig(array $config = null)
40+
{
41+
$this->config = $config;
42+
if (!$config) {
43+
$this->aliases = null;
44+
return;
45+
}
46+
$this->aliases = [];
47+
foreach ($config as $key => $cfg) {
48+
$this->aliases[$key] = &$config[$key];
49+
if (isset($cfg['short'])) {
50+
$this->aliases[$cfg['short']] = &$config[$key];
51+
}
52+
$config[$key]['long'] = $key;
53+
}
54+
}
55+
56+
/**
57+
* @return array|null
58+
*/
59+
public function getArguments()
60+
{
61+
if (!$this->arguments && isset($GLOBALS['argv']) && is_array($GLOBALS['argv'])) {
62+
$this->arguments = self::parseArray($GLOBALS['argv']);
63+
}
64+
return $this->arguments;
65+
}
66+
67+
/**
68+
* @param string $arg
69+
* @return mixed
1970
*/
20-
public static function parse($argv, $default = null)
71+
public function getArg($arg)
2172
{
22-
if (!$argv) {
23-
return [];
73+
if (!$cfg = $this->getArgFromConfig($arg)) {
74+
return null;
75+
}
76+
$arguments = $this->getArguments();
77+
if (isset($cfg['long']) && isset($arguments[$cfg['long']])) {
78+
return $arguments[$cfg['long']];
2479
}
25-
if (is_array($argv)) {
26-
return static::parseArray($argv,
27-
$default);
80+
if (isset($cfg['short']) && isset($arguments[$cfg['short']])) {
81+
return $arguments[$cfg['short']];
82+
}
83+
return isset($cfg['default']) ? $cfg['default'] : null;
84+
}
85+
86+
/**
87+
* @param $arg
88+
* @return null
89+
*/
90+
protected function getArgFromConfig($arg)
91+
{
92+
if (isset($this->aliases[$arg])) {
93+
return $this->aliases[$arg];
2894
}
29-
return [];
95+
return null;
3096
}
3197

3298
/**

src/run.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,43 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11-
1211
include(__DIR__ . '/autoloader.php');
1312

1413
use CliArgs\CliArgs;
1514

16-
$res = CliArgs::parse($argv, true);
17-
var_dump($res);
15+
$config = [
16+
'help' => [
17+
'short' => 'h',
18+
'info' => 'some text',
19+
'filter' => 'bool',
20+
],
21+
'count' => [
22+
'short' => 'c',
23+
'info' => 'count of bla',
24+
'default' => 0,
25+
'filter' => 'int'
26+
],
27+
'user-id' => [
28+
'long' => 'user-id',
29+
'info' => 'Id of user',
30+
'default' => 0,
31+
'filter' => 'int',
32+
],
33+
'type' => [
34+
'short' => 't',
35+
'info' => 'type please',
36+
'default' => 'a',
37+
'filter' => ['a', 'b', 'c'],
38+
],
39+
'alias' => [
40+
'short' => 'a',
41+
'info' => 'type please',
42+
'default' => 'a',
43+
'filter' => '/^a\d{2}$/',
44+
],
45+
];
46+
$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'));

0 commit comments

Comments
 (0)