|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Licensed under The MIT License |
|
5
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
9
|
|
|
* @author Terry Cullen - [email protected] |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* PARSE ARGUMENTS |
|
15
|
|
|
* |
|
16
|
|
|
* This command line option parser supports any combination of three types of options |
|
17
|
|
|
* [single character options (`-a -b` or `-ab` or `-c -d=dog` or `-cd dog`), |
|
18
|
|
|
* long options (`--foo` or `--bar=baz` or `--bar baz`) |
|
19
|
|
|
* and arguments (`arg1 arg2`)] and returns a simple array. |
|
20
|
|
|
* |
|
21
|
|
|
* [pfisher ~]$ php test.php --foo --bar=baz --spam eggs |
|
22
|
|
|
* ["foo"] => true |
|
23
|
|
|
* ["bar"] => "baz" |
|
24
|
|
|
* ["spam"] => "eggs" |
|
25
|
|
|
* |
|
26
|
|
|
* [pfisher ~]$ php test.php -abc foo |
|
27
|
|
|
* ["a"] => true |
|
28
|
|
|
* ["b"] => true |
|
29
|
|
|
* ["c"] => "foo" |
|
30
|
|
|
* |
|
31
|
|
|
* [pfisher ~]$ php test.php arg1 arg2 arg3 |
|
32
|
|
|
* [0] => "arg1" |
|
33
|
|
|
* [1] => "arg2" |
|
34
|
|
|
* [2] => "arg3" |
|
35
|
|
|
* |
|
36
|
|
|
* [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \ |
|
37
|
|
|
* > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s |
|
38
|
|
|
* [0] => "plain-arg" |
|
39
|
|
|
* ["foo"] => true |
|
40
|
|
|
* ["bar"] => "baz" |
|
41
|
|
|
* ["funny"] => "spam=eggs" |
|
42
|
|
|
* ["also-funny"]=> "spam=eggs" |
|
43
|
|
|
* [1] => "plain arg 2" |
|
44
|
|
|
* ["a"] => true |
|
45
|
|
|
* ["b"] => true |
|
46
|
|
|
* ["c"] => true |
|
47
|
|
|
* ["k"] => "value" |
|
48
|
|
|
* [2] => "plain arg 3" |
|
49
|
|
|
* ["s"] => "overwrite" |
|
50
|
|
|
* |
|
51
|
|
|
* Not supported: `-cd=dog`. |
|
52
|
|
|
* |
|
53
|
|
|
* @author Patrick Fisher <[email protected]> |
|
54
|
|
|
* @since August 21, 2009 |
|
55
|
|
|
* @see https://github.com/pwfisher/CommandLine.php |
|
56
|
|
|
* @see http://www.php.net/manual/en/features.commandline.php |
|
57
|
|
|
* #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008 |
|
58
|
|
|
* #78651 function getArgs($args) by B Crawford, 22-Oct-2007 |
|
59
|
|
|
* @usage $args = CommandLine::parseArgs($_SERVER['argv']); |
|
60
|
|
|
* @param null $argv |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
function parseArgs($argv) |
|
64
|
|
|
{ |
|
65
|
|
|
array_shift($argv); |
|
66
|
|
|
$out = []; |
|
67
|
|
|
for ($i = 0, $j = count($argv); $i < $j; $i++) |
|
68
|
|
|
{ |
|
69
|
|
|
$arg = $argv[$i]; |
|
70
|
|
|
// --foo --bar=baz |
|
71
|
|
|
if (mb_substr($arg, 0, 2) === '--') |
|
72
|
|
|
{ |
|
73
|
|
|
$eqPos = mb_strpos($arg, '='); |
|
74
|
|
|
// --foo |
|
75
|
|
|
if ($eqPos === false) |
|
76
|
|
|
{ |
|
77
|
|
|
$key = mb_substr($arg, 2); |
|
78
|
|
|
// --foo value |
|
79
|
|
|
if ($i + 1 < $j && $argv[$i + 1][0] !== '-') |
|
80
|
|
|
{ |
|
81
|
|
|
$value = $argv[$i + 1]; |
|
82
|
|
|
$i++; |
|
83
|
|
|
} |
|
84
|
|
|
else |
|
85
|
|
|
{ |
|
86
|
|
|
$value = isset($out[$key]) ? $out[$key] : true; |
|
87
|
|
|
} |
|
88
|
|
|
$out[$key] = $value; |
|
89
|
|
|
} |
|
90
|
|
|
// --bar=baz |
|
91
|
|
|
else |
|
92
|
|
|
{ |
|
93
|
|
|
$key = mb_substr($arg, 2, $eqPos - 2); |
|
94
|
|
|
$value = mb_substr($arg, $eqPos + 1); |
|
95
|
|
|
$out[$key] = $value; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
// -k=value -abc |
|
99
|
|
|
else if (mb_substr($arg, 0, 1) === '-') |
|
100
|
|
|
{ |
|
101
|
|
|
// -k=value |
|
102
|
|
|
$key = null; |
|
103
|
|
|
if (mb_substr($arg, 2, 1) === '=') |
|
104
|
|
|
{ |
|
105
|
|
|
$key = mb_substr($arg, 1, 1); |
|
106
|
|
|
$value = mb_substr($arg, 3); |
|
107
|
|
|
$out[$key] = $value; |
|
108
|
|
|
} |
|
109
|
|
|
// -abc |
|
110
|
|
|
else |
|
111
|
|
|
{ |
|
112
|
|
|
$chars = str_split(mb_substr($arg, 1)); |
|
113
|
|
|
foreach ($chars as $char) |
|
114
|
|
|
{ |
|
115
|
|
|
$key = $char; |
|
116
|
|
|
$value = isset($out[$key]) ? $out[$key] : true; |
|
117
|
|
|
$out[$key] = $value; |
|
118
|
|
|
} |
|
119
|
|
|
// -a value1 -abc value2 |
|
120
|
|
|
if ($key && $i + 1 < $j && $argv[$i + 1][0] !== '-') |
|
121
|
|
|
{ |
|
122
|
|
|
$out[$key] = $argv[$i + 1]; |
|
123
|
|
|
$i++; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
// plain-arg |
|
128
|
|
|
else |
|
129
|
|
|
{ |
|
130
|
|
|
$value = $arg; |
|
131
|
|
|
$out[] = $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return $out; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
function after($needle, $hayStack, $returnOrigIfNeedleNotExists=false) |
|
138
|
|
|
{ |
|
139
|
|
|
if ( !is_bool(mb_strpos($hayStack, $needle)) ) |
|
140
|
|
|
{ |
|
141
|
|
|
return mb_substr($hayStack, mb_strpos($hayStack, $needle) + mb_strlen($needle)); |
|
142
|
|
|
} |
|
143
|
|
|
return $returnOrigIfNeedleNotExists ? $hayStack : ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function before($needle, $hayStack, $returnOrigIfNeedleNotExists=false) |
|
147
|
|
|
{ |
|
148
|
|
|
$result = mb_substr($hayStack, 0, mb_strpos($hayStack, $needle)); |
|
149
|
|
|
if ( !$result && $returnOrigIfNeedleNotExists ) |
|
150
|
|
|
{ |
|
151
|
|
|
return $hayStack; |
|
152
|
|
|
} |
|
153
|
|
|
return $result; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|