advanced php programming,Advanced PHP Programming -- 如何理解 Console/Getopt.php 的使用 P129

开始没怎么理解short options 与 long options , 后来看了(http://pear.php.net/manual/en/package.console.console-getopt.examples.php)这个连接中的例子理解了。对“Console/Getopt.php”的了解也根据上面连接里的内容。

例子如下:

/**

* Example how to get a key-value pair array

* from command line parameters with Console_Getopt.

*

* @link http://pear.php.net/bugs/bug.php?id=13902

*/

require_once 'Console/Getopt.php';

/**

* Make a key-value array.

* Since Console_Getopt does not provide such a method,

* we implement it ourselves.

*

* @params array $params Array of parameters from Console_Getopt::getopt2()

*

* @return array key-value pair array

*/

function &condense_arguments($params)

{

$new_params = array();

foreach ($params[0] as $param) {

$new_params[$param[0]] = $param[1];

}

return $new_params;

}

$cg = new Console_Getopt();

$args = $cg->readPHPArgv();

array_shift($args);

$shortOpts = 'u:g:';

$longOpts = array('user=', 'group=');

$params = $cg->getopt2($args, $shortOpts, $longOpts);

if (PEAR::isError($params)) {

echo 'Error: ' . $params->getMessage() . "\n";

exit(1);

}

var_dump(condense_arguments($params));

/*

When called as follows:

associative.php -u jason -g argonauts

you will get this output:

array(2) {

["u"]=>

string(5) "jason"

["g"]=>

string(9) "argonauts"

}

*/

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值