@@ -60,6 +60,7 @@ static function checkPackage($package){
6060 }
6161 ///will encode to utf8 on failing for bad encoding
6262 static function json_encode ($ x , $ options =0 , $ depth = 512 ){
63+ $ x = self ::deresource ($ x );
6364 $ json = json_encode ($ x , $ options , $ depth );
6465 if ($ json === false ){
6566 if (json_last_error () == JSON_ERROR_UTF8 ){
@@ -99,7 +100,7 @@ static function json_throw_on_error(){
99100 }
100101
101102
102- # turn a value into a non circular value (potentially by turning objects int)
103+ # turn a value into a non circular value
103104 static function decirculate ($ source , $ options =[], $ parents =[]){
104105
105106 #+ set the default circular value hander if not provided {
@@ -146,6 +147,23 @@ static function decirculate($source, $options=[], $parents=[]){
146147 }
147148 }
148149
150+ # remove resource varaibles by replacing them with a string returned by get_resource_type
151+ static function deresource ($ source ){
152+ if (is_array ($ source )){
153+ $ return = [];
154+ foreach ($ source as $ k =>$ v ){
155+ $ return [$ k ] = self ::deresource ($ v );
156+ }
157+ return $ return ;
158+ }else {
159+ if (is_resource ($ source )){
160+ return 'Resource Type: ' .get_resource_type ($ source );
161+ }else {
162+ return $ source ;
163+ }
164+ }
165+ }
166+
149167 /// remove circular references
150168 static function flat_json_encode ($ v , $ json_options =0 , $ max_depth =512 , $ decirculate_options =[]){
151169 return self ::json_encode (self ::decirculate ($ v , $ decirculate_options ), $ json_options , $ max_depth );
@@ -210,33 +228,63 @@ function password_verify($password, $hash){
210228 static function cli_parse_args ($ args , $ options =[]){
211229 $ options = array_merge (['default ' =>true ], $ options );
212230 $ params = [];
231+
232+ # use the options map if present
233+ $ key_get = function ($ key ) use ($ options ){
234+ if ($ options ['map ' ] && $ options ['map ' ][$ key ]){
235+ return $ options ['map ' ][$ key ];
236+ }
237+ return $ key ;
238+ };
239+
240+ # only set a default for keys that don't have previous values
241+ $ param_set_default = function ($ key ) use (&$ params , $ options ){
242+ if (!array_key_exists ($ key ,$ params )){
243+ $ params [$ key ] = $ options ['default ' ];
244+ }
245+ };
246+
247+ # clear out defaults when values are provided, and make keys arrays when multiple values provided
248+ $ param_set = function ($ key , $ value ) use (&$ params , $ options ){
249+ if (array_key_exists ($ key ,$ params ) && $ params [$ key ] !== $ options ['default ' ]){
250+ if (is_array ($ params [$ key ])){
251+ $ params [$ key ][] = $ value ;
252+ }else {
253+ $ params [$ key ] = [$ params [$ key ], $ value ];
254+ }
255+ }else {
256+ $ params [$ key ] = $ value ;
257+ }
258+ };
259+
213260 $ current_key = '' ;
214261 foreach ($ args as $ arg ){
215262 if ($ arg [0 ] == '- ' ){
216- if ($ arg [1 ] == '- ' ){
217- if (strpos ($ arg , '= ' ) !== false ){
263+ if ($ arg [1 ] == '- ' ){ # case of `--key`
264+ if (strpos ($ arg , '= ' ) !== false ){ # case of `--key=bob`
218265 list ($ key , $ value ) = explode ('= ' , $ arg );
219- $ params [ substr ($ key , 2 )] = $ value ;
220- }else {
221- $ current_key = substr ($ arg , 2 );
222- $ params [ $ current_key] = $ options [ ' default ' ] ;
266+ $ param_set ( $ key_get ( substr ($ key , 2 )), $ value) ;
267+ }else { # case of `--key bob`
268+ $ current_key = $ key_get ( substr ($ arg , 2 ) );
269+ $ param_set_default ( $ current_key) ;
223270 }
224271 }else {
225- if (strlen ($ arg ) > 2 ){
272+ if (strlen ($ arg ) > 2 ){ # case of `-abc`
226273 $ keys = str_split (substr ($ arg , 1 ));
227- $ current_key = array_pop ($ keys );
228- $ params [ $ current_key] = $ options [ ' default ' ] ;
274+ $ current_key = $ key_get ( array_pop ($ keys) );
275+ $ param_set_default ( $ current_key) ;
229276 foreach ($ keys as $ key ){
230- $ params [ $ key ] = $ options [ ' default ' ] ;
277+ $ param_set_default ( $ key_get ( $ key )) ;
231278 }
232- }else {
233- $ current_key = substr ($ arg , 1 );
234- $ params [$ current_key ] = $ options ['default ' ];
279+ }else { # case of `-a`
280+
281+ $ current_key = $ key_get (substr ($ arg , 1 ));
282+ $ param_set_default ($ current_key );
235283 }
236284 }
237285 }else {
238286 if ($ current_key ){
239- $ params [ $ current_key] = $ arg ;
287+ $ param_set ( $ current_key, $ arg) ;
240288 unset($ current_key );
241289 }else {
242290 $ params [] = $ arg ;
@@ -245,4 +293,13 @@ static function cli_parse_args($args, $options=[]){
245293 }
246294 return $ params ;
247295 }
296+ static function cli_stdin_get (){
297+ $ streams = [STDIN ];
298+ $ null = NULL ;
299+ if (stream_select ($ streams , $ null , $ null , 0 )){
300+ return stream_get_contents (STDIN );
301+ }else {
302+ return false ;
303+ }
304+ }
248305}
0 commit comments