Reprinted this site article, please note, Reprinted from:
FukaiHttp://www.php-oa.com
Link to this article:
Http://www.php-oa.com/2009/04/04/perl_getopt-long.html
In Linux, we often use a program that requires adding parameters. Now let's take a look at the getopt: Long module in Perl, which is much more powerful than the array directly using @ argv. I want to know that there are two types of parameters in Linux.
- Long parameter-help
- Short parameter-H
That is,-and-respectively.-indicates the complete parameter.-indicates the simplified parameter. These two methods are also supported in this module of Perl.
This article introduces two getopt modules. One is getopt: long and the other is getopt: STD. Here we will only introduce getopt: Long. This module is more powerful.
Getopt: Long Module
Initializes parameters accepted in the Perl Command Line to simplify the parsing of command line parameters. The following is an example of a program.
#! /Usr/bin/perl
Use strict;
Use getopt: long;
Use smart: comments;
My @ libs = ();
My % flags = ();
My ($ verbose, $ all, $ more, $ diam, $ debug, $ test, $ step );
Getoptions (
'Verbose + '=> \ $ verbose,
'More! '=> \ $ More,
'Debug: I '=> \ $ debug,
'Lib = s' = >\@ libs,
'Flag = s' => \ % flags,
'Test | T' => \ $ test,
'All | everything | universe' => $ all,
);
### $ Verbose
#### $ More ###$ debug ####$ test ###@ libs ###% flags
This is the method used. The following is a detailed explanation. Note that the => above section in getoptions is used. Below is a detailed explanation
- The 'verbose + 'option is followed by the + option and does not need to be added later. You just need to use it directly. A variable will be added each time it appears, that is, the value of verbose will be changed to 2 when the "verbose-verbose" parameter appears twice in the command line.
- 'More! 'Connected! The option does not receive the variable (that is, you do not need to add the parameter-more to use it later), as long as this parameter appears in the command line, the default value is 1, is used to set to enable and disable a function>. You can add "no" before the parameter to change to "negative", for example, "-nomore.
- The 'flag = S' string must be string (s), INTEGER (I), or floating point (f.
- The 'debug: I 'option with: will accept optional variables with the default value 0 or empty string.
- The 'test | T' option is connected to |, indicating that-test can be abbreviated to-t.
- 'Lib = s' => @ libs if the associated variable is an array, such as @ libs in this region, the option can appear multiple times and the value can be pushed to the array.
- 'Flag = s' => % flags if the associated variable is a hash, a key = value (Key = value) pair is required and inserted into the hash.
Note:
When the parameter name is matched, getoptions ignores case sensitivity by default. The default parameter is abbreviated as a unique shortest string (first letter) (for example,-M indicates-more. when the first letter is the same, the second letter will be added to distinguish)
The procedure of the getopt module is as follows:
Based on the example above, for example, we wrote a program named test. pl. We only need to add the following parameters in the command line:
$./Test. pl -- verbose-V -- more \
-- Lib = '/lib'-l'/lib64' -- f a = 1 -- flag B = 2 -- debug 2-T Fukai
It's a little long. When you look at the above, you will understand the meaning. In this place, I use the SMART: Comment module, so the following ### will output the content of the variable itself. This is also a super powerful module. Let's take a look at the input parameters. What will be output.
### $ Verbose: 3
### $ More: 1
### $ Debug: 2
###@ Libs :[
### '/Lib ',
### '/Lib64'
###]
### % Flags :{
### A => '1 ',
### B => '2'
###}
< span style = "font - size: 18 px" >
< span style = "the font-family: Arial, Verdana, sans-serif" > < span style = "white - space: normal" > in the input parameters of the above, I see it. < / span > < / span > < / span >
A brief summary of the Getopt module
(1. Pass the value parameter into the program
* parameter types: integer, float, string
Getoptions (
'tag = s = > \ $tag
);
'=' means that this parameter must have a parameter value. ':' instead means that the parameter must not have a parameter value
's' represents the pass string argument, if an integer argument is passed for the 'I' table, and if a float is passed for the 'f' table.
Methods used with value parameters
$test. Pl - tag = string
(2. Parameters that need to be passed multiple values into the program.
For example, you need to pass a few values into @libfiles.
< span style = "font - size: 18 px" > GetOptions (" library = s = & gt;" \ @ libfiles);
GetOptions (" library = s @ "= & gt; \ $libfiles); < / span >
Parameters are passed to @$tag
Methods used
$test.pl --library lib/stdlib --library lib/extlib
(3. Parameter transfer for key-value pairs
Sometimes we need to pass some key value pairs into the program for processing, so we need to use this function.
< span style = "font - size: 18 px" > GetOptions (" define = s = "& gt; Defines \ %);
< / span >
GetOptions (define = "% s" = > \ $defines);
Methods used
$test.pl --define OS = Linux --define vendor=redhat
(4. Alias of parameters
You can use the following method when you need an alias such as a shorthand for a parameter
< span style = "font - size: 18 px" > GetOptions (' length | = f 'height = & gt; \ $length); < / span >
The first name is primary name, the others are alias(you can have multiple alias names), and the primary name is used as the key value when the hash parameter is used.