long.html
来自「perl教程」· HTML 代码 · 共 1,141 行 · 第 1/5 页
HTML
1,141 行
<p>
</p>
<h2><a name="argument_callback">Argument callback</a></h2>
<p>A special option 'name' <code><></code> can be used to designate a subroutine
to handle non-option arguments. When <code>GetOptions()</code> encounters an
argument that does not look like an option, it will immediately call this
subroutine and passes it one parameter: the argument name.</p>
<p>For example:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$width</span> <span class="operator">=</span> <span class="number">80</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> process </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span>
<span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">'width=i'</span> <span class="operator">=></span> <span class="operator">\</span><span class="variable">$width</span><span class="operator">,</span> <span class="string">'<>'</span> <span class="operator">=></span> <span class="operator">\&</span><span class="variable">process</span><span class="operator">);</span>
</pre>
<p>When applied to the following command line:</p>
<pre>
arg1 --width=72 arg2 --width=60 arg3</pre>
<p>This will call
<code>process("arg1")</code> while <code>$width</code> is <code>80</code>,
<code>process("arg2")</code> while <code>$width</code> is <code>72</code>, and
<code>process("arg3")</code> while <code>$width</code> is <code>60</code>.</p>
<p>This feature requires configuration option <strong>permute</strong>, see section
<a href="#configuring_getopt__long">Configuring Getopt::Long</a>.</p>
<p>
</p>
<hr />
<h1><a name="configuring_getopt__long">Configuring Getopt::Long</a></h1>
<p>Getopt::Long can be configured by calling subroutine
Getopt::Long::Configure(). This subroutine takes a list of quoted
strings, each specifying a configuration option to be enabled, e.g.
<a href="#item_ignore_case"><code>ignore_case</code></a>, or disabled, e.g. <code>no_ignore_case</code>. Case does not
matter. Multiple calls to <code>Configure()</code> are possible.</p>
<p>Alternatively, as of version 2.24, the configuration options may be
passed together with the <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Getopt::Long</span> <span class="string">qw(:config no_ignore_case bundling)</span><span class="operator">;</span>
</pre>
<p>The following options are available:</p>
<dl>
<dt><strong><a name="item_default">default</a></strong>
<dd>
<p>This option causes all configuration options to be reset to their
default values.</p>
</dd>
</li>
<dt><strong><a name="item_posix_default">posix_default</a></strong>
<dd>
<p>This option causes all configuration options to be reset to their
default values as if the environment variable POSIXLY_CORRECT had
been set.</p>
</dd>
</li>
<dt><strong><a name="item_auto_abbrev">auto_abbrev</a></strong>
<dd>
<p>Allow option names to be abbreviated to uniqueness.
Default is enabled unless environment variable
POSIXLY_CORRECT has been set, in which case <a href="#item_auto_abbrev"><code>auto_abbrev</code></a> is disabled.</p>
</dd>
</li>
<dt><strong><a name="item_getopt_compat">getopt_compat</a></strong>
<dd>
<p>Allow <a href="#item__2b"><code>+</code></a> to start options.
Default is enabled unless environment variable
POSIXLY_CORRECT has been set, in which case <a href="#item_getopt_compat"><code>getopt_compat</code></a> is disabled.</p>
</dd>
</li>
<dt><strong><a name="item_gnu_compat">gnu_compat</a></strong>
<dd>
<p><a href="#item_gnu_compat"><code>gnu_compat</code></a> controls whether <code>--opt=</code> is allowed, and what it should
do. Without <a href="#item_gnu_compat"><code>gnu_compat</code></a>, <code>--opt=</code> gives an error. With <a href="#item_gnu_compat"><code>gnu_compat</code></a>,
<code>--opt=</code> will give option <code>opt</code> and empty value.
This is the way GNU <code>getopt_long()</code> does it.</p>
</dd>
</li>
<dt><strong><a name="item_gnu_getopt">gnu_getopt</a></strong>
<dd>
<p>This is a short way of setting <a href="#item_gnu_compat"><code>gnu_compat</code></a> <a href="#item_bundling"><code>bundling</code></a> <a href="#item_permute"><code>permute</code></a>
<code>no_getopt_compat</code>. With <a href="#item_gnu_getopt"><code>gnu_getopt</code></a>, command line handling should be
fully compatible with GNU getopt_long().</p>
</dd>
</li>
<dt><strong><a name="item_require_order">require_order</a></strong>
<dd>
<p>Whether command line arguments are allowed to be mixed with options.
Default is disabled unless environment variable
POSIXLY_CORRECT has been set, in which case <a href="#item_require_order"><code>require_order</code></a> is enabled.</p>
</dd>
<dd>
<p>See also <a href="#item_permute"><code>permute</code></a>, which is the opposite of <a href="#item_require_order"><code>require_order</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_permute">permute</a></strong>
<dd>
<p>Whether command line arguments are allowed to be mixed with options.
Default is enabled unless environment variable
POSIXLY_CORRECT has been set, in which case <a href="#item_permute"><code>permute</code></a> is disabled.
Note that <a href="#item_permute"><code>permute</code></a> is the opposite of <a href="#item_require_order"><code>require_order</code></a>.</p>
</dd>
<dd>
<p>If <a href="#item_permute"><code>permute</code></a> is enabled, this means that</p>
</dd>
<dd>
<pre>
--foo arg1 --bar arg2 arg3</pre>
</dd>
<dd>
<p>is equivalent to</p>
</dd>
<dd>
<pre>
--foo --bar arg1 arg2 arg3</pre>
</dd>
<dd>
<p>If an argument callback routine is specified, <a href="../../lib/Pod/perlvar.html#item__argv"><code>@ARGV</code></a> will always be
empty upon successful return of <code>GetOptions()</code> since all options have been
processed. The only exception is when <code>--</code> is used:</p>
</dd>
<dd>
<pre>
--foo arg1 --bar arg2 -- arg3</pre>
</dd>
<dd>
<p>This will call the callback routine for arg1 and arg2, and then
terminate <code>GetOptions()</code> leaving <code>"arg2"</code> in <a href="../../lib/Pod/perlvar.html#item__argv"><code>@ARGV</code></a>.</p>
</dd>
<dd>
<p>If <a href="#item_require_order"><code>require_order</code></a> is enabled, options processing
terminates when the first non-option is encountered.</p>
</dd>
<dd>
<pre>
--foo arg1 --bar arg2 arg3</pre>
</dd>
<dd>
<p>is equivalent to</p>
</dd>
<dd>
<pre>
--foo -- arg1 --bar arg2 arg3</pre>
</dd>
<dd>
<p>If <a href="#item_pass_through"><code>pass_through</code></a> is also enabled, options processing will terminate
at the first unrecognized option, or non-option, whichever comes
first.</p>
</dd>
</li>
<dt><strong><a name="item_bundling">bundling (default: disabled)</a></strong>
<dd>
<p>Enabling this option will allow single-character options to be
bundled. To distinguish bundles from long option names, long options
<em>must</em> be introduced with <code>--</code> and bundles with <code>-</code>.</p>
</dd>
<dd>
<p>Note that, if you have options <code>a</code>, <code>l</code> and <code>all</code>, and
auto_abbrev enabled, possible arguments and option settings are:</p>
</dd>
<dd>
<pre>
using argument sets option(s)
------------------------------------------
-a, --a a
-l, --l l
-al, -la, -ala, -all,... a, l
--al, --all all</pre>
</dd>
<dd>
<p>The surprising part is that <code>--a</code> sets option <code>a</code> (due to auto
completion), not <code>all</code>.</p>
</dd>
<dd>
<p>Note: disabling <a href="#item_bundling"><code>bundling</code></a> also disables <a href="#item_bundling_override"><code>bundling_override</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_bundling_override">bundling_override (default: disabled)</a></strong>
<dd>
<p>If <a href="#item_bundling_override"><code>bundling_override</code></a> is enabled, bundling is enabled as with
<a href="#item_bundling"><code>bundling</code></a> but now long option names override option bundles.</p>
</dd>
<dd>
<p>Note: disabling <a href="#item_bundling_override"><code>bundling_override</code></a> also disables <a href="#item_bundling"><code>bundling</code></a>.</p>
</dd>
<dd>
<p><strong>Note:</strong> Using option bundling can easily lead to unexpected results,
especially when mixing long options and bundles. Caveat emptor.</p>
</dd>
</li>
<dt><strong><a name="item_ignore_case">ignore_case (default: enabled)</a></strong>
<dd>
<p>If enabled, case is ignored when matching long option names. If,
however, bundling is enabled as well, single character options will be
treated case-sensitive.</p>
</dd>
<dd>
<p>With <a href="#item_ignore_case"><code>ignore_case</code></a>, option specifications for options that only
differ in case, e.g., <code>"foo"</code> and <code>"Foo"</code>, will be flagged as
duplicates.</p>
</dd>
<dd>
<p>Note: disabling <a href="#item_ignore_case"><code>ignore_case</code></a> also disables <a href="#item_ignore_case_always"><code>ignore_case_always</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_ignore_case_always">ignore_case_always (default: disabled)</a></strong>
<dd>
<p>When bundling is in effect, case is ignored on single-character
options also.</p>
</dd>
<dd>
<p>Note: disabling <a href="#item_ignore_case_always"><code>ignore_case_always</code></a> also disables <a href="#item_ignore_case"><code>ignore_case</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_auto_version">auto_version (default:disabled)</a></strong>
<dd>
<p>Automatically provide support for the <strong>--version</strong> option if
the application did not specify a handler for this option itself.</p>
</dd>
<dd>
<p>Getopt::Long will provide a standard version message that includes the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?