12
12
13
13
class CliArgs
14
14
{
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 ' ;
15
20
16
21
/**
17
22
* @var array|null
@@ -28,6 +33,11 @@ class CliArgs
28
33
*/
29
34
protected $ arguments ;
30
35
36
+ /**
37
+ * @var array
38
+ */
39
+ protected $ cache = [];
40
+
31
41
/**
32
42
* @param array $config
33
43
*/
@@ -36,9 +46,13 @@ public function __construct(array $config = null)
36
46
$ this ->setConfig ($ config );
37
47
}
38
48
49
+ /**
50
+ * @param array|null $config
51
+ */
39
52
public function setConfig (array $ config = null )
40
53
{
41
54
$ this ->config = $ config ;
55
+ $ this ->cache = [];
42
56
if (!$ config ) {
43
57
$ this ->aliases = null ;
44
58
return ;
@@ -73,14 +87,75 @@ public function getArg($arg)
73
87
if (!$ cfg = $ this ->getArgFromConfig ($ arg )) {
74
88
return null ;
75
89
}
90
+ if (array_key_exists ($ arg , $ this ->cache )) {
91
+ return $ this ->cache [$ arg ];
92
+ }
76
93
$ arguments = $ this ->getArguments ();
94
+
77
95
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 ;
79
103
}
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 ;
82
111
}
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 ;
84
159
}
85
160
86
161
/**
@@ -95,6 +170,22 @@ protected function getArgFromConfig($arg)
95
170
return null ;
96
171
}
97
172
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
+
98
189
/**
99
190
* @param array $argv
100
191
* @param mixed $default
0 commit comments