home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _a26cf856a3d7c21cd30f343c130c6d71 < prev    next >
Text File  |  2000-03-23  |  38KB  |  750 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Getopt::Long - Extended processing of command line options</TITLE>
  5. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Getopt::Long - Extended processing of command line options</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <LI><A HREF="#command line options, an introduction">Command Line Options, an Introduction</A></LI>
  26.     <LI><A HREF="#getting started with getopt::long">Getting Started with Getopt::Long</A></LI>
  27.     <UL>
  28.  
  29.         <LI><A HREF="#simple options">Simple options</A></LI>
  30.         <LI><A HREF="#a little bit less simple options">A little bit less simple options</A></LI>
  31.         <LI><A HREF="#mixing command line option with other arguments">Mixing command line option with other arguments</A></LI>
  32.         <LI><A HREF="#options with values">Options with values</A></LI>
  33.         <LI><A HREF="#options with multiple values">Options with multiple values</A></LI>
  34.         <LI><A HREF="#options with hash values">Options with hash values</A></LI>
  35.         <LI><A HREF="#userdefined subroutines to handle options">User-defined subroutines to handle options</A></LI>
  36.         <LI><A HREF="#options with multiple names">Options with multiple names</A></LI>
  37.         <LI><A HREF="#case and abbreviations">Case and abbreviations</A></LI>
  38.         <LI><A HREF="#summary of option specifications">Summary of Option Specifications</A></LI>
  39.     </UL>
  40.  
  41.     <LI><A HREF="#advanced possibilities">Advanced Possibilities</A></LI>
  42.     <UL>
  43.  
  44.         <LI><A HREF="#documentation and help texts">Documentation and help texts</A></LI>
  45.         <LI><A HREF="#storing options in a hash">Storing options in a hash</A></LI>
  46.         <LI><A HREF="#bundling">Bundling</A></LI>
  47.         <LI><A HREF="#the lonesome dash">The lonesome dash</A></LI>
  48.         <LI><A HREF="#argument callback">Argument call-back</A></LI>
  49.     </UL>
  50.  
  51.     <LI><A HREF="#configuring getopt::long">Configuring Getopt::Long</A></LI>
  52.     <LI><A HREF="#return values and errors">Return values and Errors</A></LI>
  53.     <LI><A HREF="#legacy">Legacy</A></LI>
  54.     <UL>
  55.  
  56.         <LI><A HREF="#default destinations">Default destinations</A></LI>
  57.         <LI><A HREF="#alternative option starters">Alternative option starters</A></LI>
  58.         <LI><A HREF="#configuration variables">Configuration variables</A></LI>
  59.     </UL>
  60.  
  61.     <LI><A HREF="#author">AUTHOR</A></LI>
  62.     <LI><A HREF="#copyright and disclaimer">COPYRIGHT AND DISCLAIMER</A></LI>
  63. </UL>
  64. <!-- INDEX END -->
  65.  
  66. <HR>
  67. <P>
  68. <H1><A NAME="name">NAME</A></H1>
  69. <P>Getopt::Long - Extended processing of command line options</P>
  70. <P>
  71. <HR>
  72. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  73. <UL>
  74. <LI>Linux</LI>
  75. <LI>Solaris</LI>
  76. <LI>Windows</LI>
  77. </UL>
  78. <HR>
  79. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  80. <PRE>
  81.   use Getopt::Long;
  82.   $result = GetOptions (...option-descriptions...);</PRE>
  83. <P>
  84. <HR>
  85. <H1><A NAME="description">DESCRIPTION</A></H1>
  86. <P>The Getopt::Long module implements an extended getopt function called
  87. GetOptions(). This function adheres to the POSIX syntax for command
  88. line options, with GNU extensions. In general, this means that options
  89. have long names instead of single letters, and are introduced with a
  90. double dash ``--''. Support for bundling of command line options, as was
  91. the case with the more traditional single-letter approach, is provided
  92. but not enabled by default.</P>
  93. <P>
  94. <HR>
  95. <H1><A NAME="command line options, an introduction">Command Line Options, an Introduction</A></H1>
  96. <P>Command line operated programs traditionally take their arguments from
  97. the command line, for example filenames or other information that the
  98. program needs to know. Besides arguments, these programs often take
  99. command line <EM>options</EM> as well. Options are not necessary for the
  100. program to work, hence the name 'option', but are used to modify its
  101. default behaviour. For example, a program could do its job quietly,
  102. but with a suitable option it could provide verbose information about
  103. what it did.</P>
  104. <P>Command line options come in several flavours. Historically, they are
  105. preceded by a single dash <CODE>-</CODE>, and consist of a single letter.</P>
  106. <PRE>
  107.     -l -a -c</PRE>
  108. <P>Usually, these single-character options can be bundled:</P>
  109. <PRE>
  110.     -lac</PRE>
  111. <P>Options can have values, the value is placed after the option
  112. character. Sometimes with whitespace in between, sometimes not:</P>
  113. <PRE>
  114.     -s 24 -s24</PRE>
  115. <P>Due to the very cryptic nature of these options, another style was
  116. developed that used long names. So instead of a cryptic <CODE>-l</CODE> one
  117. could use the more descriptive <CODE>--long</CODE>. To distinguish between a
  118. bundle of single-character options and a long one, two dashes are used
  119. to precede the option name. Early implementations of long options used
  120. a plus <A HREF="#item_%2B"><CODE>+</CODE></A> instead. Also, option values could be specified either
  121. like</P>
  122. <PRE>
  123.     --size=24</PRE>
  124. <P>or</P>
  125. <PRE>
  126.     --size 24</PRE>
  127. <P>The <A HREF="#item_%2B"><CODE>+</CODE></A> form is now obsolete and strongly deprecated.</P>
  128. <P>
  129. <HR>
  130. <H1><A NAME="getting started with getopt::long">Getting Started with Getopt::Long</A></H1>
  131. <P>Getopt::Long is the Perl5 successor of <CODE>newgetopt.pl</CODE>. This was
  132. the firs Perl module that provided support for handling the new style
  133. of command line options, hence the name Getopt::Long. This module
  134. also supports single-character options and bundling. In this case, the
  135. options are restricted to alphabetic characters only, and the
  136. characters <CODE>?</CODE> and <CODE>-</CODE>.</P>
  137. <P>To use Getopt::Long from a Perl program, you must include the
  138. following line in your Perl program:</P>
  139. <PRE>
  140.     use Getopt::Long;</PRE>
  141. <P>This will load the core of the Getopt::Long module and prepare your
  142. program for using it. Most of the actual Getopt::Long code is not
  143. loaded until you really call one of its functions.</P>
  144. <P>In the default configuration, options names may be abbreviated to
  145. uniqueness, case does not matter, and a single dash is sufficient,
  146. even for long option names. Also, options may be placed between
  147. non-option arguments. See <A HREF="#configuring getopt::long">Configuring Getopt::Long</A> for more
  148. details on how to configure Getopt::Long.</P>
  149. <P>
  150. <H2><A NAME="simple options">Simple options</A></H2>
  151. <P>The most simple options are the ones that take no values. Their mere
  152. presence on the command line enables the option. Popular examples are:</P>
  153. <PRE>
  154.     --all --verbose --quiet --debug</PRE>
  155. <P>Handling simple options is straightforward:</P>
  156. <PRE>
  157.     my $verbose = '';   # option variable with default value (false)
  158.     my $all = '';       # option variable with default value (false)
  159.     GetOptions ('verbose' => \$verbose, 'all' => \$all);</PRE>
  160. <P>The call to <CODE>GetOptions()</CODE> parses the command line arguments that are
  161. present in <CODE>@ARGV</CODE> and sets the option variable to the value <CODE>1</CODE> if
  162. the option did occur on the command line. Otherwise, the option
  163. variable is not touched. Setting the option value to true is often
  164. called <EM>enabling</EM> the option.</P>
  165. <P>The option name as specified to the <CODE>GetOptions()</CODE> function is called
  166. the option <EM>specification</EM>. Later we'll see that this specification
  167. can contain more than just the option name. The reference to the
  168. variable is called the option <EM>destination</EM>.</P>
  169. <P><CODE>GetOptions()</CODE> will return a true value if the command line could be
  170. processed successfully. Otherwise, it will write error messages to
  171. STDERR, and return a false result.</P>
  172. <P>
  173. <H2><A NAME="a little bit less simple options">A little bit less simple options</A></H2>
  174. <P>Getopt::Long supports two useful variants of simple options:
  175. <EM>negatable</EM> options and <EM>incremental</EM> options.</P>
  176. <P>A negatable option is specified with a exclamation mark <A HREF="#item_%21"><CODE>!</CODE></A> after the
  177. option name:</P>
  178. <PRE>
  179.     my $verbose = '';   # option variable with default value (false)
  180.     GetOptions ('verbose!' => \$verbose);</PRE>
  181. <P>Now, using <CODE>--verbose</CODE> on the command line will enable <CODE>$verbose</CODE>,
  182. as expected. But it is also allowed to use <CODE>--noverbose</CODE>, which will
  183. disable <CODE>$verbose</CODE> by setting its value to <CODE>0</CODE>. Using a suitable
  184. default value, the program can find out whether <CODE>$verbose</CODE> is false
  185. by default, or disabled by using <CODE>--noverbose</CODE>.</P>
  186. <P>An incremental option is specified with a plus <A HREF="#item_%2B"><CODE>+</CODE></A> after the
  187. option name:</P>
  188. <PRE>
  189.     my $verbose = '';   # option variable with default value (false)
  190.     GetOptions ('verbose+' => \$verbose);</PRE>
  191. <P>Using <CODE>--verbose</CODE> on the command line will increment the value of
  192. <CODE>$verbose</CODE>. This way the program can keep track of how many times the
  193. option occurred on the command line. For example, each occurrence of
  194. <CODE>--verbose</CODE> could increase the verbosity level of the program.</P>
  195. <P>
  196. <H2><A NAME="mixing command line option with other arguments">Mixing command line option with other arguments</A></H2>
  197. <P>Usually programs take command line options as well as other arguments,
  198. for example, file names. It is good practice to always specify the
  199. options first, and the other arguments last. Getopt::Long will,
  200. however, allow the options and arguments to be mixed and 'filter out'
  201. all the options before passing the rest of the arguments to the
  202. program. To stop Getopt::Long from processing further arguments,
  203. insert a double dash <CODE>--</CODE> on the command line:</P>
  204. <PRE>
  205.     --size 24 -- --all</PRE>
  206. <P>In this example, <CODE>--all</CODE> will <EM>not</EM> be treated as an option, but
  207. passed to the program unharmed, in <CODE>@ARGV</CODE>.</P>
  208. <P>
  209. <H2><A NAME="options with values">Options with values</A></H2>
  210. <P>For options that take values it must be specified whether the option
  211. value is required or not, and what kind of value the option expects.</P>
  212. <P>Three kinds of values are supported: integer numbers, floating point
  213. numbers, and strings.</P>
  214. <P>If the option value is required, Getopt::Long will take the
  215. command line argument that follows the option and assign this to the
  216. option variable. If, however, the option value is specified as
  217. optional, this will only be done if that value does not look like a
  218. valid command line option itself.</P>
  219. <PRE>
  220.     my $tag = '';       # option variable with default value
  221.     GetOptions ('tag=s' => \$tag);</PRE>
  222. <P>In the option specification, the option name is followed by an equals
  223. sign <CODE>=</CODE> and the letter <A HREF="#item_s"><CODE>s</CODE></A>. The equals sign indicates that this
  224. option requires a value. The letter <A HREF="#item_s"><CODE>s</CODE></A> indicates that this value is
  225. an arbitrary string. Other possible value types are <A HREF="#item_i"><CODE>i</CODE></A> for integer
  226. values, and <A HREF="#item_f"><CODE>f</CODE></A> for floating point values. Using a colon <CODE>:</CODE> instead
  227. of the equals sign indicates that the option value is optional. In
  228. this case, if no suitable value is supplied, string valued options get
  229. an empty string <CODE>''</CODE> assigned, while numeric options are set to <CODE>0</CODE>.</P>
  230. <P>
  231. <H2><A NAME="options with multiple values">Options with multiple values</A></H2>
  232. <P>Options sometimes take several values. For example, a program could
  233. use multiple directories to search for library files:</P>
  234. <PRE>
  235.     --library lib/stdlib --library lib/extlib</PRE>
  236. <P>To accomplish this behaviour, simply specify an array reference as the
  237. destination for the option:</P>
  238. <PRE>
  239.     my @libfiles = ();
  240.     GetOptions ("library=s" => \@libfiles);</PRE>
  241. <P>Used with the example above, <CODE>@libfiles</CODE> would contain two strings
  242. upon completion: <CODE>"lib/srdlib"</CODE> and <CODE>"lib/extlib"</CODE>, in that order.
  243. It is also possible to specify that only integer or floating point
  244. numbers are acceptible values.</P>
  245. <P>Often it is useful to allow comma-separated lists of values as well as
  246. multiple occurrences of the options. This is easy using Perl's <A HREF="../../lib/Pod/perlfunc.html#item_split"><CODE>split()</CODE></A>
  247. and <A HREF="../../lib/Pod/perlfunc.html#item_join"><CODE>join()</CODE></A> operators:</P>
  248. <PRE>
  249.     my @libfiles = ();
  250.     GetOptions ("library=s" => \@libfiles);
  251.     @libfiles = split(/,/,join(',',@libfiles));</PRE>
  252. <P>Of course, it is important to choose the right separator string for
  253. each purpose.</P>
  254. <P>
  255. <H2><A NAME="options with hash values">Options with hash values</A></H2>
  256. <P>If the option destination is a reference to a hash, the option will
  257. take, as value, strings of the form <EM>key</EM><CODE>=</CODE><EM>value</EM>. The value will
  258. be stored with the specified key in the hash.</P>
  259. <PRE>
  260.     my %defines = ();
  261.     GetOptions ("define=s" => \%defines);</PRE>
  262. <P>When used with command line options:</P>
  263. <PRE>
  264.     --define os=linux --define vendor=redhat</PRE>
  265. <P>the hash <CODE>%defines</CODE> will contain two keys, <CODE>"os"</CODE> with value
  266. <CODE>"linux</CODE> and <CODE>"vendor"</CODE> with value <CODE>"redhat"</CODE>.
  267. It is also possible to specify that only integer or floating point
  268. numbers are acceptible values. The keys are always taken to be strings.</P>
  269. <P>
  270. <H2><A NAME="userdefined subroutines to handle options">User-defined subroutines to handle options</A></H2>
  271. <P>Ultimate control over what should be done when (actually: each time)
  272. an option is encountered on the command line can be achieved by
  273. designating a reference to a subroutine (or an anonymous subroutine)
  274. as the option destination. When <CODE>GetOptions()</CODE> encounters the option, it
  275. will call the subroutine with two arguments: the name of the option,
  276. and the value to be assigned. It is up to the subroutine to store the
  277. value, or do whatever it thinks is appropriate.</P>
  278. <P>A trivial application of this mechanism is to implement options that
  279. are related to each other. For example:</P>
  280. <PRE>
  281.     my $verbose = '';   # option variable with default value (false)
  282.     GetOptions ('verbose' => \$verbose,
  283.                 'quiet'   => sub { $verbose = 0 });</PRE>
  284. <P>Here <CODE>--verbose</CODE> and <CODE>--quiet</CODE> control the same variable
  285. <CODE>$verbose</CODE>, but with opposite values.</P>
  286. <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
  287. the desired error message as its argument. <CODE>GetOptions()</CODE> will catch the
  288. die(), issue the error message, and record that an error result must
  289. be returned upon completion.</P>
  290. <P>If the text of the error message starts with an exclamantion mark <A HREF="#item_%21"><CODE>!</CODE></A>
  291. it is interpreted specially by GetOptions(). There is currently one
  292. special command implemented: <A HREF="../../lib/Pod/perlfunc.html#item_die"><CODE>die("!FINISH")</CODE></A> will cause <CODE>GetOptions()</CODE>
  293. to stop processing options, as if it encountered a double dash <CODE>--</CODE>.</P>
  294. <P>
  295. <H2><A NAME="options with multiple names">Options with multiple names</A></H2>
  296. <P>Often it is user friendly to supply alternate mnemonic names for
  297. options. For example <CODE>--height</CODE> could be an alternate name for
  298. <CODE>--length</CODE>. Alternate names can be included in the option
  299. specification, separated by vertical bar <CODE>|</CODE> characters. To implement
  300. the above example:</P>
  301. <PRE>
  302.     GetOptions ('length|height=f' => \$length);</PRE>
  303. <P>The first name is called the <EM>primary</EM> name, the other names are
  304. called <EM>aliases</EM>.</P>
  305. <P>Multiple alternate names are possible.</P>
  306. <P>
  307. <H2><A NAME="case and abbreviations">Case and abbreviations</A></H2>
  308. <P>Without additional configuration, <CODE>GetOptions()</CODE> will ignore the case of
  309. option names, and allow the options to be abbreviated to uniqueness.</P>
  310. <PRE>
  311.     GetOptions ('length|height=f' => \$length, "head" => \$head);</PRE>
  312. <P>This call will allow <CODE>--l</CODE> and <CODE>--L</CODE> for the length option, but
  313. requires a least <CODE>--hea</CODE> and <CODE>--hei</CODE> for the head and height options.</P>
  314. <P>
  315. <H2><A NAME="summary of option specifications">Summary of Option Specifications</A></H2>
  316. <P>Each option specifier consists of two parts: the name specification
  317. and the argument specification.</P>
  318. <P>The name specification contains the name of the option, optionally
  319. followed by a list of alternative names separated by vertical bar
  320. characters.</P>
  321. <PRE>
  322.     length            option name is "length"
  323.     length|size|l     name is "length", aliases are "size" and "l"</PRE>
  324. <P>The argument specification is optional. If omitted, the option is
  325. considered boolean, a value of 1 will be assigned when the option is
  326. used on the command line.</P>
  327. <P>The argument specification can be</P>
  328. <DL>
  329. <DT><STRONG><A NAME="item_%21">!</A></STRONG><BR>
  330. <DD>
  331. The option does not take an argument and may be negated, i.e. prefixed
  332. by ``no''. E.g. <CODE>"foo!"</CODE> will allow <CODE>--foo</CODE> (a value of 1 will be
  333. assigned) and <CODE>--nofoo</CODE> (a value of 0 will be assigned). If the
  334. option has aliases, this applies to the aliases as well.
  335. <P>Using negation on a single letter option when bundling is in effect is
  336. pointless and will result in a warning.</P>
  337. <P></P>
  338. <DT><STRONG><A NAME="item_%2B">+</A></STRONG><BR>
  339. <DD>
  340. The option does not take an argument and will be incremented by 1
  341. every time it appears on the command line. E.g. <CODE>"more+"</CODE>, when used
  342. with <CODE>--more --more --more</CODE>, will increment the value three times,
  343. resulting in a value of 3 (provided it was 0 or undefined at first).
  344. <P>The <A HREF="#item_%2B"><CODE>+</CODE></A> specifier is ignored if the option destination is not a scalar.</P>
  345. <P></P>
  346. <DT><STRONG><A NAME="item_%3D_type_%5B_desttype_%5D">= <EM>type</EM> [ <EM>desttype</EM> ]</A></STRONG><BR>
  347. <DD>
  348. The option requires an argument of the given type. Supported types
  349. are:
  350. <DL>
  351. <DT><STRONG><A NAME="item_s">s</A></STRONG><BR>
  352. <DD>
  353. String. An arbitrary sequence of characters. It is valid for the
  354. argument to start with <CODE>-</CODE> or <CODE>--</CODE>.
  355. <P></P>
  356. <DT><STRONG><A NAME="item_i">i</A></STRONG><BR>
  357. <DD>
  358. Integer. An optional leading plus or minus sign, followed by a
  359. sequence of digits.
  360. <P></P>
  361. <DT><STRONG><A NAME="item_f">f</A></STRONG><BR>
  362. <DD>
  363. Real number. For example <CODE>3.14</CODE>, <CODE>-6.23E24</CODE> and so on.
  364. <P></P></DL>
  365. <P>The <EM>desttype</EM> can be <CODE>@</CODE> or <CODE>%</CODE> to specify that the option is
  366. list or a hash valued. This is only needed when the destination for
  367. the option value is not otherwise specified. It should be omitted when
  368. not needed.</P>
  369. <DT><STRONG><A NAME="item_%3A_type_%5B_desttype_%5D">: <EM>type</EM> [ <EM>desttype</EM> ]</A></STRONG><BR>
  370. <DD>
  371. Like <CODE>=</CODE>, but designates the argument as optional.
  372. If omitted, an empty string will be assigned to string values options,
  373. and the value zero to numeric options.
  374. <P>Note that if a string argument starts with <CODE>-</CODE> or <CODE>--</CODE>, it will be
  375. considered an option on itself.</P>
  376. <P></P></DL>
  377. <P>
  378. <HR>
  379. <H1><A NAME="advanced possibilities">Advanced Possibilities</A></H1>
  380. <P>
  381. <H2><A NAME="documentation and help texts">Documentation and help texts</A></H2>
  382. <P>Getopt::Long encourages the use of Pod::Usage to produce help
  383. messages. For example:</P>
  384. <PRE>
  385.     use Getopt::Long;
  386.     use Pod::Usage;</PRE>
  387. <PRE>
  388.     my $man = 0;
  389.     my $help = 0;</PRE>
  390. <PRE>
  391.     GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
  392.     pod2usage(1) if $help;
  393.     pod2usage(-exitstatus => 0, -verbose => 2) if $man;</PRE>
  394. <PRE>
  395.     __END__</PRE>
  396. <PRE>
  397.     =head1 NAME</PRE>
  398. <PRE>
  399.     sample - Using GetOpt::Long and Pod::Usage</PRE>
  400. <PRE>
  401.     =head1 SYNOPSIS</PRE>
  402. <PRE>
  403.     sample [options] [file ...]</PRE>
  404. <PRE>
  405.      Options:
  406.        -help            brief help message
  407.        -man             full documentation</PRE>
  408. <PRE>
  409.     =head1 OPTIONS</PRE>
  410. <PRE>
  411.     =over 8</PRE>
  412. <PRE>
  413.     =item B<-help></PRE>
  414. <PRE>
  415.     Print a brief help message and exits.</PRE>
  416. <PRE>
  417.     =item B<-man></PRE>
  418. <PRE>
  419.     Prints the manual page and exits.</PRE>
  420. <PRE>
  421.     =back</PRE>
  422. <PRE>
  423.     =head1 DESCRIPTION</PRE>
  424. <PRE>
  425.     B<This program> will read the given input file(s) and do someting
  426.     useful with the contents thereof.</PRE>
  427. <PRE>
  428.     =cut</PRE>
  429. <P>See <A HREF="../../lib/Pod/Usage.html">the Pod::Usage manpage</A> for details.</P>
  430. <P>
  431. <H2><A NAME="storing options in a hash">Storing options in a hash</A></H2>
  432. <P>Sometimes, for example when there are a lot of options, having a
  433. separate variable for each of them can be cumbersome. <CODE>GetOptions()</CODE>
  434. supports, as an alternative mechanism, storing options in a hash.</P>
  435. <P>To obtain this, a reference to a hash must be passed <EM>as the first
  436. argument</EM> to GetOptions(). For each option that is specified on the
  437. command line, the option value will be stored in the hash with the
  438. option name as key. Options that are not actually used on the command
  439. line will not be put in the hash, on other words,
  440. <A HREF="../../lib/Pod/perlfunc.html#item_exists"><CODE>exists($h{option})</CODE></A> (or <A HREF="../../lib/Pod/perlfunc.html#item_defined"><CODE>defined())</CODE></A> can be used to test if an option
  441. was used. The drawback is that warnings will be issued if the program
  442. runs under <CODE>use strict</CODE> and uses <CODE>$h{option}</CODE> without testing with
  443. <A HREF="../../lib/Pod/perlfunc.html#item_exists"><CODE>exists()</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_defined"><CODE>defined()</CODE></A> first.</P>
  444. <PRE>
  445.     my %h = ();
  446.     GetOptions (\%h, 'length=i');       # will store in $h{length}</PRE>
  447. <P>For options that take list or hash values, it is necessary to indicate
  448. this by appending an <CODE>@</CODE> or <CODE>%</CODE> sign after the type:</P>
  449. <PRE>
  450.     GetOptions (\%h, 'colours=s@');     # will push to @{$h{colours}}</PRE>
  451. <P>To make things more complicated, the hash may contain references to
  452. the actual destinations, for example:</P>
  453. <PRE>
  454.     my $len = 0;
  455.     my %h = ('length' => \$len);
  456.     GetOptions (\%h, 'length=i');       # will store in $len</PRE>
  457. <P>This example is fully equivalent with:</P>
  458. <PRE>
  459.     my $len = 0;
  460.     GetOptions ('length=i' => \$len);   # will store in $len</PRE>
  461. <P>Any mixture is possible. For example, the most frequently used options
  462. could be stored in variables while all other options get stored in the
  463. hash:</P>
  464. <PRE>
  465.     my $verbose = 0;                    # frequently referred
  466.     my $debug = 0;                      # frequently referred
  467.     my %h = ('verbose' => \$verbose, 'debug' => \$debug);
  468.     GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i');
  469.     if ( $verbose ) { ... }
  470.     if ( exists $h{filter} ) { ... option 'filter' was specified ... }</PRE>
  471. <P>
  472. <H2><A NAME="bundling">Bundling</A></H2>
  473. <P>With bundling it is possible to set several single-character options
  474. at once. For example if <CODE>a</CODE>, <CODE>v</CODE> and <CODE>x</CODE> are all valid options,</P>
  475. <PRE>
  476.     -vax</PRE>
  477. <P>would set all three.</P>
  478. <P>Getopt::Long supports two levels of bundling. To enable bundling, a
  479. call to Getopt::Long::Configure is required.</P>
  480. <P>The first level of bundling can be enabled with:</P>
  481. <PRE>
  482.     Getopt::Long::Configure ("bundling");</PRE>
  483. <P>Configured this way, single-character options can be bundled but long
  484. options <STRONG>must</STRONG> always start with a double dash <CODE>--</CODE> to avoid
  485. abiguity. For example, when <CODE>vax</CODE>, <CODE>a</CODE>, <CODE>v</CODE> and <CODE>x</CODE> are all valid
  486. options,</P>
  487. <PRE>
  488.     -vax</PRE>
  489. <P>would set <CODE>a</CODE>, <CODE>v</CODE> and <CODE>x</CODE>, but</P>
  490. <PRE>
  491.     --vax</PRE>
  492. <P>would set <CODE>vax</CODE>.</P>
  493. <P>The second level of bundling lifts this restriction. It can be enabled
  494. with:</P>
  495. <PRE>
  496.     Getopt::Long::Configure ("bundling_override");</PRE>
  497. <P>Now, <CODE>-vax</CODE> would set the option <CODE>vax</CODE>.</P>
  498. <P>When any level of bundling is enabled, option values may be inserted
  499. in the bundle. For example:</P>
  500. <PRE>
  501.     -h24w80</PRE>
  502. <P>is equivalent to</P>
  503. <PRE>
  504.     -h 24 -w 80</PRE>
  505. <P>When configured for bundling, single-character options are matched
  506. case sensitive while long options are matched case insensitive. To
  507. have the single-character options matched case insensitive as well,
  508. use:</P>
  509. <PRE>
  510.     Getopt::Long::Configure ("bundling", "ignorecase_always");</PRE>
  511. <P>It goes without saying that bundling can be quite confusing.</P>
  512. <P>
  513. <H2><A NAME="the lonesome dash">The lonesome dash</A></H2>
  514. <P>Some applications require the option <CODE>-</CODE> (that's a lone dash). This
  515. can be achieved by adding an option specification with an empty name:</P>
  516. <PRE>
  517.     GetOptions ('' => \$stdio);</PRE>
  518. <P>A lone dash on the command line will now be legal, and set options
  519. variable <CODE>$stdio</CODE>.</P>
  520. <P>
  521. <H2><A NAME="argument callback">Argument call-back</A></H2>
  522. <P>A special option 'name' <CODE><</CODE>> can be used to designate a subroutine
  523. to handle non-option arguments. When <CODE>GetOptions()</CODE> encounters an
  524. argument that does not look like an option, it will immediately call this
  525. subroutine and passes it the argument as a parameter.</P>
  526. <P>For example:</P>
  527. <PRE>
  528.     my $width = 80;
  529.     sub process { ... }
  530.     GetOptions ('width=i' => \$width, '<>' => \&process);</PRE>
  531. <P>When applied to the following command line:</P>
  532. <PRE>
  533.     arg1 --width=72 arg2 --width=60 arg3</PRE>
  534. <P>This will call 
  535. <CODE>process("arg1")</CODE> while <CODE>$width</CODE> is <CODE>80</CODE>, 
  536. <CODE>process("arg2")</CODE> while <CODE>$width</CODE> is <CODE>72</CODE>, and
  537. <CODE>process("arg3")</CODE> while <CODE>$width</CODE> is <CODE>60</CODE>.</P>
  538. <P>This feature requires configuration option <STRONG>permute</STRONG>, see section
  539. <A HREF="#configuring getopt::long">Configuring Getopt::Long</A>.</P>
  540. <P>
  541. <HR>
  542. <H1><A NAME="configuring getopt::long">Configuring Getopt::Long</A></H1>
  543. <P>Getopt::Long can be configured by calling subroutine
  544. Getopt::Long::Configure(). This subroutine takes a list of quoted
  545. strings, each specifying a configuration option to be set, e.g.
  546. <A HREF="#item_ignore_case"><CODE>ignore_case</CODE></A>, or reset, e.g. <CODE>no_ignore_case</CODE>. Case does not
  547. matter. Multiple calls to <CODE>Configure()</CODE> are possible.</P>
  548. <P>The following options are available:</P>
  549. <DL>
  550. <DT><STRONG><A NAME="item_default">default</A></STRONG><BR>
  551. <DD>
  552. This option causes all configuration options to be reset to their
  553. default values.
  554. <P></P>
  555. <DT><STRONG><A NAME="item_auto_abbrev">auto_abbrev</A></STRONG><BR>
  556. <DD>
  557. Allow option names to be abbreviated to uniqueness.
  558. Default is set unless environment variable
  559. POSIXLY_CORRECT has been set, in which case <A HREF="#item_auto_abbrev"><CODE>auto_abbrev</CODE></A> is reset.
  560. <P></P>
  561. <DT><STRONG><A NAME="item_getopt_compat">getopt_compat</A></STRONG><BR>
  562. <DD>
  563. Allow <A HREF="#item_%2B"><CODE>+</CODE></A> to start options.
  564. Default is set unless environment variable
  565. POSIXLY_CORRECT has been set, in which case <A HREF="#item_getopt_compat"><CODE>getopt_compat</CODE></A> is reset.
  566. <P></P>
  567. <DT><STRONG><A NAME="item_require_order">require_order</A></STRONG><BR>
  568. <DD>
  569. Whether command line arguments are allowed to be mixed with options.
  570. Default is set unless environment variable
  571. POSIXLY_CORRECT has been set, in which case <A HREF="#item_require_order"><CODE>require_order</CODE></A> is reset.
  572. <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>
  573. <P></P>
  574. <DT><STRONG><A NAME="item_permute">permute</A></STRONG><BR>
  575. <DD>
  576. Whether command line arguments are allowed to be mixed with options.
  577. Default is set unless environment variable
  578. POSIXLY_CORRECT has been set, in which case <A HREF="#item_permute"><CODE>permute</CODE></A> is reset.
  579. Note that <A HREF="#item_permute"><CODE>permute</CODE></A> is the opposite of <A HREF="#item_require_order"><CODE>require_order</CODE></A>.
  580. <P>If <A HREF="#item_permute"><CODE>permute</CODE></A> is set, this means that</P>
  581. <PRE>
  582.     --foo arg1 --bar arg2 arg3</PRE>
  583. <P>is equivalent to</P>
  584. <PRE>
  585.     --foo --bar arg1 arg2 arg3</PRE>
  586. <P>If an argument call-back routine is specified, <CODE>@ARGV</CODE> will always be
  587. empty upon succesful return of <CODE>GetOptions()</CODE> since all options have been
  588. processed. The only exception is when <CODE>--</CODE> is used:</P>
  589. <PRE>
  590.     --foo arg1 --bar arg2 -- arg3</PRE>
  591. <P>will call the call-back routine for arg1 and arg2, and terminate
  592. <CODE>GetOptions()</CODE> leaving <CODE>"arg2"</CODE> in <CODE>@ARGV</CODE>.</P>
  593. <P>If <A HREF="#item_require_order"><CODE>require_order</CODE></A> is set, options processing
  594. terminates when the first non-option is encountered.</P>
  595. <PRE>
  596.     --foo arg1 --bar arg2 arg3</PRE>
  597. <P>is equivalent to</P>
  598. <PRE>
  599.     --foo -- arg1 --bar arg2 arg3</PRE>
  600. <P></P>
  601. <DT><STRONG><A NAME="item_bundling">bundling (default: reset)</A></STRONG><BR>
  602. <DD>
  603. Setting this option will allow single-character options to be bundled.
  604. To distinguish bundles from long option names, long options <EM>must</EM> be
  605. introduced with <CODE>--</CODE> and single-character options (and bundles) with
  606. <CODE>-</CODE>.
  607. <P>Note: resetting <A HREF="#item_bundling"><CODE>bundling</CODE></A> also resets <A HREF="#item_bundling_override"><CODE>bundling_override</CODE></A>.</P>
  608. <P></P>
  609. <DT><STRONG><A NAME="item_bundling_override">bundling_override (default: reset)</A></STRONG><BR>
  610. <DD>
  611. If <A HREF="#item_bundling_override"><CODE>bundling_override</CODE></A> is set, bundling is enabled as with
  612. <A HREF="#item_bundling"><CODE>bundling</CODE></A> but now long option names override option bundles.
  613. <P>Note: resetting <A HREF="#item_bundling_override"><CODE>bundling_override</CODE></A> also resets <A HREF="#item_bundling"><CODE>bundling</CODE></A>.</P>
  614. <P><STRONG>Note:</STRONG> Using option bundling can easily lead to unexpected results,
  615. especially when mixing long options and bundles. Caveat emptor.</P>
  616. <P></P>
  617. <DT><STRONG><A NAME="item_ignore_case">ignore_case  (default: set)</A></STRONG><BR>
  618. <DD>
  619. If set, case is ignored when matching long option names. Single
  620. character options will be treated case-sensitive.
  621. <P>Note: resetting <A HREF="#item_ignore_case"><CODE>ignore_case</CODE></A> also resets <A HREF="#item_ignore_case_always"><CODE>ignore_case_always</CODE></A>.</P>
  622. <P></P>
  623. <DT><STRONG><A NAME="item_ignore_case_always">ignore_case_always (default: reset)</A></STRONG><BR>
  624. <DD>
  625. When bundling is in effect, case is ignored on single-character
  626. options also.
  627. <P>Note: resetting <A HREF="#item_ignore_case_always"><CODE>ignore_case_always</CODE></A> also resets <A HREF="#item_ignore_case"><CODE>ignore_case</CODE></A>.</P>
  628. <P></P>
  629. <DT><STRONG><A NAME="item_pass_through">pass_through (default: reset)</A></STRONG><BR>
  630. <DD>
  631. Options that are unknown, ambiguous or supplied with an invalid option
  632. value are passed through in <CODE>@ARGV</CODE> instead of being flagged as
  633. errors. This makes it possible to write wrapper scripts that process
  634. only part of the user supplied command line arguments, and pass the
  635. remaining options to some other program.
  636. <P>This can be very confusing, especially when <A HREF="#item_permute"><CODE>permute</CODE></A> is also set.</P>
  637. <P></P>
  638. <DT><STRONG><A NAME="item_prefix">prefix</A></STRONG><BR>
  639. <DD>
  640. The string that starts options. If a constant string is not
  641. sufficient, see <A HREF="#item_prefix_pattern"><CODE>prefix_pattern</CODE></A>.
  642. <P></P>
  643. <DT><STRONG><A NAME="item_prefix_pattern">prefix_pattern</A></STRONG><BR>
  644. <DD>
  645. A Perl pattern that identifies the strings that introduce options.
  646. Default is <CODE>(--|-|\+)</CODE> unless environment variable
  647. POSIXLY_CORRECT has been set, in which case it is <CODE>(--|-)</CODE>.
  648. <P></P>
  649. <DT><STRONG><A NAME="item_debug">debug (default: reset)</A></STRONG><BR>
  650. <DD>
  651. Enable copious debugging output.
  652. <P></P></DL>
  653. <P>
  654. <HR>
  655. <H1><A NAME="return values and errors">Return values and Errors</A></H1>
  656. <P>Configuration errors and errors in the option definitions are
  657. signalled using <A HREF="../../lib/Pod/perlfunc.html#item_die"><CODE>die()</CODE></A> and will terminate the calling program unless
  658. the call to Getopt::Long::GetOptions() was embedded in <CODE>eval { ...
  659. }</CODE>, or <A HREF="../../lib/Pod/perlfunc.html#item_die"><CODE>die()</CODE></A> was trapped using <CODE>$SIG{__DIE__}</CODE>.</P>
  660. <P>A return value of 1 (true) indicates success.</P>
  661. <P>A return status of 0 (false) indicates that the function detected one
  662. or more errors during option parsing. These errors are signalled using
  663. <A HREF="../../lib/Pod/perlfunc.html#item_warn"><CODE>warn()</CODE></A> and can be trapped with <CODE>$SIG{__WARN__}</CODE>.</P>
  664. <P>Errors that can't happen are signalled using Carp::croak().</P>
  665. <P>
  666. <HR>
  667. <H1><A NAME="legacy">Legacy</A></H1>
  668. <P>The earliest development of <CODE>newgetopt.pl</CODE> started in 1990, with Perl
  669. version 4. As a result, its development, and the development of
  670. Getopt::Long, has gone through several stages. Since backward
  671. compatibility has always been extremely important, the current version
  672. of Getopt::Long still supports a lot of constructs that nowadays are
  673. no longer necessary or otherwise unwanted. This section describes
  674. briefly some of these 'features'.</P>
  675. <P>
  676. <H2><A NAME="default destinations">Default destinations</A></H2>
  677. <P>When no destination is specified for an option, GetOptions will store
  678. the resultant value in a global variable named <CODE>opt_</CODE><EM>XXX</EM>, where
  679. <EM>XXX</EM> is the primary name of this option. When a progam executes
  680. under <CODE>use strict</CODE> (recommended), these variables must be
  681. pre-declared with <A HREF="../../lib/Pod/perlfunc.html#item_our"><CODE>our()</CODE></A> or <CODE>use vars</CODE>.</P>
  682. <PRE>
  683.     our $opt_length = 0;
  684.     GetOptions ('length=i');    # will store in $opt_length</PRE>
  685. <P>To yield a usable Perl variable, characters that are not part of the
  686. syntax for variables are translated to underscores. For example,
  687. <CODE>--fpp-struct-return</CODE> will set the variable
  688. <CODE>$opt_fpp_struct_return</CODE>. Note that this variable resides in the
  689. namespace of the calling program, not necessarily <CODE>main</CODE>. For
  690. example:</P>
  691. <PRE>
  692.     GetOptions ("size=i", "sizes=i@");</PRE>
  693. <P>with command line ``-size 10 -sizes 24 -sizes 48'' will perform the
  694. equivalent of the assignments</P>
  695. <PRE>
  696.     $opt_size = 10;
  697.     @opt_sizes = (24, 48);</PRE>
  698. <P>
  699. <H2><A NAME="alternative option starters">Alternative option starters</A></H2>
  700. <P>A string of alternative option starter characters may be passed as the
  701. first argument (or the first argument after a leading hash reference
  702. argument).</P>
  703. <PRE>
  704.     my $len = 0;
  705.     GetOptions ('/', 'length=i' => $len);</PRE>
  706. <P>Now the command line may look like:</P>
  707. <PRE>
  708.     /length 24 -- arg</PRE>
  709. <P>Note that to terminate options processing still requires a double dash
  710. <CODE>--</CODE>.</P>
  711. <P><CODE>GetOptions()</CODE> will not interpret a leading <CODE>"<</CODE>``> as option starters
  712. if the next argument is a reference. To force <CODE>"<"</CODE> and <CODE>"</CODE>''> as
  713. option starters, use <CODE>"</CODE><``>. Confusing? Well, <STRONG>using a starter
  714. argument is strongly deprecated</STRONG> anyway.</P>
  715. <P>
  716. <H2><A NAME="configuration variables">Configuration variables</A></H2>
  717. <P>Previous versions of Getopt::Long used variables for the purpose of
  718. configuring. Although manipulating these variables still work, it
  719. is strongly encouraged to use the new <CODE>config</CODE> routine. Besides, it
  720. is much easier.</P>
  721. <P>
  722. <HR>
  723. <H1><A NAME="author">AUTHOR</A></H1>
  724. <P>Johan Vromans <<A HREF="mailto:jvromans@squirrel.nl">jvromans@squirrel.nl</A>></P>
  725. <P>
  726. <HR>
  727. <H1><A NAME="copyright and disclaimer">COPYRIGHT AND DISCLAIMER</A></H1>
  728. <P>This program is Copyright 2000,1990 by Johan Vromans.
  729. This program is free software; you can redistribute it and/or
  730. modify it under the terms of the Perl Artistic License or the
  731. GNU General Public License as published by the Free Software
  732. Foundation; either version 2 of the License, or (at your option) any
  733. later version.</P>
  734. <P>This program is distributed in the hope that it will be useful,
  735. but WITHOUT ANY WARRANTY; without even the implied warranty of
  736. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  737. GNU General Public License for more details.</P>
  738. <P>If you do not have a copy of the GNU General Public License write to
  739. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
  740. MA 02139, USA.</P>
  741. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  742. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  743. <STRONG><P CLASS=block> Getopt::Long - Extended processing of command line options</P></STRONG>
  744. </TD></TR>
  745. </TABLE>
  746.  
  747. </BODY>
  748.  
  749. </HTML>
  750.