📄 ch08_117.htm
字号:
<html><head><title>Getopt::Long (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_116.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_118.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.117. Getopt::Long</h2><p><a name="INDEX-1465" />Lets your program accept command-lineoptions with long names, introduced by <tt class="literal">--</tt>.Standard single-character options are also accepted. Options thatstart with <tt class="literal">--</tt> may have an argument appended,following a space or an equals sign (<tt class="literal">=</tt>):</p><blockquote><pre class="code">--foo=bar--foo bar</pre></blockquote><p>Provides two functions: <tt class="literal">GetOptions</tt> and<tt class="literal">config</tt>.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>config</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Getopt::Long::config(<em class="replaceable">optionlist</em>)</pre><p>Sets the variables in <em class="replaceable">optionlist</em> to changethe default behavior of <tt class="literal">GetOptions</tt>. The followingoptions are available:</p><dl><dt><b><tt class="literal">$Getopt::Long::autoabbrev</tt></b></dt><dd>If true, option names can be invoked with unique abbreviations.Default is <tt class="literal">1</tt> (true) unless the environmentvariable <tt class="literal">POSIXLY_CORRECT</tt> has been set.</p></dd><dt><b><tt class="literal">$Getopt::Long::getopt_compat</tt></b></dt><dd>If true, options can start with <tt class="literal">+</tt>. Default is<tt class="literal">1</tt> unless the environment variable<tt class="literal">POSIXLY_CORRECT</tt> has been set.</p></dd><dt><b><tt class="literal">$Getopt::Long::order</tt></b></dt><dd>Value indicates whether options and non-options may be mixed on thecommand line:</p><dl><dt><b><tt class="literal">$PERMUTE</tt></b></dt><dd>Non-options may be mixed with options. The default if<tt class="literal">POSIXLY_CORRECT</tt> is not set.</p></dd><dt><b><tt class="literal">$REQUIRE_ORDER</tt></b></dt><dd>Mixing is not allowed. The default if<tt class="literal">POSIXLY_CORRECT</tt> is set.</p></dd></dl></dd><dt><b><tt class="literal">$Getopt::Long::ignorecase</tt></b></dt><dd>If true, ignore case when matching options. Default is<tt class="literal">1</tt>.</p></dd><dt><b><tt class="literal">$Getopt::Long::VERSION</tt></b></dt><dd>The version number of this Getopt::Long implementation in the format<em class="emphasis">major.minor</em>.</p></dd><dt><b><tt class="literal">$Getopt::Long::error</tt></b></dt><dd>Internal error flag. May be incremented from a callback routine tocause options-parsing to fail.</p></dd><dt><b><tt class="literal">$Getopt::Long::debug</tt></b></dt><dd>If true, enables debugging output. Default is <tt class="literal">0</tt>(false).</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>GetOptions</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">result</em> = GetOptions(<em class="replaceable">option-descriptions</em>)</pre><p>Uses descriptions from <em class="replaceable">option-descriptions</em>to retrieve and process the command-line options with which your Perlprogram was invoked. The options are taken from<tt class="literal">@ARGV</tt>. After <tt class="literal">GetOptions</tt> hasprocessed the options, <tt class="literal">@ARGV</tt> contains onlycommand-line arguments that were not options. Returns<tt class="literal">0</tt> if errors are detected. Each option descriptionconsists of two elements:</p><dl><dt><i><em class="emphasis">Option specifier</em></i></dt><dd>Defines the option name and optionally a value as an argumentspecifier.</p></dd><dt><i><em class="emphasis">Option linkage</em></i></dt><dd>A reference to a variable that is set when the option is present.</p></dd></dl><p><tt class="literal">GetOptions</tt> can also take as a first argument areference to a hash that describes the linkage for the options. Thelinkage specified in the argument list takes precedence over the onespecified in the hash. Thus, the following are equivalent:</p><blockquote><pre class="code">%optctl = (size => \$offset);&GetOptions(\%optctl, "size=i");</pre></blockquote><p>and: </p><blockquote><pre class="code">&GetOptions("size=i" => \$offset);</pre></blockquote><h4 class="refsect1">Option specifiers</h4><p>Each option specifier consists of an option name and possibly anargument specifier. The name can be a name, or a list of namesseparated by <tt class="literal">|</tt>; the first name in the list is thetrue name of the option, and the others are treated as aliases.Option names may be invoked with the shortest unique abbreviation.Values for argument specifiers are:</p><dl><dt><b><tt class="literal"><none></tt></b></dt><dd>Option takes no argument. The option variable is set to<tt class="literal">1</tt>.</p></dd><dt><b><tt class="literal">!</tt></b></dt><dd>Option does not take an argument and may be negated, that is,prefixed by <tt class="literal">no</tt>.</p></dd><dt><b><tt class="literal">=s</tt></b></dt><dd>Option takes a mandatory argument that is a string that will beassigned to the option variable. Even if the argument starts with<tt class="literal">-</tt> or <tt class="literal">--</tt>, it is assigned to theoption variable rather than treated as another option.</p></dd><dt><b><tt class="literal">:s</tt></b></dt><dd>Option takes an optional string argument. If the option is invokedwith no argument, an empty string("") is assigned to the optionvariable. If the argument starts with <tt class="literal">-</tt> or<tt class="literal">--</tt>, it is treated as another option rather thanassigned to the option variable.</p></dd><dt><b><tt class="literal">=i</tt></b></dt><dd>Option takes a mandatory integer argument, which may start with<tt class="literal">-</tt> to indicate a negative value.</p></dd><dt><b><tt class="literal">:i</tt></b></dt><dd>Option takes an optional integer argument that may start with<tt class="literal">-</tt> to indicate a negative value. With no argument,the value <tt class="literal">0</tt> is assigned to the option variable.</p></dd><dt><b><tt class="literal">=f</tt></b></dt><dd>Option takes a mandatory floating-point argument that may start with<tt class="literal">-</tt> to indicate a negative value.</p></dd><dt><b><tt class="literal">:f</tt></b></dt><dd>Option takes an optional floating-point argument that may start with<tt class="literal">-</tt> to indicate a negative value. With no argument,the value <tt class="literal">0</tt> is assigned to the option variable.</p></dd></dl><p>A hyphen (<tt class="literal">-</tt>) by itself is considered an optionwhose name is the empty string. A double hyphen(<tt class="literal">--</tt>) by itself terminates option processing. Anyoptions following the double hyphen remain in<tt class="literal">@ARGV</tt> when <tt class="literal">GetOptions</tt> returns.If an argument specifier ends with <tt class="literal">@</tt> (e.g.,<tt class="literal">=s@</tt>), then the option is treated as an array.</p><p>The special option specifier <tt class="literal"><></tt> can be usedto designate a subroutine to handle non-option arguments. For thisspecifier to be used, the variable<tt class="literal">$Getopt::Long::order</tt> must have the value of thepredefined and exported variable, <tt class="literal">$PERMUTE</tt>. Seethe description of <tt class="literal">Getopt::Long::config</tt> below.</p><h4 class="refsect1">Linkage specification</h4><p>The linkage specifier can be a reference to any of the following:</p><dl><dt><i><em class="emphasis">Scalar</em></i></dt><dd>The new value is stored in the referenced variable. If the optionoccurs more than once, the previous value is overwritten.</p></dd><dt><i><em class="emphasis">Array</em></i></dt><dd>The new value is appended (pushed) onto the referenced array.</p></dd><dt><i><em class="emphasis">Subroutine</em></i></dt><dd>The referenced subroutine is called with two arguments: the optionname, which is always the true name, and the option value.</p></dd></dl><p>If no linkage is explicitly specified, but a hash reference ispassed, <tt class="literal">GetOptions</tt> puts the value in the hash. Forarray options, a reference to an anonymous array is generated.</p><p>If no linkage is explicitly specified, and no hash reference ispassed, <tt class="literal">GetOptions</tt> puts the value into a globalvariable named after the option, prefixed by <tt class="literal">opt_</tt>.Characters that are not part of the variable syntax are translated tounderscores. For example, <tt class="literal">--fpp-struct-return</tt> setsthe variable <tt class="literal">$opt_fpp_struct_return</tt>.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_116.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_118.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.116. GDBM_File</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.118. Getopt::Std</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -