home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_63fc70d5fb0496ec024d6fc28098ac2b
< prev
next >
Wrap
Text File
|
2000-03-23
|
18KB
|
372 lines
<HTML>
<HEAD>
<TITLE>Getopt::Mixed - getopt processing with both long and short options</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Getopt::Mixed - getopt processing with both long and short options</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<UL>
<LI><A HREF="#option descriptions">Option Descriptions</A></LI>
<LI><A HREF="#user interface">User Interface</A></LI>
<LI><A HREF="#the simple method">The Simple Method</A></LI>
<LI><A HREF="#the flexible method">The Flexible Method</A></LI>
</UL>
<LI><A HREF="#other functions">OTHER FUNCTIONS</A></LI>
<LI><A HREF="#customization">CUSTOMIZATION</A></LI>
<LI><A HREF="#bugs">BUGS</A></LI>
<LI><A HREF="#license">LICENSE</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Getopt::Mixed - getopt processing with both long and short options</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use Getopt::Mixed;
Getopt::Mixed::getOptions(...option-descriptions...);
...examine $opt_* variables...</PRE>
<P>or</P>
<PRE>
use Getopt::Mixed "nextOption";
Getopt::Mixed::init(...option-descriptions...);
while (($option, $value) = nextOption()) {
...process option...
}
Getopt::Mixed::cleanup();</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This package is my response to the standard modules Getopt::Std and
Getopt::Long. <CODE>Std</CODE> doesn't support long options, and <CODE>Long</CODE>
doesn't support short options. I wanted both, since long options are
easier to remember and short options are faster to type.</P>
<P>This package is intended to be the ``Getopt-to-end-all-Getop's''. It
combines (I hope) flexibility and simplicity. It supports both short
options (introduced by <CODE>-</CODE>) and long options (introduced by <CODE>--</CODE>).
Short options which do not take an argument can be grouped together.
Short options which do take an argument must be the last option in
their group, because everything following the option will be
considered to be its argument.</P>
<P>There are two methods for using Getopt::Mixed: the simple method and
the flexible method. Both methods use the same format for option
descriptions.</P>
<P>
<H2><A NAME="option descriptions">Option Descriptions</A></H2>
<P>The option-description arguments required by <CODE>init</CODE> and <CODE>getOptions</CODE>
are strings composed of individual option descriptions. Several
option descriptions can appear in the same string if they are
separated by whitespace.</P>
<P>Each description consists of the option name and an optional trailing
argument specifier. Option names may consist of any characters but
whitespace, <CODE>=</CODE>, <CODE>:</CODE>, and <CODE>></CODE>.</P>
<P>Values for argument specifiers are:</P>
<PRE>
<none> option does not take an argument
=s :s option takes a mandatory (=) or optional (:) string argument
=i :i option takes a mandatory (=) or optional (:) integer argument
=f :f option takes a mandatory (=) or optional (:) real number argument
>new option is a synonym for option `new'</PRE>
<P>The <CODE>></CODE> specifier is not really an argument specifier. It
defines an option as being a synonym for another option. For example,
``a=i apples>a'' would define <STRONG>-a</STRONG> as an option that requires an
integer argument and <STRONG>--apples</STRONG> as a synonym for <STRONG>-a</STRONG>. Only one
level of synonyms is supported, and the root option must be listed
first. For example, ``apples>a a=i'' and ``a=i apples>a oranges>apples''
are illegal; use ``a=i apples>a oranges>a'' if that's what you want.</P>
<P>For example, in the option description:
``a b=i c:s apple baker>b charlie:s''
-a and --apple do not take arguments
-b takes a mandatory integer argument
--baker is a synonym for -b
-c and --charlie take an optional string argument</P>
<P>If the first argument to <CODE>init</CODE> or <CODE>getOptions</CODE> is entirely
non-alphanumeric characters with no whitespace, it represents the
characters which can begin options.</P>
<P>
<H2><A NAME="user interface">User Interface</A></H2>
<P>From the user's perspective, short options are introduced by a dash
(<CODE>-</CODE>) and long options are introduced by a double dash (<CODE>--</CODE>).
Short options may be combined (``-a -b'' can be written ``-ab''), but an
option that takes an argument must be the last one in its group,
because anything following it is considered part of the argument. A
double dash by itself marks the end of the options; all arguments
following it are treated as normal arguments, not options. A single
dash by itself is treated as a normal argument, <EM>not</EM> an option.</P>
<P>Long options may be abbreviated. An option <STRONG>--all-the-time</STRONG> could be
abbreviated <STRONG>--all</STRONG>, <STRONG>--a--tim</STRONG>, or even <STRONG>--a</STRONG>. Note that <STRONG>--time</STRONG>
would not work; the abbreviation must start at the beginning of the
option name. If an abbreviation is ambiguous, an error message will
be printed.</P>
<P>In the following examples, <STRONG>-i</STRONG> and <STRONG>--int</STRONG> take integer arguments,
<STRONG>-f</STRONG> and <STRONG>--float</STRONG> take floating point arguments, and <STRONG>-s</STRONG> and
<STRONG>--string</STRONG> take string arguments. All other options do not take an
argument.</P>
<PRE>
-i24 -f24.5 -sHello
-i=24 --int=-27 -f=24.5 --float=0.27 -s=Hello --string=Hello</PRE>
<P>If the argument is required, it can also be separated by whitespace:</P>
<PRE>
-i 24 --int -27 -f 24.5 --float 0.27 -s Hello --string Hello</PRE>
<P>Note that if the option is followed by <CODE>=</CODE>, whatever follows the <CODE>=</CODE>
<EM>is</EM> the argument, even if it's the null string. In the example</P>
<PRE>
-i= 24 -f= 24.5 -s= Hello</PRE>
<P><STRONG>-i</STRONG> and <STRONG>-f</STRONG> will cause an error, because the null string is not a
number, but <STRONG>-s</STRONG> is perfectly legal; its argument is the null string,
not ``Hello''.</P>
<P>Remember that optional arguments <EM>cannot</EM> be separated from the
option by whitespace.</P>
<P>
<H2><A NAME="the simple method">The Simple Method</A></H2>
<P>The simple method is</P>
<PRE>
use Getopt::Mixed;
Getopt::Mixed::getOptions(...option-descriptions...);</PRE>
<P>You then examine the <CODE>$opt_*</CODE> variables to find out what options were
specified and the <CODE>@ARGV</CODE> array to see what arguments are left.</P>
<P>If <STRONG>-a</STRONG> is an option that doesn't take an argument, then <CODE>$opt_a</CODE>
will be set to 1 if the option is present, or left undefined if the
option is not present.</P>
<P>If <STRONG>-b</STRONG> is an option that takes an argument, then <CODE>$opt_b</CODE> will be
set to the value of the argument if the option is present, or left
undefined if the option is not present. If the argument is optional
but not supplied, <CODE>$opt_b</CODE> will be set to the null string.</P>
<P>Note that even if you specify that an option <EM>requires</EM> a string
argument, you can still get the null string (if the user specifically
enters it). If the option requires a numeric argument, you will never
get the null string (because it isn't a number).</P>
<P>When converting the option name to a Perl identifier, any non-word
characters in the name will be converted to underscores (<CODE>_</CODE>).</P>
<P>If the same option occurs more than once, only the last occurrence
will be recorded. If that's not acceptable, you'll have to use the
flexible method instead.</P>
<P>
<H2><A NAME="the flexible method">The Flexible Method</A></H2>
<P>The flexible method is</P>
<PRE>
use Getopt::Mixed "nextOption";
Getopt::Mixed::init(...option-descriptions...);
while (($option, $value, $pretty) = nextOption()) {
...process option...
}
Getopt::Mixed::cleanup();</PRE>
<P>This lets you process arguments one at a time. You can then handle
repeated options any way you want to. It also lets you see option
names with non-alphanumeric characters without any translation. This
is also the only method that lets you find out what order the options
and other arguments were in.</P>
<P>First, you call Getopt::Mixed::init with the option descriptions.
Then, you keep calling nextOption until it returns an empty list.
Finally, you call Getopt::Mixed::cleanup when you're done. The
remaining (non-option) arguments will be found in @ARGV.</P>
<P>Each call to nextOption returns a list of the next option, its value,
and the option as the user typed it. The value will be undefined if
the option does not take an argument. The option is stripped of its
starter (e.g., you get ``a'' and ``foo'', not ``-a'' or ``--foo''). If you
want to print an error message, use the third element, which does
include the option starter.</P>
<P>
<HR>
<H1><A NAME="other functions">OTHER FUNCTIONS</A></H1>
<P>Getopt::Mixed provides one other function you can use. <CODE>abortMsg</CODE>
prints its arguments on STDERR, plus your program's name and a
newline. It then exits with status 1. For example, if <EM>foo.pl</EM>
calls <CODE>abortMsg</CODE> like this:</P>
<PRE>
Getopt::Mixed::abortMsg("Error");</PRE>
<P>The output will be:</P>
<PRE>
foo.pl: Error</PRE>
<P>
<HR>
<H1><A NAME="customization">CUSTOMIZATION</A></H1>
<P>There are several customization variables you can set. All of these
variables should be set <EM>after</EM> calling Getopt::Mixed::init and
<EM>before</EM> calling nextOption.</P>
<P>If you set any of these variables, you <EM>must</EM> check the version
number first. The easiest way to do this is like this:</P>
<PRE>
use Getopt::Mixed 1.006;</PRE>
<P>If you are using the simple method, and you want to set these
variables, you'll need to call init before calling getOptions, like
this:</P>
<PRE>
use Getopt::Mixed 1.006;
Getopt::Mixed::init(...option-descriptions...);
...set configuration variables...
Getopt::Mixed::getOptions(); # IMPORTANT: no parameters</PRE>
<DL>
<DT><STRONG><A NAME="item_%24order">$order</A></STRONG><BR>
<DD>
$order can be set to $REQUIRE_ORDER, $PERMUTE, or $RETURN_IN_ORDER.
The default is $REQUIRE_ORDER if the environment variable
POSIXLY_CORRECT has been set, $PERMUTE otherwise.
<P>$REQUIRE_ORDER means that no options can follow the first argument
which isn't an option.</P>
<P>$PERMUTE means that all options are treated as if they preceded all
other arguments.</P>
<P>$RETURN_IN_ORDER means that all arguments maintain their ordering.
When nextOption is called, and the next argument is not an option, it
returns the null string as the option and the argument as the value.
nextOption never returns the null list until all the arguments have
been processed.</P>
<P></P>
<DT><STRONG><A NAME="item_%24ignoreCase">$ignoreCase</A></STRONG><BR>
<DD>
Ignore case when matching options. Default is 1 unless the option
descriptions contain an upper-case letter.
<P></P>
<DT><STRONG><A NAME="item_%24optionStart">$optionStart</A></STRONG><BR>
<DD>
A string of characters that can start options. Default is ``-''.
<P></P>
<DT><STRONG><A NAME="item_%24badOption">$badOption</A></STRONG><BR>
<DD>
A reference to a function that is called when an unrecognized option
is encountered. The function receives three arguments. $_[0] is the
position in @ARGV where the option came from. $_[1] is the option as
the user typed it (including the option start character). $_[2] is
either undef or a string describing the reason the option was not
recognized (Currently, the only possible value is 'ambiguous', for a
long option with several possible matches). The option has already
been removed from @ARGV. To put it back, you can say:
<PRE>
splice(@ARGV,$_[0],0,$_[1]);</PRE>
<P>The function can do anything you want to @ARGV. It should return
whatever you want nextOption to return.</P>
<P>The default is a function that prints an error message and exits the
program.</P>
<P></P>
<DT><STRONG><A NAME="item_%24checkArg">$checkArg</A></STRONG><BR>
<DD>
A reference to a function that is called to make sure the argument
type is correct. The function receives four arguments. $_[0] is the
position in @ARGV where the option came from. $_[1] is the text
following the option, or undefined if there was no text following the
option. $_[2] is the name of the option as the user typed it
(including the option start character), suitable for error messages.
$_[3] is the argument type specifier.
<P>The function can do anything you want to @ARGV. It should return
the value for this option.</P>
<P>The default is a function that prints an error message and exits the
program if the argument is not the right type for the option. You can
also adjust the behavior of the default function by changing
$intRegexp or $floatRegexp.</P>
<P></P>
<DT><STRONG><A NAME="item_%24intRegexp">$intRegexp</A></STRONG><BR>
<DD>
A regular expression that matches an integer. Default is
'^[-+]?\d+$', which matches a string of digits preceded by an
optional sign. Unlike the other configuration variables, this cannot
be changed after nextOption is called, because the pattern is compiled
only once.
<P></P>
<DT><STRONG><A NAME="item_%24floatRegexp">$floatRegexp</A></STRONG><BR>
<DD>
A regular expression that matches a floating point number. Default is
'^[-+]?(\d*\.?\d+|\d+\.)$', which matches the following formats:
``123'', ``123.'', ``123.45'', and ``.123'' (plus an optional sign). It does
not match exponential notation. Unlike the other configuration
variables, this cannot be changed after nextOption is called, because
the pattern is compiled only once.
<P></P>
<DT><STRONG><A NAME="item_%24typeChars">$typeChars</A></STRONG><BR>
<DD>
A string of the characters which are legal argument types. The
default is 'sif', for String, Integer, and Floating point arguments.
The string should consist only of letters. Upper case letters are
discouraged, since this will hamper the case-folding of options. If
you change this, you should set $checkType to a function that will
check arguments of your new type. Unlike the other configuration
variables, this must be set <EM>before</EM> calling init(), and cannot be
changed afterwards.
<P></P>
<DT><STRONG><A NAME="item_%24checkType">$checkType</A></STRONG><BR>
<DD>
If you add new types to $typeChars, you should set this to a function
which will check arguments of the new types.
<P></P></DL>
<P>
<HR>
<H1><A NAME="bugs">BUGS</A></H1>
<UL>
<LI>
This document should be expanded.
<P></P>
<LI>
A long option must be at least two characters long. Sorry.
<P></P>
<LI>
The <CODE>!</CODE> argument specifier of Getopt::Long is not supported, but you
could have options <STRONG>--foo</STRONG> and <STRONG>--nofoo</STRONG> and then do something like:
<PRE>
$opt_foo = 0 if $opt_nofoo;</PRE>
<P></P>
<LI>
The <CODE>@</CODE> argument specifier of Getopt::Long is not supported. If you
want your values pushed into an array, you'll have to use nextOption
and do it yourself.
<P></P></UL>
<P>
<HR>
<H1><A NAME="license">LICENSE</A></H1>
<P>Getopt::Mixed is distributed under the terms of the GNU General Public
License as published by the Free Software Foundation; either version
2, or (at your option) any later version.</P>
<P>This means it is distributed in the hope that it will be useful, but
<EM>without any warranty</EM>; without even the implied warranty of
<EM>merchantability</EM> or <EM>fitness for a particular purpose</EM>. See the
GNU General Public License for more details.</P>
<P>Since Perl scripts are only compiled at runtime, and simply calling
Getopt::Mixed does <EM>not</EM> bring your program under the GPL, the only
real restriction is that you can't use Getopt::Mixed in an
binary-only distribution produced with <A HREF="../../../lib/Pod/perlfunc.html#item_dump"><CODE>dump</CODE></A> (unless you also
provide source code).</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Christopher J. Madsen <<EM><A HREF="mailto:ac608@yfn.ysu.edu">ac608@yfn.ysu.edu</A></EM>></P>
<P>Thanks are also due to Andreas Koenig for helping Getopt::Mixed
conform to the standards for Perl modules and for answering a bunch of
questions. Any remaining deficiencies are my fault.</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Getopt::Mixed - getopt processing with both long and short options</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>