note 86616 deleted from features.commandline by cmb

From: Date: Sun, 18 Dec 2016 10:19:52 +0000
Subject: note 86616 deleted from features.commandline by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208666@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- Here's my modification of "thomas dot harding at laposte dot net" script (below) to read arguments from $argv of the form --name=VALUE and -flag. "Input": ./script.php -a arg1 --opt1 arg2 -bcde --opt2=val2 arg3 arg4 arg5 -fg --opt3 "print_r Output": Array ( [exec] => ./script.php [options] => Array ( [0] => opt1 [1] => Array ( [0] => opt2 [1] => val2 ) [2] => opt3 ) [flags] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g ) [arguments] => Array ( [0] => arg1 [1] => arg2 [2] => arg3 [3] => arg4 [4] => arg5 ) ) <?php function arguments($args ) { $ret = array( 'exec' => '', 'options' => array(), 'flags' => array(), 'arguments' => array(), ); $ret['exec'] = array_shift( $args ); while (($arg = array_shift($args)) != NULL) { // Is it a option? (prefixed with --) if ( substr($arg, 0, 2) === '--' ) { $option = substr($arg, 2); // is it the syntax '--option=argument'? if (strpos($option,'=') !== FALSE) array_push( $ret['options'], explode('=', $option, 2) ); else array_push( $ret['options'], $option ); continue; } // Is it a flag or a serial of flags? (prefixed with -) if ( substr( $arg, 0, 1 ) === '-' ) { for ($i = 1; isset($arg[$i]) ; $i++) $ret['flags'][] = $arg[$i]; continue; } // finally, it is not option, nor flag $ret['arguments'][] = $arg; continue; } return $ret; }//function arguments ?>

« previous php.notes (#208666) next »