CIL::Mixed(3pm)
π NAME π
okay so bestie, welcome to CIL::Mixed(3pm) β this is the manual page for Getopt::Mixed, a module that does getopt processing with both long and short options. yes both. icon behavior honestly. (it's also OBSOLETE, but we'll get to that drama in a sec π«Ά)
β¨ VERSION β¨
this doc describes version 1.12 of Getopt::Mixed, released February 8, 2014. vintage. retro. a lil vintage moment. π
πΈ SYNOPSIS πΈ
first vibe:
use Getopt::Mixed;
Getopt::Mixed::getOptions(...option-descriptions...);
...examine $opt_* variables...
or, the other vibe:
use Getopt::Mixed "nextOption";
Getopt::Mixed::init(...option-descriptions...);
while (($option, $value) = nextOption()) {
...process option...
}
Getopt::Mixed::cleanup();
π¦ DESCRIPTION π¦
so like. this module is obsolete. that's the headline, no cap. π
the tea is this: this package was the author's response to the standard modules Getopt::Std and Getopt::Long. Std doesn't support long options, and Long didn't support short options. and they wanted both, because long options are easier to remember and short options are faster to type. makes sense, that's just good UX.
BUT. years ago Getopt::Long got changed to support short options too, and it has the huge advantage of being part of the standard Perl distribution. so Getopt::Mixed is now effectively obsolete. the author doesn't intend to make any more changes, but it's staying available for people who have code that already uses it. for new modules, they recommend using Getopt::Long like this:
use Getopt::Long 2.17; # Released with Perl 5.005
Getopt::Long::Configure(qw(bundling no_getopt_compat));
GetOptions(...option-descriptions...);
this package was intended to be the "Getopt-to-end-all-Getop's". π
it combines (hopefully) flexibility and simplicity. it supports both short options (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 in their group, because everything following the option will be considered to be its argument. periodt.
there are two methods for using Getopt::Mixed: the simple method and the flexible method. both methods use the same format for option descriptions. slay.
π Option Descriptions π
the option-description arguments required by init and getOptions are strings composed of individual option descriptions. several option descriptions can appear in the same string if they are separated by whitespace. easy.
each description consists of the option name and an optional trailing argument specifier. option names may consist of any characters but whitespace, =, :, 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 optionnew
the > specifier is not really an argument specifier, bestie. it defines an option as being a synonym for another option. for example, a=i apples>a would define -a as an option that requires an integer argument and --apples as a synonym for -a. 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. don't be messy about it. π
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 entirely non-alphanumeric characters with no whitespace, it represents the characters which can begin options. cute.
π 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 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, not an option.
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. rude but fair.
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 =, 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 a number, but -s is perfectly legal; its argument is the null string, not Hello.
remember bestie: optional arguments cannot be separated from the option by whitespace. that's the rule. that's the law. π
π 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 were specified and the @ARGV array to see what arguments are left.
if -a is an option that doesn't take an argument, then $opt_a will be set to 1 if the option is present, or left undefined if the option is not present.
if -b is an option that takes an argument, then $opt_b 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, $opt_b will be set to the null string.
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). math is math. π§
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. sorry bestie.
β 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 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. order info = unlocked. π₯
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 FUNCTIONS π
Getopt::Mixed provides one other function you can use. abortMsg prints its arguments on STDERR, plus your program's name and a newline. it then exits with status 1. for example, if foo.pl calls abortMsg like this:
Getopt::Mixed::abortMsg("Error");
the output will be:
foo.pl: Error
and that's it, it just bails. iconic exit behavior honestly. π
β¨ CUSTOMIZATION β¨
okay bestie, here's where it gets fun π
you can set a whole bunch of customization variables. the rules are: set ALL of these after calling Getopt::Mixed::init and before calling nextOption. that order is not a suggestion, it's the vibe. πΈ
and bestie, if you're setting any of these, you MUST check the version number first. the easiest way is literally just:
use Getopt::Mixed 1.006;
and if you're living that simple-method life and still wanna set these variables, you gotta call init before getOptions, like this:
use Getopt::Mixed 1.006;
Getopt::Mixed::init(...option-descriptions...);
...set configuration variables...
Getopt::Mixed::getOptions(); # IMPORTANT: no parameters
notice the # IMPORTANT: no parameters β that's not me being dramatic, that's the real deal. no parameters. periodt.
the variables π
$orderβ okay so$ordercan be set to$REQUIRE_ORDER,$PERMUTE, or$RETURN_IN_ORDER. the default? it's$REQUIRE_ORDERif the environment variablePOSIXLY_CORRECThas been set, and$PERMUTEotherwise. those are your two options, bestie.$REQUIRE_ORDERmeans no options can follow the first argument which isn't an option. like, once a non-option shows up, that's it, the option party is over. π«$PERMUTEmeans all options are treated as if they preceded all other arguments. so it's like, rearranging the vibe so the options go first. β¨$RETURN_IN_ORDERmeans all arguments maintain their ordering. so whennextOptionis called, and the next argument is not an option, it returns the null string as the option and the argument as the value. andnextOptionnever returns the null list until all the arguments have been processed. never ever. π
$ignoreCaseβ ignore case when matching options. the default is1unless the option descriptions contain an upper-case letter. uppercase said "no thank you" to case-folding, basically. π$optionStartβ a string of characters that can start options. default is"-". iconic honestly. β$badOptionβ a reference to a function that gets called when an unrecognized option is encountered. the function receives three arguments. let me break it down for you:$_[0]is the position in@ARGVwhere the option came from.$_[1]is the option as the user typed it (including the option start character).$_[2]is eitherundefor 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:
splice(@ARGV,$_[0],0,$_[1]);
the function can do anything you want to @ARGV. it should return whatever you want nextOption to return.
the default is a function that prints an error message and exits the program. so like, if you don't customize this, bad options = rage quit. π
$checkArgβ a reference to a function that's called to make sure the argument type is correct. this one receives four arguments:$_[0]is the position in@ARGVwhere 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.
the function can do anything you want to @ARGV. it should return the value for this option.
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. π
$intRegexpβ 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 one cannot be changed afternextOptionis called, because the pattern is compiled only once. so set it early or forever hold your peace. π$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 does not match exponential notation. and again, unlike the other configuration variables, this cannot be changed afternextOptionis called, because the pattern is compiled only once. π$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$checkTypeto a function that will check arguments of your new type. and unlike the other configuration variables, this must be set before callinginit(), and cannot be changed afterwards. this one's got the strictest curfew. π$checkTypeβ if you add new types to$typeChars, you should set this to a function which will check arguments of the new types. matching set, bestie. π«Ά
π SEE ALSO π
Getopt::Long
π CONFIGURATION AND ENVIRONMENT π
Getopt::Mixed requires no configuration files or environment variables. low maintenance queen. β¨
π INCOMPATIBILITIES π
None reported.
π BUGS AND LIMITATIONS π
- This document should be expanded. (relatable. so should I. π)
- A long option must be at least two characters long. Sorry. π
- The
"!"argument specifier ofGetopt::Longis not supported. but you could have options--fooand--nofooand then do something like:
$opt_foo = 0 if $opt_nofoo;
- The
"@"argument specifier ofGetopt::Longis not supported. if you want your values pushed into an array, you'll have to usenextOptionand do it yourself. DIY era. π
π AUTHOR π
Christopher J. Madsen "<perl AT cjmweb.net>"
Please report any bugs or feature requests to "<bug-Getopt-Mixed AT rt.cpan.org>" or through the web interface at <http://rt.cpan.org/Public/Bug/Report.html?Queue=Getopt-Mixed>.
You can follow or contribute to Getopt::Mixed's development at <http://github.com/madsen/getopt-mixed>.
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. π«Ά
π COPYRIGHT AND LICENSE π
This software is copyright (c) 1995 by Christopher J. Madsen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
β οΈ DISCLAIMER OF WARRANTY β οΈ
bestie. this part is in ALL CAPS for a reason. it is not a vibe, it is a warning. read it. π¨
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
and that's the whole page bestie!! π this edition lives at perl v5.28.0, dated 2018-09-09, and it's filed under CIL::Mixed(3pm). go forth and parse some options, you absolute icon. πβ¨π¦
π¦ where this page comes from
- ubuntu jammy: cil 0.07.00-12 Β· GPL-3.0-or-later
- ubuntu noble: cil 0.07.00-12 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms