MAN page from Old RedHat 6.X Getopt-Mixed-1.008-1.i386.rpm
lib::Getopt::Mixed
Section: User Contributed Perl Documentation (3)
Updated: perl 5.005, patch 03
Index NAME
Getopt::Mixed - getopt processing with both long and short options
SYNOPSIS
use Getopt::Mixed; Getopt::Mixed::getOptions(...option-descriptions...); ...examine $opt_* variables...
or
use Getopt::Mixed "nextOption"; Getopt::Mixed::init(...option-descriptions...); while (($option, $value) = nextOption()) { ...process option... } Getopt::Mixed::cleanup(); DESCRIPTION
This package is my response to the standard modules Getopt::Std andGetopt::Long.
Std doesn't support long options, and
Longdoesn't support short options. I wanted both, since long options areeasier to remember and short options are faster to type.
This package is intended to be the ``Getopt-to-end-all-Getop's''. Itcombines (I hope) flexibility and simplicity. It supports both shortoptions (introduced by -) and long options (introduced by --).Short options which do not take an argument can be grouped together.Short options which do take an argument must be the last option intheir group, because everything following the option will beconsidered to be its argument.
There are two methods for using Getopt::Mixed: the simple method andthe flexible method. Both methods use the same format for optiondescriptions.
Option Descriptions
The option-description arguments required by
init and
getOptionsare strings composed of individual option descriptions. Severaloption descriptions can appear in the same string if they areseparated by whitespace.
Each description consists of the option name and an optional trailingargument specifier. Option names may consist of any characters butwhitespace, =, :, and >.
Values for argument specifiers are:
<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'
The
> specifier is not really an argument specifier. Itdefines an option as being a synonym for another option. For example,``a=i apples>a'' would define
-a as an option that requires aninteger argument and
--apples as a synonym for
-a. Only onelevel of synonyms is supported, and the root option must be listedfirst. 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.
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
If the first argument to init or getOptions is entirelynon-alphanumeric characters with no whitespace, it represents thecharacters which can begin options.
User Interface
From the user's perspective, short options are introduced by a dash(
-) and long options are introduced by a double dash (
--).Short options may be combined (''-a -b'' can be written ``-ab"), but anoption that takes an argument must be the last one in its group,because anything following it is considered part of the argument. Adouble dash by itself marks the end of the options; all argumentsfollowing it are treated as normal arguments, not options. A singledash by itself is treated as a normal argument,
not an option.
Long options may be abbreviated. An option --all-the-time could beabbreviated --all, --a---tim, or even --a. Note that --timewould not work; the abbreviation must start at the beginning of theoption name. If an abbreviation is ambiguous, an error message willbe printed.
In the following examples, -i and --int take integer arguments,-f and --float take floating point arguments, and -s and--string take string arguments. All other options do not take anargument.
-i24 -f24.5 -sHello -i=24 --int=-27 -f=24.5 --float=0.27 -s=Hello --string=Hello
If the argument is required, it can also be separated by whitespace:
-i 24 --int -27 -f 24.5 --float 0.27 -s Hello --string Hello
Note that if the option is followed by
=, whatever follows the
=is the argument, even if it's the null string. In the example
-i= 24 -f= 24.5 -s= Hello
-i and
-f will cause an error, because the null string is not anumber, but
-s is perfectly legal; its argument is the null string,not ``Hello''.
Remember that optional arguments cannot be separated from theoption by whitespace.
The Simple Method
The simple method is
use Getopt::Mixed; Getopt::Mixed::getOptions(...option-descriptions...);
You then examine the
$opt_* variables to find out what options werespecified and the
@ARGV array to see what arguments are left.
If -a is an option that doesn't take an argument, then $opt_awill be set to 1 if the option is present, or left undefined if theoption is not present.
If -b is an option that takes an argument, then $opt_b will beset to the value of the argument if the option is present, or leftundefined if the option is not present. If the argument is optionalbut not supplied, $opt_b will be set to the null string.
Note that even if you specify that an option requires a stringargument, you can still get the null string (if the user specificallyenters it). If the option requires a numeric argument, you will neverget the null string (because it isn't a number).
When converting the option name to a Perl identifier, any non-wordcharacters in the name will be converted to underscores (_).
If the same option occurs more than once, only the last occurrencewill be recorded. If that's not acceptable, you'll have to use theflexible method instead.
The Flexible Method
The flexible method is
use Getopt::Mixed "nextOption"; Getopt::Mixed::init(...option-descriptions...); while (($option, $value, $pretty) = nextOption()) { ...process option... } Getopt::Mixed::cleanup();This lets you process arguments one at a time. You can then handlerepeated options any way you want to. It also lets you see optionnames with non-alphanumeric characters without any translation. Thisis also the only method that lets you find out what order the optionsand other arguments were in.
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. Theremaining (non-option) arguments will be found in @ARGV.
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 ifthe option does not take an argument. The option is stripped of itsstarter (e.g., you get ``a'' and ``foo'', not ``-a'' or ``---foo"). If youwant to print an error message, use the third element, which doesinclude the option starter.
OTHER FUNCTIONS
Getopt::Mixed provides one other function you can use.
abortMsgprints its arguments on STDERR, plus your program's name and anewline. It then exits with status 1. For example, if
foo.plcalls
abortMsg like this:
Getopt::Mixed::abortMsg("Error");The output will be:
foo.pl: Error
CUSTOMIZATION
There are several customization variables you can set. All of thesevariables should be set
after calling Getopt::Mixed::init and
before calling nextOption.
If you set any of these variables, you must check the versionnumber first. The easiest way to do this is like this:
use Getopt::Mixed 1.006;
If you are using the simple method, and you want to set thesevariables, you'll need to call init before calling getOptions, likethis:
use Getopt::Mixed 1.006; Getopt::Mixed::init(...option-descriptions...); ...set configuration variables... Getopt::Mixed::getOptions(); # IMPORTANT: no parameters
- $order
- $order can be set to $REQUIRE_ORDER, $PERMUTE, or $RETURN_IN_ORDER.The default is $REQUIRE_ORDER if the environment variablePOSIXLY_CORRECT has been set, $PERMUTE otherwise.
$REQUIRE_ORDER means that no options can follow the first argumentwhich isn't an option.
$PERMUTE means that all options are treated as if they preceded allother arguments.
$RETURN_IN_ORDER means that all arguments maintain their ordering.When nextOption is called, and the next argument is not an option, itreturns the null string as the option and the argument as the value.nextOption never returns the null list until all the arguments havebeen processed.
- $ignoreCase
- Ignore case when matching options. Default is 1 unless the optiondescriptions contain an upper-case letter.
- $optionStart
- A string of characters that can start options. Default is ``-''.
- $badOption
- A reference to a function that is called when an unrecognized optionis encountered. The function receives three arguments. $_[0] is theposition in @ARGV where the option came from. $_[1] is the option asthe user typed it (including the option start character). $_[2] iseither undef or a string describing the reason the option was notrecognized (Currently, the only possible value is `ambiguous', for along option with several possible matches). The option has alreadybeen removed from @ARGV. To put it back, you can say:
splice(@ARGV,$_[0],0,$_[1]);
The function can do anything you want to @ARGV. It should returnwhatever you want nextOption to return.The default is a function that prints an error message and exits theprogram.
- $checkArg
- A reference to a function that is called to make sure the argumenttype is correct. The function receives four arguments. $_[0] is theposition in @ARGV where the option came from. $_[1] is the textfollowing the option, or undefined if there was no text following theoption. $_[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.
The function can do anything you want to @ARGV. It should returnthe value for this option.
The default is a function that prints an error message and exits theprogram if the argument is not the right type for the option. You canalso adjust the behavior of the default function by changing$intRegexp or $floatRegexp.
- $intRegexp
- A regular expression that matches an integer. Default is'^[-+]?\d+$', which matches a string of digits preceded by anoptional sign. Unlike the other configuration variables, this cannotbe changed after nextOption is called, because the pattern is compiledonly once.
- $floatRegexp
- 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 doesnot match exponential notation. Unlike the other configurationvariables, this cannot be changed after nextOption is called, becausethe pattern is compiled only once.
- $typeChars
- A string of the characters which are legal argument types. Thedefault is `sif', for String, Integer, and Floating point arguments.The string should consist only of letters. Upper case letters arediscouraged, since this will hamper the case-folding of options. Ifyou change this, you should set $checkType to a function that willcheck arguments of your new type. Unlike the other configurationvariables, this must be set before calling init(), and cannot bechanged afterwards.
- $checkType
- If you add new types to $typeChars, you should set this to a functionwhich will check arguments of the new types.
BUGS
- *
- This document should be expanded.
- *
- A long option must be at least two characters long. Sorry.
- *
- The ! argument specifier of Getopt::Long is not supported, but youcould have options --foo and --nofoo and then do something like:
$opt_foo = 0 if $opt_nofoo;
- *
- The @ argument specifier of Getopt::Long is not supported. If youwant your values pushed into an array, you'll have to use nextOptionand do it yourself.
LICENSE
Getopt::Mixed is distributed under the terms of the GNU General PublicLicense as published by the Free Software Foundation; either version2, or (at your option) any later version.
This means it is distributed in the hope that it will be useful, butwithout any warranty; without even the implied warranty ofmerchantability or fitness for a particular purpose. See theGNU General Public License for more details.
Since Perl scripts are only compiled at runtime, and simply callingGetopt::Mixed does not bring your program under the GPL, the onlyreal restriction is that you can't use Getopt::Mixed in anbinary-only distribution produced with dump (unless you alsoprovide source code).
AUTHOR
Christopher J. Madsen <
ac608AATTyfn.ysu.edu>
Thanks are also due to Andreas Koenig for helping Getopt::Mixedconform to the standards for Perl modules and for answering a bunch ofquestions. Any remaining deficiencies are my fault.
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- Option Descriptions
- User Interface
- The Simple Method
- The Flexible Method
- OTHER FUNCTIONS
- CUSTOMIZATION
- BUGS
- LICENSE
- AUTHOR
This document was created byman2html,using the manual pages.