Use the Getopt::long module in Perl to receive user command line parameters _ Application Tips

Source: Internet
Author: User
Tags hash

We often use a program in Linux to add parameters, and now it is much more powerful to understand the module Getopt::long in Perl about control parameters than to use @ARGV arrays directly. I think you know there are two different types of parameters in Linux.

• Long Parameter –help
• Short Parameters-H
That is--and--the distinction. – Represents the full parameter.-Represents a simplified parameter. These two methods are supported in this module of Perl.
This is to introduce the two Getopt actually has two modules, one called Getopt::long one is called GETOPT::STD. The following is only introduced Getopt::long. Because this module is more powerful.

Getopt::long Module
Initializing the parameters accepted in the Perl command line simplifies parsing of command-line arguments. See examples of programs below

Copy Code code as follows:

#!/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 use of the method, the following is explained in detail, pay attention to see the getoptions in the front of the => part. Here is the detailed

• ' verbose+ ' with + option does not receive the variable, the following do not need to add content. Just use it, and it will add one variable at a time, that is, when the-verbose-verbose occurs two times in the argument, the verbose value becomes 2.
• ' more! ' and then! option does not receive variables (that is, you do not need to add a parameter –more to use on the line), as long as the command line appears in this parameter, the default is 1, is used to set up and turn off a function of the. You can add no to the parameters such as-nomore.
• ' Flag=s ' has a string that requires a string (s), an integer (i), or a variable of a type such as a floating-point (f).
• ' debug:i ' option will accept an optional variable with a default of 0 or an empty string
• ' Test|t ' is followed by | option indicates that the –test can be abbreviated to-T.
• ' Lib=s ' => @libs if the associated variable is a number of arrays, such as the @libs of the place, the options can appear multiple times and the values can be pushed into the array.
• ' Flag=s ' =>%flags if the associated variable is a hash, then a key = value (Key=value) pair is required and inserted into the hash.

Note:
When a parameter name is matched, GetOptions ignores case by default, and the default parameter is abbreviated to the only shortest string (first letter) (for example, M represents-more.) The same first letter will be added to the second letter to distinguish

Methods used by the Getopt module:

According to the above example, we wrote a program called test.pl. We only need to add the following parameters to the command line:

Copy Code code as follows:
$./test.pl--verbose--verbose-v--more \--lib= '/lib '-l '/lib64 '--f a=1--flag b=2--debug 2-t

A little long, look at the above, you will understand the meaning. In this place, I use the Smart::comment module, so at the bottom of the ### is going to output the contents of the variable itself. This is also a super powerful module. Let's look at the input parameters.

Copy Code code as follows:

### $verbose: 3
### $more: 1
### $debug: 2
### @libs: [
### '/lib ',
### '/lib64 '
###        ]
###%flags: {
### a => ' 1 ',
### b => ' 2 '
###         }

On the input of the above parameters, understand it.

A brief summary of the Getopt module

(1. Incoming program with value parameter internal

※ Parameter type: integer, floating-point number, string

Copy Code code as follows:

GetOptions (
' Tag=s ' => \ $tag
);

' = ' means that this parameter must have a parameter value, if instead of ': ' instead of indicating that the parameter does not necessarily have a parameter value
' s ' means passing string arguments, if the ' I ' table passes an integer argument, if the ' F ' table passes floating-point numbers.

Methods for use with value parameters

Copy Code code as follows:

$ test.pl--tag=string
$ test.pl--tag String

(2. You need to transfer parameters for multiple values into your program.

For example, you need to pass several values to the @libfiles operation method.

Copy Code code as follows:

GetOptions ("library=s" => \ @libfiles);
GetOptions ("library=s@" => \ $libfiles);

parameter to the @ $tag
The method used

Copy Code code as follows:
$ test.pl--library lib/stdlib--library lib/extlib

(3. Passing arguments to key-value pairs

Sometimes we need to transfer some key values to the program to process, we need to use this function.

Copy Code code as follows:

GetOptions ("Define=s" => \%defines);
GetOptions ("define=s%" => \ $defines);

The method used

Copy Code code as follows:
$ test.pl--define os=linux--define vendor=redhat

(4. Alias of parameter)
When we need an alias with a shorthand, we can use the following method

Copy Code code as follows:
GetOptions (' length|height=f ' => \ $length);

The first name is named Primary name, the other is alias (can have more than one alias name), and when the hash parameter is used, primary name is used as the key value

Related Article

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.