• 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。

    字符参数后面可以:

    1. 没有冒号(比如:h):不需要提供value,有提供也不会读取,用来判断有没有设置了这个参数。
    2. 有一个冒号(比如:p:):需要提供value,没有提供value就不读取这个参数。可以按这几种方式来设置值:-p 123–p123-p=123
    3. 有两个冒号(比如:f::):value是可选的,有没有设置value都读取(没有设置时读取的value为false),value需要连在参数后面,不能用空格分隔开。可以按这几种方式来设置值:-f=123-f123

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

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

  • 相关阅读:
    sublime text3 常用插件安装
    eclipse 快捷键
    数组去重及数组的prototype原型
    如何用angularjs给从后台传来数据添加链接
    【转载】Java Web的web.xml文件作用及基本配置
    【转】数据库几种范式
    Socket编程
    进程间的五种通信方式介绍
    进程与线程的区别
    【转载】程序员必知之浮点数运算原理详解
  • 原文地址:https://www.cnblogs.com/moxiaoping/p/8227836.html
Copyright © 2020-2023  润新知