ActivePerl Documentation
|
NAMEGetopt::Mixed - getopt processing with both long and short options
SUPPORTED PLATFORMS
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();
DESCRIPTIONThis package is my response to the standard modules Getopt::Std and
Getopt::Long. 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 There are two methods for using Getopt::Mixed: the simple method and the flexible method. Both methods use the same format for option descriptions.
Option DescriptionsThe option-description arguments required by Each description consists of the option name and an optional trailing
argument specifier. Option names may consist of any characters but
whitespace, 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 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
User InterfaceFrom the user's perspective, short options are introduced by a dash
( Long options may be abbreviated. An option --all-the-time could be abbreviated --all, --a--tim, or even --a. Note that --time 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. 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 an argument. -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 -i= 24 -f= 24.5 -s= Hello -i and -f will cause an error, because the null string is not a number, but -s is perfectly legal; its argument is the null string, not ``Hello''. Remember that optional arguments cannot be separated from the option by whitespace.
The Simple MethodThe simple method is
use Getopt::Mixed;
Getopt::Mixed::getOptions(...option-descriptions...);
You then examine the If -a is an option that doesn't take an argument, then If -b is an option that takes an argument, then Note that even if you specify that an option requires 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). When converting the option name to a Perl identifier, any non-word
characters in the name will be converted to underscores ( 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.
The Flexible MethodThe 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 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. 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. 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.
OTHER FUNCTIONSGetopt::Mixed provides one other function you can use.
Getopt::Mixed::abortMsg("Error");
The output will be: foo.pl: Error
CUSTOMIZATIONThere are several customization variables you can set. All of these variables should be set after calling Getopt::Mixed::init and before calling nextOption. If you set any of these variables, you must check the version number 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 these variables, you'll need to call init before calling getOptions, like this:
use Getopt::Mixed 1.006;
Getopt::Mixed::init(...option-descriptions...);
...set configuration variables...
Getopt::Mixed::getOptions(); # IMPORTANT: no parameters
BUGS
LICENSEGetopt::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. This means it is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details. Since Perl scripts are only compiled at runtime, and simply calling
Getopt::Mixed does not 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
AUTHORChristopher J. Madsen <ac608@yfn.ysu.edu> 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.
|