📄 getopt::long.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Getopt::Long 3".TH Getopt::Long 3 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"Getopt::Long \- Extended processing of command line options.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 7\& use Getopt::Long;\& my $data = "file.dat";\& my $length = 24;\& my $verbose;\& $result = GetOptions ("length=i" => \e$length, # numeric\& "file=s" => \e$data, # string\& "verbose" => \e$verbose); # flag.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"The Getopt::Long module implements an extended getopt function called\&\fIGetOptions()\fR. This function adheres to the \s-1POSIX\s0 syntax for commandline options, with \s-1GNU\s0 extensions. In general, this means that optionshave long names instead of single letters, and are introduced with adouble dash \*(L"\-\-\*(R". Support for bundling of command line options, as wasthe case with the more traditional single-letter approach, is providedbut not enabled by default..SH "Command Line Options, an Introduction".IX Header "Command Line Options, an Introduction"Command line operated programs traditionally take their arguments fromthe command line, for example filenames or other information that theprogram needs to know. Besides arguments, these programs often takecommand line \fIoptions\fR as well. Options are not necessary for theprogram to work, hence the name 'option', but are used to modify itsdefault behaviour. For example, a program could do its job quietly,but with a suitable option it could provide verbose information aboutwhat it did..PPCommand line options come in several flavours. Historically, they arepreceded by a single dash \f(CW\*(C`\-\*(C'\fR, and consist of a single letter..PP.Vb 1\& \-l \-a \-c.Ve.PPUsually, these single-character options can be bundled:.PP.Vb 1\& \-lac.Ve.PPOptions can have values, the value is placed after the optioncharacter. Sometimes with whitespace in between, sometimes not:.PP.Vb 1\& \-s 24 \-s24.Ve.PPDue to the very cryptic nature of these options, another style wasdeveloped that used long names. So instead of a cryptic \f(CW\*(C`\-l\*(C'\fR onecould use the more descriptive \f(CW\*(C`\-\-long\*(C'\fR. To distinguish between abundle of single-character options and a long one, two dashes are usedto precede the option name. Early implementations of long options useda plus \f(CW\*(C`+\*(C'\fR instead. Also, option values could be specified eitherlike.PP.Vb 1\& \-\-size=24.Ve.PPor.PP.Vb 1\& \-\-size 24.Ve.PPThe \f(CW\*(C`+\*(C'\fR form is now obsolete and strongly deprecated..SH "Getting Started with Getopt::Long".IX Header "Getting Started with Getopt::Long"Getopt::Long is the Perl5 successor of \f(CW\*(C`newgetopt.pl\*(C'\fR. This was thefirst Perl module that provided support for handling the new style ofcommand line options, hence the name Getopt::Long. This module alsosupports single-character options and bundling. Single characteroptions may be any alphabetic character, a question mark, and a dash.Long options may consist of a series of letters, digits, and dashes.Although this is currently not enforced by Getopt::Long, multipleconsecutive dashes are not allowed, and the option name must not endwith a dash..PPTo use Getopt::Long from a Perl program, you must include thefollowing line in your Perl program:.PP.Vb 1\& use Getopt::Long;.Ve.PPThis will load the core of the Getopt::Long module and prepare yourprogram for using it. Most of the actual Getopt::Long code is notloaded until you really call one of its functions..PPIn the default configuration, options names may be abbreviated touniqueness, case does not matter, and a single dash is sufficient,even for long option names. Also, options may be placed betweennon-option arguments. See \*(L"Configuring Getopt::Long\*(R" for moredetails on how to configure Getopt::Long..Sh "Simple options".IX Subsection "Simple options"The most simple options are the ones that take no values. Their merepresence on the command line enables the option. Popular examples are:.PP.Vb 1\& \-\-all \-\-verbose \-\-quiet \-\-debug.Ve.PPHandling simple options is straightforward:.PP.Vb 3\& my $verbose = \*(Aq\*(Aq; # option variable with default value (false)\& my $all = \*(Aq\*(Aq; # option variable with default value (false)\& GetOptions (\*(Aqverbose\*(Aq => \e$verbose, \*(Aqall\*(Aq => \e$all);.Ve.PPThe call to \fIGetOptions()\fR parses the command line arguments that arepresent in \f(CW@ARGV\fR and sets the option variable to the value \f(CW1\fR ifthe option did occur on the command line. Otherwise, the optionvariable is not touched. Setting the option value to true is oftencalled \fIenabling\fR the option..PPThe option name as specified to the \fIGetOptions()\fR function is calledthe option \fIspecification\fR. Later we'll see that this specificationcan contain more than just the option name. The reference to thevariable is called the option \fIdestination\fR..PP\&\fIGetOptions()\fR will return a true value if the command line could beprocessed successfully. Otherwise, it will write error messages to\&\s-1STDERR\s0, and return a false result..Sh "A little bit less simple options".IX Subsection "A little bit less simple options"Getopt::Long supports two useful variants of simple options:\&\fInegatable\fR options and \fIincremental\fR options..PPA negatable option is specified with an exclamation mark \f(CW\*(C`!\*(C'\fR after theoption name:.PP.Vb 2\& my $verbose = \*(Aq\*(Aq; # option variable with default value (false)\& GetOptions (\*(Aqverbose!\*(Aq => \e$verbose);.Ve.PPNow, using \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will enable \f(CW$verbose\fR,as expected. But it is also allowed to use \f(CW\*(C`\-\-noverbose\*(C'\fR, which willdisable \f(CW$verbose\fR by setting its value to \f(CW0\fR. Using a suitabledefault value, the program can find out whether \f(CW$verbose\fR is falseby default, or disabled by using \f(CW\*(C`\-\-noverbose\*(C'\fR..PPAn incremental option is specified with a plus \f(CW\*(C`+\*(C'\fR after theoption name:.PP.Vb 2\& my $verbose = \*(Aq\*(Aq; # option variable with default value (false)\& GetOptions (\*(Aqverbose+\*(Aq => \e$verbose);.Ve.PPUsing \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will increment the value of\&\f(CW$verbose\fR. This way the program can keep track of how many times theoption occurred on the command line. For example, each occurrence of\&\f(CW\*(C`\-\-verbose\*(C'\fR could increase the verbosity level of the program..Sh "Mixing command line option with other arguments".IX Subsection "Mixing command line option with other arguments"Usually programs take command line options as well as other arguments,for example, file names. It is good practice to always specify theoptions first, and the other arguments last. Getopt::Long will,however, allow the options and arguments to be mixed and 'filter out'all the options before passing the rest of the arguments to theprogram. To stop Getopt::Long from processing further arguments,insert a double dash \f(CW\*(C`\-\-\*(C'\fR on the command line:.PP.Vb 1\& \-\-size 24 \-\- \-\-all.Ve.PPIn this example, \f(CW\*(C`\-\-all\*(C'\fR will \fInot\fR be treated as an option, butpassed to the program unharmed, in \f(CW@ARGV\fR..Sh "Options with values".IX Subsection "Options with values"For options that take values it must be specified whether the optionvalue is required or not, and what kind of value the option expects..PPThree kinds of values are supported: integer numbers, floating pointnumbers, and strings..PPIf the option value is required, Getopt::Long will take thecommand line argument that follows the option and assign this to theoption variable. If, however, the option value is specified asoptional, this will only be done if that value does not look like avalid command line option itself..PP.Vb 2\& my $tag = \*(Aq\*(Aq; # option variable with default value\& GetOptions (\*(Aqtag=s\*(Aq => \e$tag);.Ve.PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -