您好,欢迎访问一九零五行业门户网

PHP 命令行参数解析工具类的示例代码

php 命令行参数解析工具类的示例代码
<?php /** * 命令行参数解析工具类 * @author guolinchao */ class commandline { // 临时记录短选项的选项值 private static $shortoptval = null; // options value private static $optsarr = array(); // command args private static $argsarr = array(); // 是否已解析过命令行参数 private static $isparse = false; public function construct() { if(!self::$isparse) { self::parseargs(); } } /** * 获取选项值 */ public function getoptval($opt) { if(isset(self::$optsarr[$opt])) { return self::$optsarr[$opt]; } return null; } /** * 获取命令行参数 */ public function getarg($index) { if(isset(self::$argsarr[$index])) { return self::$argsarr[$index]; } return null; } /** * 注册选项对应的回调函数, $callback 应该有一个参数, 用于接收选项值 */ public function option($opt, $callback) { // check if(!is_callable($callback)) { throw new exception(sprintf('not a valid callback function [%s].', $callback)); } if(isset(self::$optsarr[$opt])) { // call user function call_user_func($callback, self::$optsarr[$opt]); } else { throw new exception(sprintf('unknown option [%s].', $opt)); } } /** * 是否是 -s 形式的短选项 */ public static function isshortoptions($opt) { if(preg_match('/^\-([a-za-z])$/', $opt, $matchs)) { return $matchs[1]; } return false; } /** * 是否是 -hlocalhost 形式的短选项 */ public static function isshortoptionswithvalue($opt) { if(preg_match('/^\-([a-za-z])([\s]+)$/', $opt, $matchs)) { self::$shortoptval = $matchs[2]; return $matchs[1]; } return false; } /** * 是否是 --help 形式的长选项 */ public static function islongoptions($opt) { if(preg_match('/^\-\-([a-za-z0-9\-_]{2,})$/', $opt, $matchs)) { return $matchs[1]; } return false; } /** * 是否是 --options=opt_value 形式的长选项 */ public static function islongoptionswithvalue($opt) { if(preg_match('/^\-\-([a-za-z0-9\-_]{2,})(?:\=(.*?))$/', $opt, $matchs)) { $tmpv = trim($matchs[2], '"'); self::$shortoptval = empty($tmpv) ? true : $tmpv; return $matchs[1]; } return false; } /** * 是否是命令行参数 */ public static function isarg($value) { return ! preg_match('/^\-/', $value); } /** * 解析命令行参数 */ public static function parseargs() { global $argv; if(self::$isparse) { return ; } // index start from 1. $index = 1; $length = count($argv); $retargs = array('opts'=>array(), 'args'=>array()); while($index < $length) { // current value $curval = $argv[$index]; // short options or long options if( ($sp = self::isshortoptions($curval)) || ($lp = self::islongoptions($curval)) ) { // options array key $key = $sp ? $sp : $lp; // go ahead $index++; if( isset($argv[$index]) && self::isarg($argv[$index]) ) { $retargs['opts'][$key] = $argv[$index]; } else { $retargs['opts'][$key] = true; // back away $index--; } } // short options with value || long options with value else if( false !== ($key = self::isshortoptionswithvalue($curval)) || false !== ($key = self::islongoptionswithvalue($curval)) ) { $retargs['opts'][$key] = self::$shortoptval; } // command args else if( self::isarg($curval) ) { $retargs['args'][] = $curval; } // incr index $index++; } self::$optsarr = $retargs['opts']; self::$argsarr = $retargs['args']; self::$isparse = true; return $retargs; } }
用法如下:
<?php include 'commandline.php'; $args = commandline::parseargs(); print_r($args); // or $cmd = new commandline(); $cmd->option('h', function ($val){ // 处理选项 h // $val 选项值 // ... echo 'option h handler.'; });
命令行测试:
以上就是php 命令行参数解析工具类的示例代码的详细内容。
其它类似信息

推荐信息