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:
- There is no colon (for example:h): No value is required and is provided and not read to determine if this parameter is set.
- 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.
- 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