-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLParser.php
54 lines (52 loc) · 1.24 KB
/
CLParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Copyright (c) 2020
* @file CLParser.php
* @author USPilot
* @date 2021/02/06
* @time 12:00
*
*/
namespace USPilot;
class CLParser
{
/**
* Return associative array of command-line arguments
*
* @param array $argv Command line arguments
* @return array associative array of command-line arguments
* [
* [input] - arguments (without '-')
* ['arg']=>'value' - parameters
* ]
*/
public static function arguments(array $argv)
{
$_ARG = array();
foreach ($argv as $arg)
{
if (preg_match('/^-{1,2}([a-zA-Z0-9\-_]*)=?(.*)$/', $arg, $matches))
{
$key = $matches[1];
switch ($matches[2])
{
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}
$_ARG[$key] = $arg;
}
else
{
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
}