.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Getopt::Mixed 3pm" .TH Getopt::Mixed 3pm "2000-06-12" "perl v5.22.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Getopt::Mixed \- getopt processing with both long and short options .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use Getopt::Mixed; \& Getopt::Mixed::getOptions(...option\-descriptions...); \& ...examine $opt_* variables... .Ve .PP or .PP .Vb 6 \& use Getopt::Mixed "nextOption"; \& Getopt::Mixed::init(...option\-descriptions...); \& while (($option, $value) = nextOption()) { \& ...process option... \& } \& Getopt::Mixed::cleanup(); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This package is my response to the standard modules Getopt::Std and Getopt::Long. \f(CW\*(C`Std\*(C'\fR doesn't support long options, and \f(CW\*(C`Long\*(C'\fR doesn't support short options. I wanted both, since long options are easier to remember and short options are faster to type. .PP This package is intended to be the \*(L"Getopt-to-end-all-Getop's\*(R". It combines (I hope) flexibility and simplicity. It supports both short options (introduced by \f(CW\*(C`\-\*(C'\fR) and long options (introduced by \f(CW\*(C`\-\-\*(C'\fR). 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. .PP There are two methods for using Getopt::Mixed: the simple method and the flexible method. Both methods use the same format for option descriptions. .SS "Option Descriptions" .IX Subsection "Option Descriptions" The option-description arguments required by \f(CW\*(C`init\*(C'\fR and \f(CW\*(C`getOptions\*(C'\fR are strings composed of individual option descriptions. Several option descriptions can appear in the same string if they are separated by whitespace. .PP Each description consists of the option name and an optional trailing argument specifier. Option names may consist of any characters but whitespace, \f(CW\*(C`=\*(C'\fR, \f(CW\*(C`:\*(C'\fR, and \f(CW\*(C`>\*(C'\fR. .PP Values for argument specifiers are: .PP .Vb 5 \& 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\*(Aq .Ve .PP The \f(CW\*(C`>\*(C'\fR specifier is not really an argument specifier. It defines an option as being a synonym for another option. For example, \&\*(L"a=i apples>a\*(R" would define \fB\-a\fR as an option that requires an integer argument and \fB\-\-apples\fR as a synonym for \fB\-a\fR. Only one level of synonyms is supported, and the root option must be listed first. For example, \*(L"apples>a a=i\*(R" and \*(L"a=i apples>a oranges>apples\*(R" are illegal; use \*(L"a=i apples>a oranges>a\*(R" if that's what you want. .PP For example, in the option description: \*(L"a b=i c:s apple baker>b charlie:s\*(R" \-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 .PP If the first argument to \f(CW\*(C`init\*(C'\fR or \f(CW\*(C`getOptions\*(C'\fR is entirely non-alphanumeric characters with no whitespace, it represents the characters which can begin options. .SS "User Interface" .IX Subsection "User Interface" From the user's perspective, short options are introduced by a dash (\f(CW\*(C`\-\*(C'\fR) and long options are introduced by a double dash (\f(CW\*(C`\-\-\*(C'\fR). Short options may be combined (\*(L"\-a \-b\*(R" can be written \*(L"\-ab\*(R"), 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, \fInot\fR an option. .PP Long options may be abbreviated. An option \fB\-\-all\-the\-time\fR could be abbreviated \fB\-\-all\fR, \fB\-\-a\*(--tim\fR, or even \fB\-\-a\fR. Note that \fB\-\-time\fR 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. .PP In the following examples, \fB\-i\fR and \fB\-\-int\fR take integer arguments, \&\fB\-f\fR and \fB\-\-float\fR take floating point arguments, and \fB\-s\fR and \&\fB\-\-string\fR take string arguments. All other options do not take an argument. .PP .Vb 2 \& \-i24 \-f24.5 \-sHello \& \-i=24 \-\-int=\-27 \-f=24.5 \-\-float=0.27 \-s=Hello \-\-string=Hello .Ve .PP If the argument is required, it can also be separated by whitespace: .PP .Vb 1 \& \-i 24 \-\-int \-27 \-f 24.5 \-\-float 0.27 \-s Hello \-\-string Hello .Ve .PP Note that if the option is followed by \f(CW\*(C`=\*(C'\fR, whatever follows the \f(CW\*(C`=\*(C'\fR \&\fIis\fR the argument, even if it's the null string. In the example .PP .Vb 1 \& \-i= 24 \-f= 24.5 \-s= Hello .Ve .PP \&\fB\-i\fR and \fB\-f\fR will cause an error, because the null string is not a number, but \fB\-s\fR is perfectly legal; its argument is the null string, not \*(L"Hello\*(R". .PP Remember that optional arguments \fIcannot\fR be separated from the option by whitespace. .SS "The Simple Method" .IX Subsection "The Simple Method" The simple method is .PP .Vb 2 \& use Getopt::Mixed; \& Getopt::Mixed::getOptions(...option\-descriptions...); .Ve .PP You then examine the \f(CW\*(C`$opt_*\*(C'\fR variables to find out what options were specified and the \f(CW@ARGV\fR array to see what arguments are left. .PP If \fB\-a\fR is an option that doesn't take an argument, then \f(CW$opt_a\fR will be set to 1 if the option is present, or left undefined if the option is not present. .PP If \fB\-b\fR is an option that takes an argument, then \f(CW$opt_b\fR 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, \f(CW$opt_b\fR will be set to the null string. .PP Note that even if you specify that an option \fIrequires\fR 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). .PP When converting the option name to a Perl identifier, any non-word characters in the name will be converted to underscores (\f(CW\*(C`_\*(C'\fR). .PP 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. .SS "The Flexible Method" .IX Subsection "The Flexible Method" The flexible method is .PP .Vb 6 \& use Getopt::Mixed "nextOption"; \& Getopt::Mixed::init(...option\-descriptions...); \& while (($option, $value, $pretty) = nextOption()) { \& ...process option... \& } \& Getopt::Mixed::cleanup(); .Ve .PP 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. .PP 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 \f(CW@ARGV\fR. .PP 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 \*(L"a\*(R" and \*(L"foo\*(R", not \*(L"\-a\*(R" or \*(L"\-\-foo\*(R"). If you want to print an error message, use the third element, which does include the option starter. .SH "OTHER FUNCTIONS" .IX Header "OTHER FUNCTIONS" Getopt::Mixed provides one other function you can use. \f(CW\*(C`abortMsg\*(C'\fR prints its arguments on \s-1STDERR,\s0 plus your program's name and a newline. It then exits with status 1. For example, if \fIfoo.pl\fR calls \f(CW\*(C`abortMsg\*(C'\fR like this: .PP .Vb 1 \& Getopt::Mixed::abortMsg("Error"); .Ve .PP The output will be: .PP .Vb 1 \& foo.pl: Error .Ve .SH "CUSTOMIZATION" .IX Header "CUSTOMIZATION" There are several customization variables you can set. All of these variables should be set \fIafter\fR calling Getopt::Mixed::init and \&\fIbefore\fR calling nextOption. .PP If you set any of these variables, you \fImust\fR check the version number first. The easiest way to do this is like this: .PP .Vb 1 \& use Getopt::Mixed 1.006; .Ve .PP 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: .PP .Vb 4 \& use Getopt::Mixed 1.006; \& Getopt::Mixed::init(...option\-descriptions...); \& ...set configuration variables... \& Getopt::Mixed::getOptions(); # IMPORTANT: no parameters .Ve .ie n .IP "$order" 4 .el .IP "\f(CW$order\fR" 4 .IX Item "$order" \&\f(CW$order\fR can be set to \f(CW$REQUIRE_ORDER\fR, \f(CW$PERMUTE\fR, or \f(CW$RETURN_IN_ORDER\fR. The default is \f(CW$REQUIRE_ORDER\fR if the environment variable \&\s-1POSIXLY_CORRECT\s0 has been set, \f(CW$PERMUTE\fR otherwise. .Sp \&\f(CW$REQUIRE_ORDER\fR means that no options can follow the first argument which isn't an option. .Sp \&\f(CW$PERMUTE\fR means that all options are treated as if they preceded all other arguments. .Sp \&\f(CW$RETURN_IN_ORDER\fR 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. .ie n .IP "$ignoreCase" 4 .el .IP "\f(CW$ignoreCase\fR" 4 .IX Item "$ignoreCase" Ignore case when matching options. Default is 1 unless the option descriptions contain an upper-case letter. .ie n .IP "$optionStart" 4 .el .IP "\f(CW$optionStart\fR" 4 .IX Item "$optionStart" A string of characters that can start options. Default is \*(L"\-\*(R". .ie n .IP "$badOption" 4 .el .IP "\f(CW$badOption\fR" 4 .IX Item "$badOption" A reference to a function that is called when an unrecognized option is encountered. The function receives three arguments. \f(CW$_\fR[0] is the position in \f(CW@ARGV\fR where the option came from. \f(CW$_\fR[1] is the option as the user typed it (including the option start character). \f(CW$_\fR[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 \f(CW@ARGV\fR. To put it back, you can say: .Sp .Vb 1 \& splice(@ARGV,$_[0],0,$_[1]); .Ve .Sp The function can do anything you want to \f(CW@ARGV\fR. It should return whatever you want nextOption to return. .Sp The default is a function that prints an error message and exits the program. .ie n .IP "$checkArg" 4 .el .IP "\f(CW$checkArg\fR" 4 .IX Item "$checkArg" A reference to a function that is called to make sure the argument type is correct. The function receives four arguments. \f(CW$_\fR[0] is the position in \f(CW@ARGV\fR where the option came from. \f(CW$_\fR[1] is the text following the option, or undefined if there was no text following the option. \f(CW$_\fR[2] is the name of the option as the user typed it (including the option start character), suitable for error messages. \&\f(CW$_\fR[3] is the argument type specifier. .Sp The function can do anything you want to \f(CW@ARGV\fR. It should return the value for this option. .Sp 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 \&\f(CW$intRegexp\fR or \f(CW$floatRegexp\fR. .ie n .IP "$intRegexp" 4 .el .IP "\f(CW$intRegexp\fR" 4 .IX Item "$intRegexp" A regular expression that matches an integer. Default is \&'^[\-+]?\ed+$', 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. .ie n .IP "$floatRegexp" 4 .el .IP "\f(CW$floatRegexp\fR" 4 .IX Item "$floatRegexp" A regular expression that matches a floating point number. Default is \&'^[\-+]?(\ed*\e.?\ed+|\ed+\e.)$', which matches the following formats: \&\*(L"123\*(R", \*(L"123.\*(R", \*(L"123.45\*(R", and \*(L".123\*(R" (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. .ie n .IP "$typeChars" 4 .el .IP "\f(CW$typeChars\fR" 4 .IX Item "$typeChars" 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 \f(CW$checkType\fR to a function that will check arguments of your new type. Unlike the other configuration variables, this must be set \fIbefore\fR calling \fIinit()\fR, and cannot be changed afterwards. .ie n .IP "$checkType" 4 .el .IP "\f(CW$checkType\fR" 4 .IX Item "$checkType" If you add new types to \f(CW$typeChars\fR, you should set this to a function which will check arguments of the new types. .SH "BUGS" .IX Header "BUGS" .IP "\(bu" 4 This document should be expanded. .IP "\(bu" 4 A long option must be at least two characters long. Sorry. .IP "\(bu" 4 The \f(CW\*(C`!\*(C'\fR argument specifier of Getopt::Long is not supported, but you could have options \fB\-\-foo\fR and \fB\-\-nofoo\fR and then do something like: .Sp .Vb 1 \& $opt_foo = 0 if $opt_nofoo; .Ve .IP "\(bu" 4 The \f(CW\*(C`@\*(C'\fR 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. .SH "LICENSE" .IX Header "LICENSE" Getopt::Mixed is distributed under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. .PP This means it is distributed in the hope that it will be useful, but \&\fIwithout any warranty\fR; without even the implied warranty of \&\fImerchantability\fR or \fIfitness for a particular purpose\fR. See the \&\s-1GNU\s0 General Public License for more details. .PP Since Perl scripts are only compiled at runtime, and simply calling Getopt::Mixed does \fInot\fR bring your program under the \s-1GPL,\s0 the only real restriction is that you can't use Getopt::Mixed in an binary-only distribution produced with \f(CW\*(C`dump\*(C'\fR (unless you also provide source code). .SH "AUTHOR" .IX Header "AUTHOR" Christopher J. Madsen <\fIac608@yfn.ysu.edu\fR> .PP 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.