您的位置 首页 php

PHP使用getopt()解析cli参数

广东IT 优就业

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

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

/**

* Gets options from the command line argument list

* @link

* @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。

广东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培训 老师今天分享的内容对大家有所帮助。

广东IT优就业

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

更多IT精彩推荐:

吸金能力者-PHP工程师:

文章来源:智云一二三科技

文章标题:PHP使用getopt()解析cli参数

文章地址:https://www.zhihuclub.com/153632.shtml

关于作者: 智云科技

热门文章

网站地图