PHP uses getopt () to parse CLI parameters

Source: Internet
Author: User

The PHP manual getopt () is described below:

/**

* 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 would be used as option characters and

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

* HYPHEN (-).

* For example, a 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 would be used as option

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

* Hyphens (-).

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

* Option--opt.

* Prior to PHP5.3.0 This parameter is only available on few systems

* @param int $optind If The Optind parameter is present and then the index where argument parsing stopped would be written to This variable.

* @return Array This function would 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 : String type, where each character is treated as an option character, and the option to match the incoming script begins with a single hyphen (-). For example, an option string "X" recognizes an option-X. Only A-Z, A-Z, and 0-9 are allowed.

Character arguments can be followed by:

    1. There is no colon (for example:h): No value is required and is provided and not read to determine if this parameter is set.
    2. There is a colon (for example:P:): You need to supply value and do not read this parameter without providing value. There are several ways to set values:-P 123,–p123,-p=123.
    3. There are two colons (for example:F::): value is optional, there is no setting value to read (value is false when not set), value needs to be appended to the parameter and cannot be separated by a space. There are several ways to set the value:-f=123,-f123.

$longopts : array type, each element in this array is used as an option string, matching the option to pass in a script with two hyphens (--). For example, the long option element "opt" identifies an option--opt.

The format of $options and $longopts is almost the same, except that $longopts need to be an array of options (each element is an option), and $options A string is required (each character is an option).

PHP uses getopt () to parse CLI parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.