Perl's getopt: Long and POD: Usage

Source: Internet
Author: User

Code:

Variables need to be explicitly defined and initialized. For example, optionx.

If no variables are defined and explicitly initialized, and no options are specified on the command line, the variables corresponding to the options are undefined.

 

# ! /Bin/perl-5.8.3/bin/perl $
Use Warnings; $
Use Strict; $

UseData: dumper; $
UseGetopt: long; $
UsePod: usage; $

Our $ G_opts ;
Our $ Optionx = '' ; # If not defined in command line, it will be empty string
Sub Parse_opts {
My $ Result = Getoptions (
" Optiona = s " => \ $ G_opts -> { ' Optiona ' }, # String
" Optionb = s " => \$ G_opts -> { ' Optionb ' }, # String
" Optionc = I " => \ $ G_opts -> { ' Optionc ' }, # Integer
" Optiond = f " => \ $ G_opts -> { ' Optiond ' }, # Float
" Optionx = f " => \ $ Optionx ,
" Optiony = f " => \ $ Optiony ,
" Verbose " => \ $ G_opts -> { ' Verbose ' }, # Flag
" Quiet " => Sub { $ G_opts -> { ' Verbose ' } = 0 },
" Help |? " => \$ G_opts -> { ' Help ' }
);
If (! ( $ G_opts -> { ' Optiona ' })){
& Pod2usage (-verbose => 1 ); # Exit status will be 1
}
If ( $ G_opts -> { ' Help ' }){
& Pod2usage (-verbose => 1 ); # Exit status will be 1
}
}

& Parse_opts ();
Print("\ N $ optionx \ n");
Print($ Optiony);#If not defined in command line, it will be undefined
Print($ G_opts-> {"Optionb"});

Foreach My $ Key ( Keys % { $ G_opts }) {$
If (! $ G_opts -> { $ Key }){ Next ;} $
Print ( $ Key . " = " . $ G_opts -> { $ Key }. " \ N " ); $
} $
$
Exit ( 0 ); $
$

$
_ End __ $
$
= Head1 name $
$
Sample-using getopt: Long and POD: usage $
$
= Head1 synopsis $
$
Sample [Options] [ARGs...] $
$
Options: $
$
-Optiona $
-Optionb $
-Optionc $
-Optiond $
-Verbose $
-Quiet noverbose $
-Help brief help message $
$
= Head1 options $
$
= Over 8 $
$
= Item B <-help> $
$
Print A brief help message and exits. $
$
= Back $
$
= Head1 description $
$
B <this program> will Read The given input file (s) and Do Something $
Useful with the contents thereof. $
$
= Cut $

 

Complete!

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.