long.html

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

HTML
1,141 行
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Getopt::Long - Extended processing of command line options</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Getopt::Long - Extended processing of command line options</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#command_line_options__an_introduction">Command Line Options, an Introduction</a></li>
	<li><a href="#getting_started_with_getopt__long">Getting Started with Getopt::Long</a></li>
	<ul>

		<li><a href="#simple_options">Simple options</a></li>
		<li><a href="#a_little_bit_less_simple_options">A little bit less simple options</a></li>
		<li><a href="#mixing_command_line_option_with_other_arguments">Mixing command line option with other arguments</a></li>
		<li><a href="#options_with_values">Options with values</a></li>
		<li><a href="#options_with_multiple_values">Options with multiple values</a></li>
		<li><a href="#options_with_hash_values">Options with hash values</a></li>
		<li><a href="#userdefined_subroutines_to_handle_options">User-defined subroutines to handle options</a></li>
		<li><a href="#options_with_multiple_names">Options with multiple names</a></li>
		<li><a href="#case_and_abbreviations">Case and abbreviations</a></li>
		<li><a href="#summary_of_option_specifications">Summary of Option Specifications</a></li>
	</ul>

	<li><a href="#advanced_possibilities">Advanced Possibilities</a></li>
	<ul>

		<li><a href="#object_oriented_interface">Object oriented interface</a></li>
		<li><a href="#thread_safety">Thread Safety</a></li>
		<li><a href="#documentation_and_help_texts">Documentation and help texts</a></li>
		<li><a href="#storing_option_values_in_a_hash">Storing option values in a hash</a></li>
		<li><a href="#bundling">Bundling</a></li>
		<li><a href="#the_lonesome_dash">The lonesome dash</a></li>
		<li><a href="#argument_callback">Argument callback</a></li>
	</ul>

	<li><a href="#configuring_getopt__long">Configuring Getopt::Long</a></li>
	<li><a href="#exportable_methods">Exportable Methods</a></li>
	<li><a href="#return_values_and_errors">Return values and Errors</a></li>
	<li><a href="#legacy">Legacy</a></li>
	<ul>

		<li><a href="#default_destinations">Default destinations</a></li>
		<li><a href="#alternative_option_starters">Alternative option starters</a></li>
		<li><a href="#configuration_variables">Configuration variables</a></li>
	</ul>

	<li><a href="#trouble_shooting">Trouble Shooting</a></li>
	<ul>

		<li><a href="#getoptions_does_not_return_a_false_result_when_an_option_is_not_supplied">GetOptions does not return a false result when an option is not supplied</a></li>
		<li><a href="#getoptions_does_not_split_the_command_line_correctly">GetOptions does not split the command line correctly</a></li>
		<li><a href="#undefined_subroutine__main__getoptions_called">Undefined subroutine &amp;main::GetOptions called</a></li>
		<li><a href="#how_do_i_put_a__option_into_a_getopt__long">How do I put a &quot;-?&quot; option into a Getopt::Long?</a></li>
	</ul>

	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#copyright_and_disclaimer">COPYRIGHT AND DISCLAIMER</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Getopt::Long - Extended processing of command line options</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
  <span class="keyword">use</span> <span class="variable">Getopt::Long</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$data</span>   <span class="operator">=</span> <span class="string">"file.dat"</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$length</span> <span class="operator">=</span> <span class="number">24</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$verbose</span><span class="operator">;</span>
  <span class="variable">$result</span> <span class="operator">=</span> <span class="variable">GetOptions</span> <span class="operator">(</span><span class="string">"length=i"</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$length</span><span class="operator">,</span>    <span class="comment"># numeric</span>
                        <span class="string">"file=s"</span>   <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$data</span><span class="operator">,</span>      <span class="comment"># string</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="comment"># flag</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The Getopt::Long module implements an extended getopt function called
GetOptions(). This function adheres to the POSIX syntax for command
line options, with GNU extensions. In general, this means that options
have long names instead of single letters, and are introduced with a
double dash &quot;--&quot;. Support for bundling of command line options, as was
the case with the more traditional single-letter approach, is provided
but not enabled by default.</p>
<p>
</p>
<hr />
<h1><a name="command_line_options__an_introduction">Command Line Options, an Introduction</a></h1>
<p>Command line operated programs traditionally take their arguments from
the command line, for example filenames or other information that the
program needs to know. Besides arguments, these programs often take
command line <em>options</em> as well. Options are not necessary for the
program to work, hence the name 'option', but are used to modify its
default behaviour. For example, a program could do its job quietly,
but with a suitable option it could provide verbose information about
what it did.</p>
<p>Command line options come in several flavours. Historically, they are
preceded by a single dash <code>-</code>, and consist of a single letter.</p>
<pre>
    -l -a -c</pre>
<p>Usually, these single-character options can be bundled:</p>
<pre>
    -lac</pre>
<p>Options can have values, the value is placed after the option
character. Sometimes with whitespace in between, sometimes not:</p>
<pre>
    -s 24 -s24</pre>
<p>Due to the very cryptic nature of these options, another style was
developed that used long names. So instead of a cryptic <code>-l</code> one
could use the more descriptive <code>--long</code>. To distinguish between a
bundle of single-character options and a long one, two dashes are used
to precede the option name. Early implementations of long options used
a plus <a href="#item__2b"><code>+</code></a> instead. Also, option values could be specified either
like</p>
<pre>
    --size=24</pre>
<p>or</p>
<pre>
    --size 24</pre>
<p>The <a href="#item__2b"><code>+</code></a> form is now obsolete and strongly deprecated.</p>
<p>
</p>
<hr />
<h1><a name="getting_started_with_getopt__long">Getting Started with Getopt::Long</a></h1>
<p>Getopt::Long is the Perl5 successor of <code>newgetopt.pl</code>. This was the
first Perl module that provided support for handling the new style of
command line options, hence the name Getopt::Long. This module also
supports single-character options and bundling. Single character
options 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, multiple
consecutive dashes are not allowed, and the option name must not end
with a dash.</p>
<p>To use Getopt::Long from a Perl program, you must include the
following line in your Perl program:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Getopt::Long</span><span class="operator">;</span>
</pre>
<p>This will load the core of the Getopt::Long module and prepare your
program for using it. Most of the actual Getopt::Long code is not
loaded until you really call one of its functions.</p>
<p>In the default configuration, options names may be abbreviated to
uniqueness, case does not matter, and a single dash is sufficient,
even for long option names. Also, options may be placed between
non-option arguments. See <a href="#configuring_getopt__long">Configuring Getopt::Long</a> for more
details on how to configure Getopt::Long.</p>
<p>
</p>
<h2><a name="simple_options">Simple options</a></h2>
<p>The most simple options are the ones that take no values. Their mere
presence on the command line enables the option. Popular examples are:</p>
<pre>
    --all --verbose --quiet --debug</pre>
<p>Handling simple options is straightforward:</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="keyword">my</span> <span class="variable">$all</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">'all'</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">$all</span><span class="operator">);</span>
</pre>
<p>The call to <code>GetOptions()</code> parses the command line arguments that are
present in <a href="../../lib/Pod/perlvar.html#item__argv"><code>@ARGV</code></a> and sets the option variable to the value <code>1</code> if
the option did occur on the command line. Otherwise, the option
variable is not touched. Setting the option value to true is often
called <em>enabling</em> the option.</p>
<p>The option name as specified to the <code>GetOptions()</code> function is called
the option <em>specification</em>. Later we'll see that this specification
can contain more than just the option name. The reference to the
variable is called the option <em>destination</em>.</p>
<p><code>GetOptions()</code> will return a true value if the command line could be
processed successfully. Otherwise, it will write error messages to
STDERR, and return a false result.</p>
<p>
</p>
<h2><a name="a_little_bit_less_simple_options">A little bit less simple options</a></h2>
<p>Getopt::Long supports two useful variants of simple options:
<em>negatable</em> options and <em>incremental</em> options.</p>
<p>A negatable option is specified with an exclamation mark <a href="#item__21"><code>!</code></a> after the
option name:</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>
</pre>
<p>Now, using <code>--verbose</code> on the command line will enable <code>$verbose</code>,
as expected. But it is also allowed to use <code>--noverbose</code>, which will
disable <code>$verbose</code> by setting its value to <code>0</code>. Using a suitable
default value, the program can find out whether <code>$verbose</code> is false
by default, or disabled by using <code>--noverbose</code>.</p>
<p>An incremental option is specified with a plus <a href="#item__2b"><code>+</code></a> after the
option name:</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>
</pre>
<p>Using <code>--verbose</code> on the command line will increment the value of
<code>$verbose</code>. This way the program can keep track of how many times the
option occurred on the command line. For example, each occurrence of
<code>--verbose</code> could increase the verbosity level of the program.</p>
<p>
</p>
<h2><a name="mixing_command_line_option_with_other_arguments">Mixing command line option with other arguments</a></h2>
<p>Usually programs take command line options as well as other arguments,
for example, file names. It is good practice to always specify the
options first, and the other arguments last. Getopt::Long will,

⌨️ 快捷键说明

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