⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getopt.php

📁 Bug tracker, and reporter.
💻 PHP
📖 第 1 页 / 共 3 页
字号:
                 * @see Zend_Console_Getopt_Exception                 */                throw new Zend_Console_Getopt_Exception(                    "Option \"$o\" is being defined more than once.");            }            $this->_rules[$flag]['alias'][] = $alias;            $this->_ruleMap[$alias] = $flag;        }        return $this;    }    /**     * Define help messages for options.     *     * The parameter $help_map is an associative array     * mapping option name (short or long) to the help string.     *     * @param  array $helpMap     * @return Zend_Console_Getopt Provides a fluent interface     */    public function setHelp($helpMap)    {        foreach ($helpMap as $flag => $help)        {            if (!isset($this->_ruleMap[$flag])) {                continue;            }            $flag = $this->_ruleMap[$flag];            $this->_rules[$flag]['help'] = $help;        }        return $this;    }    /**     * Parse command-line arguments and find both long and short     * options.     *     * Also find option parameters, and remaining arguments after     * all options have been parsed.     *     * @return Zend_Console_Getopt|null Provides a fluent interface     */    public function parse()    {        if ($this->_parsed === true) {            return;        }        $argv = $this->_argv;        $this->_options = array();        $this->_remainingArgs = array();        while (count($argv) > 0) {            if ($argv[0] == '--') {                array_shift($argv);                if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {                    $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);                    break;                }            }            if (substr($argv[0], 0, 2) == '--') {                $this->_parseLongOption($argv);            } else if (substr($argv[0], 0, 1) == '-') {                $this->_parseShortOptionCluster($argv);            } else {                $this->_remainingArgs[] = array_shift($argv);            }        }        $this->_parsed = true;        return $this;    }    /**     * Parse command-line arguments for a single long option.     * A long option is preceded by a double '--' character.     * Long options may not be clustered.     *     * @param  mixed &$argv     * @return void     */    protected function _parseLongOption(&$argv)    {        $optionWithParam = ltrim(array_shift($argv), '-');        $l = explode('=', $optionWithParam);        $flag = array_shift($l);        $param = array_shift($l);        if (isset($param)) {            array_unshift($argv, $param);        }        $this->_parseSingleOption($flag, $argv);    }    /**     * Parse command-line arguments for short options.     * Short options are those preceded by a single '-' character.     * Short options may be clustered.     *     * @param  mixed &$argv     * @return void     */    protected function _parseShortOptionCluster(&$argv)    {        $flagCluster = ltrim(array_shift($argv), '-');        foreach (str_split($flagCluster) as $flag) {            $this->_parseSingleOption($flag, $argv);        }    }    /**     * Parse command-line arguments for a single option.     *     * @param  string $flag     * @param  mixed  $argv     * @throws Zend_Console_Getopt_Exception     * @return void     */    protected function _parseSingleOption($flag, &$argv)    {        if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {            $flag = strtolower($flag);        }        if (!isset($this->_ruleMap[$flag])) {            /**             * @see Zend_Console_Getopt_Exception             */            throw new Zend_Console_Getopt_Exception(                "Option \"$flag\" is not recognized.",                $this->getUsageMessage());        }        $realFlag = $this->_ruleMap[$flag];        switch ($this->_rules[$realFlag]['param']) {            case 'required':                if (count($argv) > 0) {                    $param = array_shift($argv);                    $this->_checkParameterType($realFlag, $param);                } else {                    /**                     * @see Zend_Console_Getopt_Exception                     */                    throw new Zend_Console_Getopt_Exception(                        "Option \"$flag\" requires a parameter.",                        $this->getUsageMessage());                }                break;            case 'optional':                if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') {                    $param = array_shift($argv);                    $this->_checkParameterType($realFlag, $param);                } else {                    $param = true;                }                break;            default:                $param = true;        }        $this->_options[$realFlag] = $param;    }    /**     * Return true if the parameter is in a valid format for     * the option $flag.     * Throw an exception in most other cases.     *     * @param  string $flag     * @param  string $param     * @throws Zend_Console_Getopt_Exception     * @return bool     */    protected function _checkParameterType($flag, $param)    {        $type = 'string';        if (isset($this->_rules[$flag]['paramType'])) {            $type = $this->_rules[$flag]['paramType'];        }        switch ($type) {            case 'word':                if (preg_match('/\W/', $param)) {                    /**                     * @see Zend_Console_Getopt_Exception                     */                    throw new Zend_Console_Getopt_Exception(                        "Option \"$flag\" requires a single-word parameter, but was given \"$param\".",                        $this->getUsageMessage());                }                break;            case 'integer':                if (preg_match('/\D/', $param)) {                    /**                     * @see Zend_Console_Getopt_Exception                     */                    throw new Zend_Console_Getopt_Exception(                        "Option \"$flag\" requires an integer parameter, but was given \"$param\".",                        $this->getUsageMessage());                }                break;            case 'string':            default:                break;        }        return true;    }    /**     * Define legal options using the gnu-style format.     *     * @param  string $rules     * @return void     */    protected function _addRulesModeGnu($rules)    {        $ruleArray = array();        /**         * Options may be single alphanumeric characters.         * Options may have a ':' which indicates a required string parameter.         * No long options or option aliases are supported in GNU style.         */        preg_match_all('/([a-zA-Z0-9]:?)/', $rules, $ruleArray);        foreach ($ruleArray[1] as $rule) {            $r = array();            $flag = substr($rule, 0, 1);            if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {                $flag = strtolower($flag);            }            $r['alias'][] = $flag;            if (substr($rule, 1, 1) == ':') {                $r['param'] = 'required';                $r['paramType'] = 'string';            } else {                $r['param'] = 'none';            }            $this->_rules[$flag] = $r;            $this->_ruleMap[$flag] = $flag;        }    }    /**     * Define legal options using the Zend-style format.     *     * @param  array $rules     * @throws Zend_Console_Getopt_Exception     * @return void     */    protected function _addRulesModeZend($rules)    {        foreach ($rules as $ruleCode => $helpMessage)        {            $tokens = preg_split('/([=-])/',                $ruleCode, 2, PREG_SPLIT_DELIM_CAPTURE);            $flagList = array_shift($tokens);            $delimiter = array_shift($tokens);            $paramType = array_shift($tokens);            if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {                $flagList = strtolower($flagList);            }            $flags = explode('|', $flagList);            $rule = array();            $mainFlag = $flags[0];            foreach ($flags as $flag) {                if (empty($flag)) {                    /**                     * @see Zend_Console_Getopt_Exception                     */                    throw new Zend_Console_Getopt_Exception(                        "Blank flag not allowed in rule \"$ruleCode\".");                }                if (strlen($flag) == 1) {                    if (isset($this->_ruleMap[$flag])) {                        /**                         * @see Zend_Console_Getopt_Exception                         */                        throw new Zend_Console_Getopt_Exception(                            "Option \"-$flag\" is being defined more than once.");                    }                    $this->_ruleMap[$flag] = $mainFlag;                    $rule['alias'][] = $flag;                } else {                    if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) {                        /**                         * @see Zend_Console_Getopt_Exception                         */                        throw new Zend_Console_Getopt_Exception(                            "Option \"--$flag\" is being defined more than once.");                    }                    $this->_ruleMap[$flag] = $mainFlag;                    $rule['alias'][] = $flag;                }            }            if (isset($delimiter)) {                switch ($delimiter) {                    case self::PARAM_REQUIRED:                        $rule['param'] = 'required';                        break;                    case self::PARAM_OPTIONAL:                    default:                        $rule['param'] = 'optional';                }                switch (substr($paramType, 0, 1)) {                    case self::TYPE_WORD:                        $rule['paramType'] = 'word';                        break;                    case self::TYPE_INTEGER:                        $rule['paramType'] = 'integer';                        break;                    case self::TYPE_STRING:                    default:                        $rule['paramType'] = 'string';                }            } else {                $rule['param'] = 'none';            }            $rule['help'] = $helpMessage;            $this->_rules[$mainFlag] = $rule;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -