long.html

来自「perl教程」· HTML 代码 · 共 1,141 行 · 第 1/5 页

HTML
1,141 行
字号
however, allow the options and arguments to be mixed and 'filter out'
all the options before passing the rest of the arguments to the
program. To stop Getopt::Long from processing further arguments,
insert a double dash <code>--</code> on the command line:</p>
<pre>
    --size 24 -- --all</pre>
<p>In this example, <code>--all</code> will <em>not</em> be treated as an option, but
passed to the program unharmed, in <a href="../../lib/Pod/perlvar.html#item__argv"><code>@ARGV</code></a>.</p>
<p>
</p>
<h2><a name="options_with_values">Options with values</a></h2>
<p>For options that take values it must be specified whether the option
value is required or not, and what kind of value the option expects.</p>
<p>Three kinds of values are supported: integer numbers, floating point
numbers, and strings.</p>
<p>If the option value is required, Getopt::Long will take the
command line argument that follows the option and assign this to the
option variable. If, however, the option value is specified as
optional, this will only be done if that value does not look like a
valid command line option itself.</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$tag</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>       <span class="comment"># option variable with default value</span>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">'tag=s'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$tag</span><span class="operator">);</span>
</pre>
<p>In the option specification, the option name is followed by an equals
sign <code>=</code> and the letter <a href="#item_s"><code>s</code></a>. The equals sign indicates that this
option requires a value. The letter <a href="#item_s"><code>s</code></a> indicates that this value is
an arbitrary string. Other possible value types are <a href="#item_i"><code>i</code></a> for integer
values, and <a href="#item_f"><code>f</code></a> for floating point values. Using a colon <code>:</code> instead
of the equals sign indicates that the option value is optional. In
this case, if no suitable value is supplied, string valued options get
an empty string <code>''</code> assigned, while numeric options are set to <code>0</code>.</p>
<p>
</p>
<h2><a name="options_with_multiple_values">Options with multiple values</a></h2>
<p>Options sometimes take several values. For example, a program could
use multiple directories to search for library files:</p>
<pre>
    --library lib/stdlib --library lib/extlib</pre>
<p>To accomplish this behaviour, simply specify an array reference as the
destination for the option:</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"library=s"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">@libfiles</span><span class="operator">);</span>
</pre>
<p>Alternatively, you can specify that the option can have multiple
values by adding a &quot;@&quot;, and pass a scalar reference as the
destination:</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"library=s@"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$libfiles</span><span class="operator">);</span>
</pre>
<p>Used with the example above, <code>@libfiles</code> (or <code>@$libfiles</code>) would
contain two strings upon completion: <code>&quot;lib/srdlib&quot;</code> and
<code>&quot;lib/extlib&quot;</code>, in that order. It is also possible to specify that
only integer or floating point numbers are acceptable values.</p>
<p>Often it is useful to allow comma-separated lists of values as well as
multiple occurrences of the options. This is easy using Perl's <a href="../../lib/Pod/perlfunc.html#item_split"><code>split()</code></a>
and <a href="../../lib/Pod/perlfunc.html#item_join"><code>join()</code></a> operators:</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"library=s"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">@libfiles</span><span class="operator">);</span>
    <span class="variable">@libfiles</span> <span class="operator">=</span> <span class="keyword">split</span><span class="operator">(</span><span class="regex">/,/</span><span class="operator">,</span><span class="keyword">join</span><span class="operator">(</span><span class="string">','</span><span class="operator">,</span><span class="variable">@libfiles</span><span class="operator">));</span>
</pre>
<p>Of course, it is important to choose the right separator string for
each purpose.</p>
<p>Warning: What follows is an experimental feature.</p>
<p>Options can take multiple values at once, for example</p>
<pre>
    --coordinates 52.2 16.4 --rgbcolor 255 255 149</pre>
<p>This can be accomplished by adding a repeat specifier to the option
specification. Repeat specifiers are very similar to the <code>{...}</code>
repeat specifiers that can be used with regular expression patterns.
For example, the above command line would be handled as follows:</p>
<pre>
    <span class="variable">GetOptions</span><span class="operator">(</span><span class="string">'coordinates=f{2}'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">@coor</span><span class="operator">,</span> <span class="string">'rgbcolor=i{3}'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">@color</span><span class="operator">);</span>
</pre>
<p>The destination for the option must be an array or array reference.</p>
<p>It is also possible to specify the minimal and maximal number of
arguments an option takes. <code>foo=s{2,4}</code> indicates an option that
takes at least two and at most 4 arguments. <code>foo=s{,}</code> indicates one
or more values; <code>foo:s{,}</code> indicates zero or more option values.</p>
<p>
</p>
<h2><a name="options_with_hash_values">Options with hash values</a></h2>
<p>If the option destination is a reference to a hash, the option will
take, as value, strings of the form <em>key</em><code>=</code><em>value</em>. The value will
be stored with the specified key in the hash.</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"define=s"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">%defines</span><span class="operator">);</span>
</pre>
<p>Alternatively you can use:</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"define=s%"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$defines</span><span class="operator">);</span>
</pre>
<p>When used with command line options:</p>
<pre>
    --define os=linux --define vendor=redhat</pre>
<p>the hash <code>%defines</code> (or <code>%$defines</code>) will contain two keys, <code>&quot;os&quot;</code>
with value <code>&quot;linux</code> and <code>&quot;vendor&quot;</code> with value <code>&quot;redhat&quot;</code>. It is
also possible to specify that only integer or floating point numbers
are acceptable values. The keys are always taken to be strings.</p>
<p>
</p>
<h2><a name="userdefined_subroutines_to_handle_options">User-defined subroutines to handle options</a></h2>
<p>Ultimate control over what should be done when (actually: each time)
an option is encountered on the command line can be achieved by
designating a reference to a subroutine (or an anonymous subroutine)
as the option destination. When <code>GetOptions()</code> encounters the option, it
will call the subroutine with two or three arguments. The first
argument is the name of the option. For a scalar or array destination,
the second argument is the value to be stored. For a hash destination,
the second arguments is the key to the hash, and the third argument
the value to be stored. It is up to the subroutine to store the value,
or do whatever it thinks is appropriate.</p>
<p>A trivial application of this mechanism is to implement options that
are related to each other. For example:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$verbose</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>   <span class="comment"># option variable with default value (false)</span>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">'verbose'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$verbose</span><span class="operator">,</span>
                <span class="string">'quiet'</span>   <span class="operator">=&gt;</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">$verbose</span> <span class="operator">=</span> <span class="number">0</span> <span class="operator">});</span>
</pre>
<p>Here <code>--verbose</code> and <code>--quiet</code> control the same variable
<code>$verbose</code>, but with opposite values.</p>
<p>If the subroutine needs to signal an error, it should call <a href="../../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> with
the desired error message as its argument. <code>GetOptions()</code> will catch the
die(), issue the error message, and record that an error result must
be returned upon completion.</p>
<p>If the text of the error message starts with an exclamation mark <a href="#item__21"><code>!</code></a>
it is interpreted specially by GetOptions(). There is currently one
special command implemented: <a href="../../lib/Pod/perlfunc.html#item_die"><code>die(&quot;!FINISH&quot;)</code></a> will cause <code>GetOptions()</code>
to stop processing options, as if it encountered a double dash <code>--</code>.</p>
<p>
</p>
<h2><a name="options_with_multiple_names">Options with multiple names</a></h2>
<p>Often it is user friendly to supply alternate mnemonic names for
options. For example <code>--height</code> could be an alternate name for
<code>--length</code>. Alternate names can be included in the option
specification, separated by vertical bar <code>|</code> characters. To implement
the above example:</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">'length|height=f'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$length</span><span class="operator">);</span>
</pre>
<p>The first name is called the <em>primary</em> name, the other names are
called <em>aliases</em>. When using a hash to store options, the key will
always be the primary name.</p>
<p>Multiple alternate names are possible.</p>
<p>
</p>
<h2><a name="case_and_abbreviations">Case and abbreviations</a></h2>
<p>Without additional configuration, <code>GetOptions()</code> will ignore the case of
option names, and allow the options to be abbreviated to uniqueness.</p>
<pre>
    <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">'length|height=f'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$length</span><span class="operator">,</span> <span class="string">"head"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$head</span><span class="operator">);</span>
</pre>
<p>This call will allow <code>--l</code> and <code>--L</code> for the length option, but
requires a least <code>--hea</code> and <code>--hei</code> for the head and height options.</p>
<p>
</p>
<h2><a name="summary_of_option_specifications">Summary of Option Specifications</a></h2>
<p>Each option specifier consists of two parts: the name specification
and the argument specification.</p>
<p>The name specification contains the name of the option, optionally
followed by a list of alternative names separated by vertical bar
characters.</p>
<pre>
    length            option name is &quot;length&quot;
    length|size|l     name is &quot;length&quot;, aliases are &quot;size&quot; and &quot;l&quot;</pre>
<p>The argument specification is optional. If omitted, the option is
considered boolean, a value of 1 will be assigned when the option is
used on the command line.</p>
<p>The argument specification can be</p>
<dl>
<dt><strong><a name="item__21">!</a></strong>

<dd>
<p>The option does not take an argument and may be negated by prefixing
it with &quot;no&quot; or &quot;no-&quot;. E.g. <code>&quot;foo!&quot;</code> will allow <code>--foo</code> (a value of
1 will be assigned) as well as <code>--nofoo</code> and <code>--no-foo</code> (a value of
0 will be assigned). If the option has aliases, this applies to the
aliases as well.</p>
</dd>
<dd>
<p>Using negation on a single letter option when bundling is in effect is
pointless and will result in a warning.</p>
</dd>
</li>
<dt><strong><a name="item__2b">+</a></strong>

<dd>
<p>The option does not take an argument and will be incremented by 1
every time it appears on the command line. E.g. <code>&quot;more+&quot;</code>, when used
with <code>--more --more --more</code>, will increment the value three times,
resulting in a value of 3 (provided it was 0 or undefined at first).</p>
</dd>
<dd>
<p>The <a href="#item__2b"><code>+</code></a> specifier is ignored if the option destination is not a scalar.</p>
</dd>
</li>
<dt><strong><a name="item__3d_type__5b_desttype__5d__5b_repeat__5d">= <em>type</em> [ <em>desttype</em> ] [ <em>repeat</em> ]</a></strong>

<dd>
<p>The option requires an argument of the given type. Supported types
are:</p>
</dd>
<dl>
<dt><strong><a name="item_s">s</a></strong>

<dd>
<p>String. An arbitrary sequence of characters. It is valid for the
argument to start with <code>-</code> or <code>--</code>.</p>
</dd>
</li>
<dt><strong><a name="item_i">i</a></strong>

<dd>
<p>Integer. An optional leading plus or minus sign, followed by a
sequence of digits.</p>
</dd>
</li>
<dt><strong><a name="item_o">o</a></strong>

<dd>
<p>Extended integer, Perl style. This can be either an optional leading
plus or minus sign, followed by a sequence of digits, or an octal
string (a zero, optionally followed by '0', '1', .. '7'), or a
hexadecimal string (<code>0x</code> followed by '0' .. '9', 'a' .. 'f', case
insensitive), or a binary string (<code>0b</code> followed by a series of '0'
and '1').</p>
</dd>
</li>
<dt><strong><a name="item_f">f</a></strong>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?