入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

PHP使用getopt()解析cli参数

创建时间:2018-01-09 投稿人: 老李 浏览次数:211

PHP使用getopt()解析cli参数

广东IT优就业

广州IT培训老师讲一下,PHP使用getopt()解析cli参数。

PHP手册的getopt()介绍如下:

/**

* Gets options from the command line argument list

* @link http://php.net/manual/en/function.getopt.php

* @param string $options Each character in this string will be used as option characters and

* matched against options passed to the script starting with a single

* hyphen (-).

* For example, an option string "x" recognizes an

* option -x.

* Only a-z, A-Z and 0-9 are allowed.

* @param array $longopts [optional] An array of options. Each element in this array will be used as option

* strings and matched against options passed to the script starting with

* two hyphens (--).

* For example, an longopts element "opt" recognizes an

* option --opt.

* Prior to PHP5.3.0 this parameter was only available on few systems

* @param int $optind If the optind parameter is present, then the index where argument parsing stopped will be written to this variable.

* @return array This function will return an array of option / argument pairs or false on

* failure.

* @since 4.3.0

* @since 5.0

*/

function getopt ($options, array $longopts = null, &$optind = null) {}

$options :字符串类型,其中每个字符被当作选项字符,匹配传入脚本的选项以单个连字符(-)开头。比如,一个选项字符串 "x" 识别了一个选项 -x。 只允许 a-z、A-Z 和 0-9。

PHP使用getopt()解析cli参数

广东IT优就业

字符参数后面可以:

没有冒号(比如:h):不需要提供value,有提供也不会读取,用来判断有没有设置了这个参数。

有一个冒号(比如:p:):需要提供value,没有提供value就不读取这个参数。可以按这几种方式来设置值:-p 123,–p123,-p=123。

有两个冒号(比如:f::):value是可选的,有没有设置value都读取(没有设置时读取的value为false),value需要连在参数后面,不能用空格分隔开。可以按这几种方式来设置值:-f=123,-f123。

$longopts :数组类型,此数组中的每个元素会被作为选项字符串,匹配了以两个连字符(--)传入到脚本的选项。 例如,长选项元素 "opt" 识别了一个选项 --opt。

$options和$longopts的格式几乎是一样的,唯一的不同之处是$longopts需要是选项的数组(每个元素为一个选项),而$options需要一个字符串(每个字符是个选项)。

希望广州IT培训老师今天分享的内容对大家有所帮助。

PHP使用getopt()解析cli参数

广东IT优就业

出处:www.cnblogs.com/moxiaoping/p/8227836.html

更多IT精彩推荐:

吸金能力者-PHP工程师:http://www.ujiuye.com/zt/php/?wt.mc_id=17009338

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。