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-weizijiaocheng-356649.html

a001aca1d856ce9070a95fa41dc827a3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值