note 86616 added to features.commandline
| From: | Anonymous at osu1 dot php dot net | Date: | Sun, 26 Oct 2008 16:52:11 +0000 |
| Subject: | note 86616 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145777@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 212.24.129.51
Probable Submitter: 85.70.163.248
----
Manual Page -- http://www.php.net/manual/en/features.commandline.php
Edit -- https://master.php.net/note/edit/86616
Del: integrated -- https://master.php.net/note/delete/86616/integrated
Del: useless -- https://master.php.net/note/delete/86616/useless
Del: bad code -- https://master.php.net/note/delete/86616/bad+code
Del: spam -- https://master.php.net/note/delete/86616/spam
Del: non-english -- https://master.php.net/note/delete/86616/non-english
Del: in docs -- https://master.php.net/note/delete/86616/in+docs
Del: other reasons-- https://master.php.net/note/delete/86616
Reject -- https://master.php.net/note/reject/86616
Search -- https://master.php.net/manage/user-notes.php