@@ -90,19 +90,19 @@ public static function parseArgv(array $params, array $config = []): array
90
90
91
91
$ config = \array_merge ([
92
92
// List of parameters without values(bool option keys)
93
- 'noValues ' => [], // ['debug', 'h']
93
+ 'boolOpts ' => [], // ['debug', 'h']
94
94
// Whether merge short-opts and long-opts
95
95
// 'mergeOpts' => false,
96
96
// want parsed options. if not empty, will ignore no matched
97
97
'wantParsedOpts ' => [],
98
- // list of params allow array.
99
- 'arrayValues ' => [], // ['names', 'status']
98
+ // list of option allow array values .
99
+ 'arrayOpts ' => [], // ['names', 'status']
100
100
], $ config );
101
101
102
102
$ args = $ sOpts = $ lOpts = [];
103
103
// config
104
- $ noValues = \array_flip ((array )$ config ['noValues ' ]);
105
- $ arrValues = \array_flip ((array )$ config ['arrayValues ' ]);
104
+ $ boolOpts = \array_flip ((array )$ config ['boolOpts ' ]);
105
+ $ arrayOpts = \array_flip ((array )$ config ['arrayOpts ' ]);
106
106
107
107
// each() will deprecated at 7.2. so,there use current and next instead it.
108
108
// while (list(,$p) = each($params)) {
@@ -111,65 +111,69 @@ public static function parseArgv(array $params, array $config = []): array
111
111
112
112
// is options
113
113
if ($ p {0 } === '- ' ) {
114
- $ val = true ;
115
- $ opt = \substr ($ p , 1 );
114
+ $ value = true ;
115
+ $ option = \substr ($ p , 1 );
116
116
$ isLong = false ;
117
117
118
118
// long-opt: (--<opt>)
119
- if (\strpos ($ opt , '- ' ) === 0 ) {
120
- $ opt = \substr ($ opt , 1 );
119
+ if (\strpos ($ option , '- ' ) === 0 ) {
120
+ $ option = \substr ($ option , 1 );
121
121
$ isLong = true ;
122
122
123
123
// long-opt: value specified inline (--<opt>=<value>)
124
- if (\strpos ($ opt , '= ' ) !== false ) {
125
- list ($ opt , $ val ) = \explode ('= ' , $ opt , 2 );
124
+ if (\strpos ($ option , '= ' ) !== false ) {
125
+ list ($ option , $ value ) = \explode ('= ' , $ option , 2 );
126
126
}
127
127
128
128
// short-opt: value specified inline (-<opt>=<value>)
129
- } elseif (isset ($ opt {1 }) && $ opt {1 } === '= ' ) {
130
- list ($ opt , $ val ) = \explode ('= ' , $ opt , 2 );
129
+ } elseif (isset ($ option {1 }) && $ option {1 } === '= ' ) {
130
+ list ($ option , $ value ) = \explode ('= ' , $ option , 2 );
131
131
}
132
132
133
133
// check if next parameter is a descriptor or a value
134
134
$ nxt = \current ($ params );
135
135
136
136
// next elem is value. fix: allow empty string ''
137
- if ($ val === true && !isset ($ noValues [ $ opt ]) && self ::nextIsValue ($ nxt )) {
137
+ if ($ value === true && !isset ($ boolOpts [ $ option ]) && self ::nextIsValue ($ nxt )) {
138
138
// list(,$val) = each($params);
139
- $ val = $ nxt ;
139
+ $ value = $ nxt ;
140
140
\next ($ params );
141
141
142
142
// short-opt: bool opts. like -e -abc
143
- } elseif (!$ isLong && $ val === true ) {
144
- foreach (\str_split ($ opt ) as $ char ) {
143
+ } elseif (!$ isLong && $ value === true ) {
144
+ foreach (\str_split ($ option ) as $ char ) {
145
145
$ sOpts [$ char ] = true ;
146
146
}
147
147
continue ;
148
148
}
149
149
150
- $ val = self ::filterBool ($ val );
151
- $ isArray = isset ($ arrValues [ $ opt ]);
150
+ $ value = self ::filterBool ($ value );
151
+ $ isArray = isset ($ arrayOpts [ $ option ]);
152
152
153
153
if ($ isLong ) {
154
154
if ($ isArray ) {
155
- $ lOpts [$ opt ][] = $ val ;
155
+ $ lOpts [$ option ][] = $ value ;
156
156
} else {
157
- $ lOpts [$ opt ] = $ val ;
157
+ $ lOpts [$ option ] = $ value ;
158
158
}
159
159
} elseif ($ isArray ) { // short
160
- $ sOpts [$ opt ][] = $ val ;
160
+ $ sOpts [$ option ][] = $ value ;
161
161
} else { // short
162
- $ sOpts [$ opt ] = $ val ;
162
+ $ sOpts [$ option ] = $ value ;
163
163
}
164
- // arguments: param doesn't belong to any option, define it is args
164
+
165
+ continue ;
166
+ }
167
+
168
+ // parse arguments:
169
+ // - param doesn't belong to any option, define it is args
170
+
171
+ // value specified inline (<arg>=<value>)
172
+ if (\strpos ($ p , '= ' ) !== false ) {
173
+ list ($ name , $ value ) = \explode ('= ' , $ p , 2 );
174
+ $ args [$ name ] = self ::filterBool ($ value );
165
175
} else {
166
- // value specified inline (<arg>=<value>)
167
- if (\strpos ($ p , '= ' ) !== false ) {
168
- list ($ name , $ val ) = \explode ('= ' , $ p , 2 );
169
- $ args [$ name ] = self ::filterBool ($ val );
170
- } else {
171
- $ args [] = $ p ;
172
- }
176
+ $ args [] = $ p ;
173
177
}
174
178
}
175
179
@@ -183,6 +187,7 @@ public static function parseArgv(array $params, array $config = []): array
183
187
* 'arg' => 'val',
184
188
* '--lp' => 'val2',
185
189
* '--s' => 'val3',
190
+ * '-h' => true,
186
191
* ]);
187
192
* ```
188
193
* @param array $params
@@ -193,14 +198,22 @@ public static function parseArray(array $params): array
193
198
$ args = $ sOpts = $ lOpts = [];
194
199
195
200
foreach ($ params as $ key => $ val ) {
196
- if ($ key === '-- ' || $ key === '- ' ) {
201
+ if (\is_int ($ key )) { // as argument
202
+ $ args [$ key ] = $ val ;
197
203
continue ;
198
204
}
199
205
200
- if (0 === \strpos ($ key , '-- ' )) {
201
- $ lOpts [substr ($ key , 2 )] = $ val ;
202
- } elseif (\strpos ($ key , '- ' ) === 0 ) {
203
- $ sOpts [\substr ($ key , 1 )] = $ val ;
206
+ $ cleanKey = \trim ((string )$ key , '- ' );
207
+
208
+ if ('' === $ cleanKey ) { // as argument
209
+ $ args [] = $ val ;
210
+ continue ;
211
+ }
212
+
213
+ if (0 === \strpos ($ key , '-- ' )) { // long option
214
+ $ lOpts [$ cleanKey ] = $ val ;
215
+ } elseif (0 === \strpos ($ key , '- ' )) { // short option
216
+ $ sOpts [$ cleanKey ] = $ val ;
204
217
} else {
205
218
$ args [$ key ] = $ val ;
206
219
}
@@ -210,9 +223,12 @@ public static function parseArray(array $params): array
210
223
}
211
224
212
225
/**
226
+ * parse flags from a string
227
+ *
213
228
* ```php
214
229
* $result = Flags::parseString('foo --bar="foobar"');
215
230
* ```
231
+ *
216
232
* @todo ...
217
233
* @param string $string
218
234
*/
@@ -250,7 +266,7 @@ public static function filterBool($val, $enable = true)
250
266
* @param mixed $val
251
267
* @return bool
252
268
*/
253
- public static function nextIsValue (string $ val ): bool
269
+ public static function nextIsValue ($ val ): bool
254
270
{
255
271
// current() fetch error, will return FALSE
256
272
if ($ val === false ) {
0 commit comments