home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D12
/
PERL1.TGZ
/
perl1.tar
/
usr
/
lib
/
perl5
/
Getopt
/
Long.pm
next >
Wrap
Text File
|
1996-06-28
|
26KB
|
892 lines
# GetOpt::Long.pm -- POSIX compatible options parsing
# RCS Status : $Id: GetoptLong.pm,v 2.1 1996/02/02 20:24:35 jv Exp $
# Author : Johan Vromans
# Created On : Tue Sep 11 15:00:12 1990
# Last Modified By: Johan Vromans
# Last Modified On: Fri Feb 2 21:24:32 1996
# Update Count : 347
# Status : Released
package Getopt::Long;
require 5.000;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
$VERSION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/);
use strict;
=head1 NAME
GetOptions - extended processing of command line options
=head1 SYNOPSIS
use Getopt::Long;
$result = GetOptions (...option-descriptions...);
=head1 DESCRIPTION
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 "--". There is no bundling of command line options, as was
the case with the more traditional single-letter approach. For
example, the UNIX "ps" command can be given the command line "option"
-vax
which means the combination of B<-v>, B<-a> and B<-x>. With the new
syntax B<--vax> would be a single option, probably indicating a
computer architecture.
Command line options can be used to set values. These values can be
specified in one of two ways:
--size 24
--size=24
GetOptions is called with a list of option-descriptions, each of which
consists of two elements: the option specifier and the option linkage.
The option specifier defines the name of the option and, optionally,
the value it can take. The option linkage is usually a reference to a
variable that will be set when the option is used. For example, the
following call to GetOptions:
&GetOptions("size=i" => \$offset);
will accept a command line option "size" that must have an integer
value. With a command line of "--size 24" this will cause the variable
$offset to get the value 24.
Alternatively, the first argument to GetOptions may be a reference to
a HASH describing the linkage for the options. The following call is
equivalent to the example above:
%optctl = ("size" => \$offset);
&GetOptions(\%optctl, "size=i");
Linkage may be specified using either of the above methods, or both.
Linkage specified in the argument list takes precedence over the
linkage specified in the HASH.
The command line options are taken from array @ARGV. Upon completion
of GetOptions, @ARGV will contain the rest (i.e. the non-options) of
the command line.
Each option specifier designates the name of the option, optionally
followed by an argument specifier. Values for argument specifiers are:
=over 8
=item <none>
Option does not take an argument.
The option variable will be set to 1.
=item !
Option does not take an argument and may be negated, i.e. prefixed by
"no". E.g. "foo!" will allow B<--foo> (with value 1) and B<-nofoo>
(with value 0).
The option variable will be set to 1, or 0 if negated.
=item =s
Option takes a mandatory string argument.
This string will be assigned to the option variable.
Note that even if the string argument starts with B<-> or B<-->, it
will not be considered an option on itself.
=item :s
Option takes an optional string argument.
This string will be assigned to the option variable.
If omitted, it will be assigned "" (an empty string).
If the string argument starts with B<-> or B<-->, it
will be considered an option on itself.
=item =i
Option takes a mandatory integer argument.
This value will be assigned to the option variable.
Note that the value may start with B<-> to indicate a negative
value.
=item :i
Option takes an optional integer argument.
This value will be assigned to the option variable.
If omitted, the value 0 will be assigned.
Note that the value may start with B<-> to indicate a negative
value.
=item =f
Option takes a mandatory real number argument.
This value will be assigned to the option variable.
Note that the value may start with B<-> to indicate a negative
value.
=item :f
Option takes an optional real number argument.
This value will be assigned to the option variable.
If omitted, the value 0 will be assigned.
=back
A lone dash B<-> is considered an option, the corresponding option
name is the empty string.
A double dash on itself B<--> signals end of the options list.
=head2 Linkage specification
The linkage specifier is optional. If no linkage is explicitly
specified but a ref HASH is passed, GetOptions will place the value in
the HASH. For example:
%optctl = ();
&GetOptions (\%optctl, "size=i");
will perform the equivalent of the assignment
$optctl{"size"} = 24;
For array options, a reference to an array is used, e.g.:
%optctl = ();
&GetOptions (\%optctl, "sizes=i@");
with command line "-sizes 24 -sizes 48" will perform the equivalent of
the assignment
$optctl{"sizes"} = [24, 48];
If no linkage is explicitly specified and no ref HASH is passed,
GetOptions will put the value in a global variable named after the
option, prefixed by "opt_". To yield a usable Perl variable,
characters that are not part of the syntax for variables are
translated to underscores. For example, "--fpp-struct-return" will set
the variable $opt_fpp_struct_return. Note that this variable resides
in the namespace of the calling program, not necessarily B<main>.
For example:
&GetOptions ("size=i", "sizes=i@");
with command line "-size 10 -sizes 24 -sizes 48" will perform the
equivalent of the assignments
$opt_size = 10;
@opt_sizes = (24, 48);
A lone dash B<-> is considered an option, the corresponding Perl
identifier is $opt_ .
The linkage specifier can be a reference to a scalar, a reference to
an array or a reference to a subroutine.
If a REF SCALAR is supplied, the new value is stored in the referenced
variable. If the option occurs more than once, the previous value is
overwritten.
If a REF ARRAY is supplied, the new value is appended (pushed) to the
referenced array.
If a REF CODE is supplied, the referenced subroutine is called with
two arguments: the option name and the option value.
The option name is always the true name, not an abbreviation or alias.
=head2 Aliases and abbreviations
The option name may actually be a list of option names, separated by
"|"s, e.g. "foo|bar|blech=s". In this example, "foo" is the true name
op this option. If no linkage is specified, options "foo", "bar" and
"blech" all will set $opt_foo.
Option names may be abbreviated to uniqueness, depending on
configuration variable $Getopt::Long::autoabbrev.
=head2 Non-option call-back routine
A special option specifier, <>, can be used to designate a subroutine
to handle non-option arguments. GetOptions will immediately call this
subroutine for every non-option it encounters in the options list.
This subroutine gets the name of the non-option passed.
This feature requires $Getopt::Long::order to have the value $PERMUTE.
See also the examples.
=head2 Option starters
On the command line, options can start with B<-> (traditional), B<-->
(POSIX) and B<+> (GNU, now being phased out). The latter is not
allowed if the environment variable B<POSIXLY_CORRECT> has been
defined.
Options that start with "--" may have an argument appended, separated
with an "=", e.g. "--foo=bar".
=head2 Return value
A return status of 0 (false) indicates that the function detected
one or more errors.
=head1 COMPATIBILITY
Getopt::Long::GetOptions() is the successor of
B<newgetopt.pl> that came with Perl 4. It is fully upward compatible.
In fact, the Perl 5 version of newgetopt.pl is just a wrapper around
the module.
If an "@" sign is appended to the argument specifier, the option is
treated as an array. Value(s) are not set, but pushed into array
@opt_name. This only applies if no linkage is supplied.
If configuration variable $Getopt::Long::getopt_compat is set to a
non-zero value, options that start with "+" may also include their
arguments, e.g. "+foo=bar". This is for compatiblity with older
implementations of the GNU "get