Home > Backend Development > PHP Tutorial > How to parse GNU C-style command line options through getopt in PHP

How to parse GNU C-style command line options through getopt in PHP

藏色散人
Release: 2023-04-07 21:50:02
forward
2353 people have browsed it

In PHP, when we get command line parameters, we can get them by traversing $argv. In fact, there are specifications to follow, that is, GNU C-style parser for command line options.

For example, when using the command wget to download a file, you can use some of the following methods to specify options

wget http://mengkang.net/a.jpg -O b.jpg
wget http://mengkang.net/a.jpg -Ob.jpg --tries=3 -b
wget http://mengkang.net/a.jpg -Ob.jpg --tries=3 -bvd
Copy after login

Let’s sort out the rules of command line options. First, the parameters are divided into short parameter names and complete parameter names. , and some also have mapping relationships. For example, when we use wget, -O corresponds to --output-document.

Summary options usage specifications

● There is a restriction for short option names, which can only be one char character, which can only be 1 byte and cannot exceed 1 byte. For example, it is not known whether the last parameter of the fourth command above is one option or three options.

● Short option names start with a single hyphen (-)

● Short options can be one - followed by multiple option names

● Long options and short options can have Mapping relationship, there may be no

● Long option name is multi-byte, starting with two hyphens (--)

● Between options and actual parameters, you can directly Connections can also be separated by spaces, or you can use equal signs to connect

● However, the short option uses an equal sign to connect values, and the equal sign will be regarded as part of the value (but it is compatible with this in PHP) One point)

● Options are divided into no value, value must be passed, value can be passed (that is, it can be passed or not)

Use in PHP

getopt ( string $options [, array $longopts [, int &$optind ]] ) : array
https://www.php.net/manual/zh/function.getopt.php
Copy after login

$options Short parameter character list, parameter characters are followed by : to indicate that values ​​must be passed; parameter characters are followed by :: to indicate optional values; only parameter characters indicate that the parameter (or option) does not accept value passing

$longopts Since long parameters are multi-byte, they must be arrays, otherwise they cannot be separated. Long parameters also follow the above::,: rules

php lacks the support of structures. Compared with the long option configuration of c, it is more concise, but it also lacks the mapping relationship configuration of long and short options.

$shortOpts = "O:Vv::dh";
$longOpts = ["output-document:","version","verbose::", "debug", "help"]; 
$options = getopt($shortOpts, $longOpts);
var_export($options);
php getopt.php -Oa.jpg 
array (
  'O' => 'a.jpg',
)
php getopt.php -O=a.jpg
array (
  'O' => 'a.jpg',
)
php getopt.php -O a.jpg                                 
array (
  'O' => 'a.jpg',
)
php getopt.php -O=a.jpg -dhV
array (
  'O' => 'a.jpg',
  'd' => false,
  'h' => false,
  'V' => false,
)
php getopt.php -O=a.jpg -dhV -vvv
array (
  'O' => 'a.jpg',
  'd' => false,
  'h' => false,
  'V' => false,
  'v' => 'vv',
)
Copy after login

In the above example, my short parameters and long parameters correspond, but there is no data structure to represent their correspondence (there is an option structure in C to manage this correspondence), so we If both are passed, the program will receive both values, and then we can decide which one to use for the short parameter or the long parameter.

php getopt.php -O=a.jpg -dhV -vvv --output-document b.jpg --debug
array (
  'O' => 'a.jpg',
  'd' => false,
  'h' => false,
  'V' => false,
  'v' => 'vv',
  'output-document' => 'b.jpg',
  'debug' => false,
)
Copy after login

Writing like this is not standardized, so try to avoid writing like this.

php getopt.php -O=a.jpg -dhVvvv  
array (
  'O' => 'a.jpg',
  'd' => false,
  'h' => false,
  'V' => false,
  'v' => 'vv',
)
php getopt.php -O=a.jpg -dhvvvV
array (
  'O' => 'a.jpg',
  'd' => false,
  'h' => false,
  'v' => 'vvV',
)
Copy after login

Summarizes the routines used by GNU C command line options. The commands are easier to use. You will not be confused about why the cases of using various tools under Linux are written in "weird" ways.

In C, short options and values ​​are not supported to be connected with the equal sign, but in PHP, it is possible. Please note.

111

Recommended: "PHP Tutorial"

The above is the detailed content of How to parse GNU C-style command line options through getopt in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:aliyun.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template