.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" 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 >0, 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 "Tabular 3pm" .TH Tabular 3pm "2022-10-13" "perl v5.34.0" "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::Tabular \- table\-driven argument parsing for Perl 5 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Getopt::Tabular; .Ve .PP (or) .PP .Vb 3 \& use Getopt::Tabular qw/GetOptions \& SetHelp SetHelpOption \& SetError GetError/; \& \& ... \& \& &Getopt::Tabular::SetHelp (long_help, usage_string); \& \& @opt_table = ( \& [section_description, "section"], \& [option, type, num_values, option_data, help_string], \& ... \& ); \& &GetOptions (\e@opt_table, \e@ARGV [, \e@newARGV]) || exit 1; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fBGetopt::Tabular\fR is a Perl 5 module for table-driven argument parsing, vaguely inspired by John Ousterhout's Tk_ParseArgv. All you really need to do to use the package is set up a table describing all your command-line options, and call &GetOptions with three arguments: a reference to your option table, a reference to \f(CW@ARGV\fR (or something like it), and an optional third array reference (say, to \f(CW@newARGV\fR). &GetOptions will process all arguments in \f(CW@ARGV\fR, and copy any leftover arguments (i.e. those that are not options or arguments to some option) to the \f(CW@newARGV\fR array. (If the \f(CW@newARGV\fR argument is not supplied, \f(CW\*(C`GetOptions\*(C'\fR will replace \f(CW@ARGV\fR with the stripped-down argument list.) If there are any invalid options, \f(CW\*(C`GetOptions\*(C'\fR will print an error message and return 0. .PP Before I tell you all about why Getopt::Tabular is a wonderful thing, let me explain some of the terminology that will keep popping up here. .IP "argument" 4 .IX Item "argument" any single word appearing on the command-line, i.e. one element of the \&\f(CW@ARGV\fR array. .IP "option" 4 .IX Item "option" an argument that starts with a certain sequence of characters; the default is \*(L"\-\*(R". (If you like GNU-style options, you can change this to \*(L"\-\-\*(R".) In most Getopt::Tabular\-based applications, options can come anywhere on the command line, and their order is unimportant (unless one option overrides a previous option). Also, Getopt::Tabular will allow any non-ambiguous abbreviation of options. .IP "option argument" 4 .IX Item "option argument" (or \fIvalue\fR) an argument that immediately follows certain types of options. For instance, if \f(CW\*(C`\-foo\*(C'\fR is a scalar-valued integer option, and \&\f(CW\*(C`\-foo 3\*(C'\fR appears on the command line, then \f(CW3\fR will be the argument to \&\f(CW\*(C`\-foo\*(C'\fR. .IP "option type" 4 .IX Item "option type" controls how \f(CW\*(C`GetOptions\*(C'\fR deals with an option and the arguments that follow it. (Actually, for most option types, the type interacts with the \&\f(CW\*(C`num_values\*(C'\fR field, which determines whether the option is scalar\- or vector-valued. This will be fully explained in due course.) .SH "FEATURES" .IX Header "FEATURES" Now for the advertising, i.e. why Getopt::Tabular is a good thing. .IP "\(bu" 4 Command-line arguments are carefully type-checked, both by pattern and number\-\-\-e.g. if an option requires two integers, GetOptions makes sure that exactly two integers follow it! .IP "\(bu" 4 The valid command-line arguments are specified in a data structure separate from the call to GetOptions; this makes it easier to have very long lists of options, and to parse options from multiple sources (e.g. the command line, an environment variable, and a configuration file). .IP "\(bu" 4 Getopt::Tabular can intelligently generate help text based on your option descriptions. .IP "\(bu" 4 The type system is extensible, and if you can define your desired argument type using a single Perl regular expression then it's particularly easy to extend. .IP "\(bu" 4 To make your program look smarter, options can be abbreviated and come in any order. .IP "\(bu" 4 .Sp .Vb 3 \& You can parse options in a "spoof" mode that has no side\-effects \-\- this \&is useful for making a validation pass over the command line without \&actually doing anything. .Ve .PP In general, I have found that Getopt::Tabular tends to encourage programs with long lists of sophisticated options, leading to great flexibility, intelligent operation, and the potential for insanely long command lines. .SH "BASIC OPERATION" .IX Header "BASIC OPERATION" The basic operation of Getopt::Tabular is driven by an \fIoption table\fR, which is just a list of \fIoption descriptions\fR (otherwise known as option table entries, or just entries). Each option description tells \&\f(CW\*(C`GetOptions\*(C'\fR everything it needs to know when it encounters a particular option on the command line. For instance, .PP .Vb 1 \& ["\-foo", "integer", 2, \e@Foo, "set the foo values"] .Ve .PP means that whenever \f(CW\*(C`\-foo\*(C'\fR is seen on the command line, \f(CW\*(C`GetOptions\*(C'\fR is to make sure that the next two arguments are integers, and copy them into the caller's \f(CW@Foo\fR array. (Well, really into the \f(CW@Foo\fR array where the option table is defined. This is almost always the same as \f(CW\*(C`GetOptions\*(C'\fR' caller, though.) .PP Typically, you'll group a bunch of option descriptions together like this: .PP .Vb 9 \& @options = \& (["\-range", "integer", 2, \e@Range, \& "set the range of allowed values"], \& ["\-file", "string", 1, \e$File, \& "set the output file"], \& ["\-clobber", "boolean", 0, \e$Clobber, \& "clobber existing files"], \& ... \& ); .Ve .PP and then call \f(CW\*(C`GetOptions\*(C'\fR like this: .PP .Vb 1 \& &GetOptions (\e@options, \e@ARGV) || exit 1; .Ve .PP which replaces \f(CW@ARGV\fR with a new array containing all the arguments left-over after options and their arguments have been removed. You can also call \f(CW\*(C`GetOptions\*(C'\fR with three arguments, like this: .PP .Vb 1 \& &GetOptions (\e@options, \e@ARGV, \e@newARGV) || exit 1; .Ve .PP in which case \f(CW@ARGV\fR is untouched, and \f(CW@newARGV\fR gets the leftover arguments. .PP In case of error, \f(CW\*(C`GetOptions\*(C'\fR prints enough information for the user to figure out what's going wrong. If you supply one, it'll even print out a brief usage message in case of error. Thus, it's enough to just \f(CW\*(C`exit 1\*(C'\fR when \f(CW\*(C`GetOptions\*(C'\fR indicates an error by returning 0. .PP Detailed descriptions of the contents of an option table entry are given next, followed by the complete run-down of available types, full details on error handling, and how help text is generated. .SH "OPTION TABLE ENTRIES" .IX Header "OPTION TABLE ENTRIES" The fields in the option table control how arguments are parsed, so it's important to understand each one in turn. First, the format of entries in the table is fairly rigid, even though this isn't really necessary with Perl. It's done that way to make the Getopt::Tabular code a little easier; the drawback is that some entries will have unused values (e.g. the \&\f(CW\*(C`num_values\*(C'\fR field is never used for boolean options, but you still have to put something there as a place-holder). The fields are as follows: .IP "option" 4 .IX Item "option" This is the option name, e.g. \*(L"\-verbose\*(R" or \*(L"\-some_value\*(R". For most option types, this is simply an option prefix followed by text; for boolean options, however, it can be a little more complicated. (The exact rules are discussed under \*(L"\s-1OPTION TYPES\*(R"\s0.) And yes, even though you tell Getopt::Tabular the valid option prefixes, you still have to put one onto the option names in the table. .IP "type" 4 .IX Item "type" The option type decides what action will be taken when this option is seen on the command line, and (if applicable) what sort of values will be accepted for this option. There are three broad classes of types: those that imply copying data from the command line into some variable in the caller's space; those that imply copying constant data into the caller's space without taking any more arguments from the command line; and those that imply some other action to be taken. The available option types are covered in greater detail below (see \*(L"\s-1OPTION TYPES\*(R"\s0), but briefly: \&\f(CW\*(C`string\*(C'\fR, \f(CW\*(C`integer\*(C'\fR, and \f(CW\*(C`float\*(C'\fR all imply copying values from the command line to a variable; \f(CW\*(C`constant\*(C'\fR, \f(CW\*(C`boolean\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, \&\f(CW\*(C`arrayconst\*(C'\fR, and \f(CW\*(C`hashconst\*(C'\fR all imply copying some pre-defined data into a variable; \f(CW\*(C`call\*(C'\fR and \f(CW\*(C`eval\*(C'\fR allow the execution of some arbitrary subroutine or chunk of code; and \f(CW\*(C`help\*(C'\fR options will cause \f(CW\*(C`GetOptions\*(C'\fR to print out all available help text and return 0. .IP "num_values" 4 .IX Item "num_values" for \f(CW\*(C`string\*(C'\fR, \f(CW\*(C`integer\*(C'\fR, and \f(CW\*(C`float\*(C'\fR options, this determines whether the option is a scalar (\fBnum_values\fR = 1) or vector (\fBnum_values\fR > 1) option. (Note that whether the option is scalar\- or vector-valued has an important influence on what you must supply in the \fBoption_data\fR field!) For \f(CW\*(C`constant\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, \f(CW\*(C`arrayconst\*(C'\fR, and \f(CW\*(C`hashconst\*(C'\fR option types, \&\fBnum_values\fR is a bit of a misnomer: it actually contains the value (or a reference to it, if array or hash) to be copied when the option is encountered. For \f(CW\*(C`call\*(C'\fR options, \fBnum_values\fR can be used to supply extra arguments to the called subroutine. In any case, though, you can think of \fBnum_values\fR as an input value. For \f(CW\*(C`boolean\*(C'\fR and \f(CW\*(C`eval\*(C'\fR options, \fBnum_values\fR is ignored and should be \f(CW\*(C`undef\*(C'\fR or 0. .IP "option_data" 4 .IX Item "option_data" For \f(CW\*(C`string\*(C'\fR, \f(CW\*(C`integer\*(C'\fR, \f(CW\*(C`float\*(C'\fR, \f(CW\*(C`boolean\*(C'\fR, \f(CW\*(C`constant\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, \&\f(CW\*(C`arrayconst\*(C'\fR, and \f(CW\*(C`hashconst\*(C'\fR types, this must be a reference to the variable into which you want \f(CW\*(C`GetOptions\*(C'\fR to copy the appropriate thing. The \*(L"appropriate thing\*(R" is either the argument(s) following the option, the constant supplied as \fBnum_values\fR, or 1 or 0 (for boolean options). .Sp For \f(CW\*(C`boolean\*(C'\fR, \f(CW\*(C`constant\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, and scalar-valued \f(CW\*(C`string\*(C'\fR, \&\f(CW\*(C`integer\*(C'\fR, and \f(CW\*(C`float\*(C'\fR options, this must be a scalar reference. For vector-valued \f(CW\*(C`string\*(C'\fR, \f(CW\*(C`integer\*(C'\fR, and \f(CW\*(C`float\*(C'\fR options (\fBnum_values\fR > 1), and for \f(CW\*(C`arrayconst\*(C'\fR options, this must be an array reference. For \&\f(CW\*(C`hashconst\*(C'\fR options, this must be a hash reference. .Sp Finally, \fBoption_data\fR is also used as an input value for \f(CW\*(C`call\*(C'\fR and \&\f(CW\*(C`eval\*(C'\fR options: for \f(CW\*(C`call\*(C'\fR, it should be a subroutine reference, and for \&\f(CW\*(C`eval\*(C'\fR options, it should be a string containing valid Perl code to evaluate when the option is seen. The subroutine called by a \f(CW\*(C`call\*(C'\fR option should take at least two arguments: a string, which is the actual option that triggered the call (because the same subroutine could be tied to many options), and an array reference, which contains all command line arguments after that option. (Further arguments can be supplied in the \&\fBnum_values\fR field.) The subroutine may freely modify this array, and those modifications will affect the behaviour of \f(CW\*(C`GetOptions\*(C'\fR afterwards. .Sp The chunk of code passed to an \f(CW\*(C`eval\*(C'\fR option is evaluated in the package from which \f(CW\*(C`GetOptions\*(C'\fR is called, and does not have access to any internal Getopt::Tabular data. .IP "help_string" 4 .IX Item "help_string" (optional) a brief description of the option. Don't worry about formatting this in any way; when \f(CW\*(C`GetOptions\*(C'\fR has to print out your help, it will do so quite nicely without any intervention. If the help string is not defined, then that option will not be included in the option help text. (However, you could supply an empty string \*(-- which is defined \*(-- to make \f(CW\*(C`GetOptions\*(C'\fR just print out the option name, but nothing else.) .IP "arg_desc" 4 .IX Item "arg_desc" (optional) an even briefer description of the values that you expect to follow your option. This is mainly used to supply place-holders in the help string, and is specified separately so that \f(CW\*(C`GetOptions\*(C'\fR can act fairly intelligently when formatting a help message. See \*(L"\s-1HELP TEXT\*(R"\s0 for more information. .SH "OPTION TYPES" .IX Header "OPTION TYPES" The option type field is the single-most important field in the table, as the type for an option \f(CW\*(C`\-foo\*(C'\fR determines (along with \fBnum_values\fR) what action \f(CW\*(C`GetOptions\*(C'\fR takes when it sees \f(CW\*(C`\-foo\*(C'\fR on the command line: how many following arguments become \f(CW\*(C`\-foo\*(C'\fR's arguments, what regular expression those arguments must conform to, or whether some other action should be taken. .PP As mentioned above, there are three main classes of argument types: .IP "argument-driven options" 4 .IX Item "argument-driven options" These are options that imply taking one or more option arguments from the command line after the option itself is taken. The arguments are then copied into some variable supplied (by reference) in the option table entry. .IP "constant-valued options" 4 .IX Item "constant-valued options" These are options that have a constant value associated with them; when the option is seen on the command line, that constant is copied to some variable in the caller's space. (Both the constant and the value are supplied in the option table entry.) Constants can be scalars, arrays, or hashes. .IP "other options" 4 .IX Item "other options" These imply some other action to be taken, usually supplied as a string to \f(CW\*(C`eval\*(C'\fR or a subroutine to call. .SS "Argument-driven option types" .IX Subsection "Argument-driven option types" .IP "string, integer, float" 4 .IX Item "string, integer, float" These are the option types that imply \*(L"option arguments\*(R", i.e. arguments after the option that will be consumed when that option is encountered on the command line and copied into the caller's space via some reference. For instance, if you want an option \f(CW\*(C`\-foo\*(C'\fR to take a single string as an argument, with that string being copied to the scalar variable \&\f(CW$Foo\fR, then you would have this entry in your option table: .Sp .Vb 1 \& ["\-foo", "string", 1, \e$Foo] .Ve .Sp (For conciseness, I've omitted the \fBhelp_string\fR and \fBargdesc\fR entries in all of the example entries in this section. In reality, you should religiously supply help text in order to make your programs easier to use and easier to maintain.) .Sp If \fBnum_values\fR is some \fIn\fR greater than one, then the \fBoption_data\fR field must be an \fIarray\fR reference, and \fIn\fR arguments are copied from the command line into that array. (The array is clobbered each time \&\f(CW\*(C`\-foo\*(C'\fR is encountered, not appended to.) In this case, \f(CW\*(C`\-foo\*(C'\fR is referred to as a \fIvector-valued\fR option, as it must be followed by a fixed number of arguments. (Eventually, I plan to add \fIlist-valued\fR options, which take a variable number of arguments.) For example an option table like .Sp .Vb 1 \& ["\-foo", "string", 3, \e@Foo] .Ve .Sp would result in the \f(CW@Foo\fR array being set to the three strings immediately following any \f(CW\*(C`\-foo\*(C'\fR option on the command line. .Sp The only difference between \fBstring\fR, \fBinteger\fR, and \fBfloat\fR options is how picky \f(CW\*(C`GetOptions\*(C'\fR is about the value(s) it will accept. For \&\fBstring\fR options, anything is \s-1OK\s0; for \fBinteger\fR options, the values must look like integers (i.e., they must match \f(CW\*(C`/[+\-]?\ed+/\*(C'\fR); for \fBfloat\fR options, the values must look like C floating point numbers (trust me, you don't want to see the regexp for this). Note that since string options will accept anything, they might accidentally slurp up arguments that are meant to be further options, if the user forgets to put the correct string. For instance, if \f(CW\*(C`\-foo\*(C'\fR and \f(CW\*(C`\-bar\*(C'\fR are both scalar-valued string options, and the arguments \f(CW\*(C`\-foo \-bar\*(C'\fR are seen on the command-line, then \*(L"\-bar\*(R" will become the argument to \f(CW\*(C`\-foo\*(C'\fR, and never be processed as an option itself. (This could be construed as either a bug or a feature. If you feel really strongly that it's a bug, then complain and I'll consider doing something about it.) .Sp If not enough arguments are found that match the required regular expression, \f(CW\*(C`GetOptions\*(C'\fR prints to standard error a clear and useful error message, followed by the usage summary (if you supplied one), and returns 0. The error messages look something like \*(L"\-foo option must be followed by an integer\*(R", or \*(L"\-foo option must be followed by 3 strings\*(R", so it really is enough for your program to \f(CW\*(C`exit 1\*(C'\fR without printing any further message. .IP "User-defined patterns" 4 .IX Item "User-defined patterns" Since the three option types described above are defined by nothing more than a regular expression, it's easy to define your own option types. For instance, let's say you want an option to accept only strings of upper-case letters. You could then call \f(CW&Getopt::Tabular::AddPatternType\fR as follows: .Sp .Vb 2 \& &Getopt::Tabular::AddPatternType \& ("upperstring", "[A\-Z]+", "uppercase string") .Ve .Sp Note that the third parameter is optional, and is only supplied to make error messages clearer. For instance, if you now have a scalar-valued option \f(CW\*(C`\-zap\*(C'\fR of type \f(CW\*(C`upperstring\*(C'\fR: .Sp .Vb 1 \& ["\-zap", "upperstring", 1, \e$Zap] .Ve .Sp and the user gets it wrong and puts an argument that doesn't consist of all uppercase letters after \f(CW\*(C`\-zap\*(C'\fR, then \f(CW\*(C`GetOptions\*(C'\fR will complain that \*(L"\-zap option must be followed by an uppercase string\*(R". If you hadn't supplied the third argument to \f(CW&AddType\fR, then the error message would have been the slightly less helpful \*(L"\-zap option must be followed by an upperstring\*(R". Also, you might have to worry about how \&\f(CW\*(C`GetOptions\*(C'\fR pluralizes your description: in this case, it will simply add an \*(L"s\*(R", which works fine much of the time, but not always. Alternately, you could supply a two-element list containing the singular and plural forms: .Sp .Vb 3 \& &Getopt::Tabular::AddPatternType \& ("upperstring", "[A\-Z]+", \& ["string of uppercase letters", "strings of uppercase letters"]) .Ve .Sp So, if \f(CW\*(C`\-zap\*(C'\fR instead expects three \f(CW\*(C`upperstring\*(C'\fRs, and the user goofs, then the error message would be (in the first example) \*(L"\-zap option must be followed by 3 uppercase strings\*(R" or \*(L"\-zap option must be followed by three strings of uppercase letters\*(R" (second example). .Sp Of course, if you don't intend to have vector-valued options of your new type, pluralization hardly matters. Also, while it might seem that this is a nice stab in the direction of multi-lingual support, the error messages are still hard-coded to English in other places. Maybe in the next version... .SS "Constant-valued option types" .IX Subsection "Constant-valued option types" .IP "boolean" 4 .IX Item "boolean" For \fBboolean\fR options, \fBoption_data\fR must be a scalar reference; \&\fBnum_values\fR is ignored (you can just set it to \f(CW\*(C`undef\*(C'\fR or 0). Booleans are slightly weird in that every boolean option implies \fItwo\fR possible arguments that will be accepted on the command line, called the positive and negative alternatives. The positive alternative (which is what you specify as the option name) results in a true value, while the negative alternative results in false. Most of the time, you can let \&\f(CW\*(C`GetOptions\*(C'\fR pick the negative alternative for you: it just inserts \&\*(L"no\*(R" after the option prefix, so \*(L"\-clobber\*(R" becomes \*(L"\-noclobber\*(R". (More precisely, \f(CW\*(C`GetOptions\*(C'\fR tests all option prefixes until one of them matches at the beginning of the option name. It then inserts \*(L"no\*(R" between this prefix and the rest of the string. So, if you want to support both GNU-style options (like \f(CW\*(C`\-\-clobber\*(C'\fR) and one-hyphen options (\f(CW\*(C`\-c\*(C'\fR), be sure to give \*(L"\-\-\*(R" \fIfirst\fR when setting the option patterns with \f(CW&SetOptionPatterns\fR. Otherwise, the negative alternative to \*(L"\-\-clobber\*(R" will be \*(L"\-no\-clobber\*(R", which might not be what you wanted.) Sometimes, though, you want to explicitly specify the negative alternative. This is done by putting both alternatives in the option name, separated by a vertical bar, e.g. \*(L"\-verbose|\-quiet\*(R". .Sp For example, the above two examples might be specified as .Sp .Vb 2 \& ["\-clobber", "boolean", undef, \e$Clobber], \& ["\-verbose|\-quiet", "boolean", undef, \e$Verbose],...); .Ve .Sp If \f(CW\*(C`\-clobber\*(C'\fR is seen on the command line, \f(CW$Clobber\fR will be set to 1; if \f(CW\*(C`\-noclobber\*(C'\fR is seen, then \f(CW$Clobber\fR will be set to 0. Likewise, \&\f(CW\*(C`\-verbose\*(C'\fR results in \f(CW$Verbose\fR being set to 1, and \f(CW\*(C`\-quiet\*(C'\fR will set \&\f(CW$Verbose\fR to 0. .IP "const" 4 .IX Item "const" For \fBconst\fR options, put a scalar value (\fInot\fR reference) in \&\fBnum_values\fR, and a scalar reference in \fBoption_data\fR. For example: .Sp .Vb 1 \& ["\-foo", "const", "hello there", \e$Foo] .Ve .Sp On encountering \f(CW\*(C`\-foo\*(C'\fR, \f(CW\*(C`GetOptions\*(C'\fR will copy \*(L"hello there\*(R" to \f(CW$Foo\fR. .IP "arrayconst" 4 .IX Item "arrayconst" For \fBarrayconst\fR options, put an array reference (input) (\fInot\fR an array value) in \fBnum_values\fR, and another array reference (output) in \&\fBoption_data\fR. For example: .Sp .Vb 1 \& ["\-foo", "arrayconst", [3, 6, 2], \e@Foo] .Ve .Sp On encountering \f(CW\*(C`\-foo\*(C'\fR, \f(CW\*(C`GetOptions\*(C'\fR will copy the array \f(CW\*(C`(3,6,2)\*(C'\fR into \&\f(CW@Foo\fR. .IP "hashconst" 4 .IX Item "hashconst" For \fBhashconst\fR options, put a hash reference (input) (\fInot\fR a hash value) in \fBnum_values\fR, and another hash reference (output) in \&\fBoption_data\fR. For example: .Sp .Vb 4 \& ["\-foo", "hashconst", { "Perl" => "Larry Wall", \& "C" => "Dennis Ritchie", \& "Pascal" => "Niklaus Wirth" }, \& \e%Inventors] .Ve .Sp On encountering \f(CW\*(C`\-foo\*(C'\fR, \f(CW\*(C`GetOptions\*(C'\fR will copy into \f(CW%Inventors\fR a hash relating various programming languages to the culprits primarily responsible for their invention. .IP "copy" 4 .IX Item "copy" \&\fBcopy\fR options act just like \fBconst\fR options, except when \&\fBnum_values\fR is undefined. In that case, the option name itself will be copied to the scalar referenced by \fBoption_data\fR, rather than the \&\f(CW\*(C`undef\*(C'\fR value that would be copied under these circumstances with a \&\fBconst\fR option. This is useful when one program accepts options that it simply passes to a sub-program; for instance, if \fIprog1\fR calls \&\fIprog2\fR, and \fIprog2\fR might be run with the \-foo option, then \&\fIprog1\fR's argument table might have this option: .Sp .Vb 2 \& ["\-foo", "copy", undef, \e$Foo, \& "run prog2 with the \-foo option"] .Ve .Sp and later on, you would run \fIprog2\fR like this: .Sp .Vb 1 \& system ("prog2 $Foo ..."); .Ve .Sp That way, if \f(CW\*(C`\-foo\*(C'\fR is never seen on \fIprog1\fR's command line, \f(CW$Foo\fR will be untouched, and will expand to the empty string when building the command line for \fIprog2\fR. .Sp If \fBnum_values\fR is anything other than \f(CW\*(C`undef\*(C'\fR, then \fBcopy\fR options behave just like \fBconstant\fR options. .SS "Other option types" .IX Subsection "Other option types" .IP "call" 4 .IX Item "call" For \fBcall\fR options, \fBoption_data\fR must be a reference to a subroutine. The subroutine will be called with at least two arguments: a string containing the option that triggered the call (because the same subroutine might be activated by many options), a reference to an array containing all remaining command-line arguments after the option, and other arguments specified using the \fBnum_values\fR field. (To be used for this purpose, \fBnum_values\fR must be an array reference; otherwise, it is ignored.) For example, you might define a subroutine .Sp .Vb 3 \& sub process_foo \& { \& my ($opt, $args, $dest) = @_; \& \& $$dest = shift @$args; # not quite right! (see below) \& } .Ve .Sp with a corresponding option table entry: .Sp .Vb 1 \& ["\-foo", "call", [\e$Foo], \e&process_foo] .Ve .Sp and then \f(CW\*(C`\-foo\*(C'\fR would act just like a scalar-valued string option that copies into \f(CW$Foo\fR. (Well, \fIalmost\fR ... read on.) .Sp A subtle point that might be missed from the above code: the value returned by \f(CW&process_foo\fR \fIdoes\fR matter: if it is false, then \f(CW\*(C`GetOptions\*(C'\fR will return 0 to its caller, indicating failure. To make sure that the user gets a useful error message, you should supply one by calling \f(CW\*(C`SetError\*(C'\fR; doing so will prevent \f(CW\*(C`GetOptions\*(C'\fR from printing out a rather mysterious (to the end user, at least) message along the lines of \*(L"subroutine call failed\*(R". The above example has two subtle problems: first, if the argument following \f(CW\*(C`\-foo\*(C'\fR is an empty string, then \f(CW\*(C`process_foo\*(C'\fR will return the empty string\-\-\-a false value\-\-\-thus causing \f(CW\*(C`GetOptions\*(C'\fR to fail confusingly. Second, if there no arguments after \f(CW\*(C`\-foo\*(C'\fR, then \&\f(CW\*(C`process_foo\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR\-\-\-again, a false value, causing \&\f(CW\*(C`GetOptions\*(C'\fR to fail. .Sp To solve these problems, we have to define the requirements for the \&\f(CW\*(C`\-foo\*(C'\fR option a little more rigorously. Let's say that any string (including the empty string) is valid, but that there must be something there. Then \f(CW\*(C`process_foo\*(C'\fR is written as follows: .Sp .Vb 3 \& sub process_foo \& { \& my ($opt, $args, $dest) = @_; \& \& $$dest = shift @$args; \& (defined $$dest) && return 1; \& &Getopt::Tabular::SetError \& ("bad_foo", "$opt option must be followed by a string"); \& return 0; \& } .Ve .Sp The \f(CW\*(C`SetError\*(C'\fR routine actually takes two arguments: an error class and an error message. This is explained fully in the \*(L"\s-1ERROR HANDLING\*(R"\s0 section, below. And, if you find yourself writing a lot of routines like this, \f(CW\*(C`SetError\*(C'\fR is optionally exported from \f(CW\*(C`Getopt::Tabular\*(C'\fR, so you can of course import it into your main package like this: .Sp .Vb 1 \& use Getopt::Tabular qw/GetOptions SetError/; .Ve .IP "eval" 4 .IX Item "eval" An \fBeval\fR option specifies a chunk of Perl code to be executed (\f(CW\*(C`eval\*(C'\fR'd) when the option is encountered on the command line. The code is supplied (as a string) in the \fBoption_data\fR field; again, \&\fBnum_values\fR is ignored. For example: .Sp .Vb 2 \& ["\-foo", "eval", undef, \& \*(Aqprint "\-foo seen on command line\en"\*(Aq] .Ve .Sp will cause \f(CW\*(C`GetOptions\*(C'\fR to print out (via an \f(CW\*(C`eval\*(C'\fR) the string \*(L"\-foo seen on the command line\en\*(R" when \-foo is seen. No other action is taken apart from what you include in the eval string. The code is evaluated in the package from which \f(CW\*(C`GetOptions\*(C'\fR was called, so you can access variables and subroutines in your program easily. If any error occurs in the \f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`GetOptions\*(C'\fR complains loudly and returns 0. .Sp Note that the supplied code is always evaluated in a \f(CW\*(C`no strict\*(C'\fR environment\-\-\-that's because \fIGetopt::Tabular\fR is itself \f(CW\*(C`use strict\*(C'\fR\-compliant, and I didn't want to force strictness on every quick hack that uses the module. (Especially since \fBeval\fR options seem to be used mostly in quick hacks.) (Anyone who knows how to fetch the strictness state for another package or scope is welcome to send me hints!) However, the \fB\-w\fR state is untouched. .IP "section" 4 .IX Item "section" \&\fBsection\fR options are just used to help formatting the help text. See \&\*(L"\s-1HELP TEXT\*(R"\s0 below for more details. .SH "ERROR HANDLING" .IX Header "ERROR HANDLING" Generally, handling errors in the argument list is pretty transparent: \&\f(CW\*(C`GetOptions\*(C'\fR (or one of its minions) generates an error message and assigns an error class, \f(CW\*(C`GetOptions\*(C'\fR prints the message to the standard error, and returns 0. You can access the error class and error message using the \f(CW\*(C`GetError\*(C'\fR routine: .PP .Vb 1 \& ($err_class, $err_msg) = &Getopt::Tabular::GetError (); .Ve .PP (Like \f(CW\*(C`SetError\*(C'\fR, \f(CW\*(C`GetError\*(C'\fR can also be exported from \&\fIGetopt::Tabular\fR.) The error message is pretty simple\-\-\-it is an explanation for the end user of what went wrong, which is why \&\f(CW\*(C`GetOptions\*(C'\fR just prints it out and forgets about it. The error class is further information that might be useful for your program; the current values are: .IP "bad_option" 4 .IX Item "bad_option" set when something that looks like an option is found on the command line, but it's either unknown or an ambiguous abbreviation. .IP "bad_value" 4 .IX Item "bad_value" set when an option is followed by an invalid argument (i.e., one that doesn't match the regexp for that type), or the wrong number of arguments. .IP "bad_call" 4 .IX Item "bad_call" set when a subroutine called via a \fBcall\fR option or the code evaluated for an \fBeval\fR option returns a false value. The subroutine or eval'd code can override this by calling \f(CW\*(C`SetError\*(C'\fR itself. .IP "bad_eval" 4 .IX Item "bad_eval" set when the code evaluted for an \fBeval\fR option has an error in it. .IP "help" 4 .IX Item "help" set when the user requests help .PP Note that most of these are errors on the end user's part, such as bad or missing arguments. There are also errors that can be caused by you, the programmer, such as bad or missing values in the option table; these generally result in \f(CW\*(C`GetOptions\*(C'\fR croaking so that your program dies immediately with enough information that you can figure out where the mistake is. \fBbad_eval\fR is a borderline case; there are conceivably cases where the end user's input can result in bogus code to evaluate, so I grouped this one in the \*(L"user errors\*(R" class. Finally, asking for help isn't really an error, but the assumption is that you probably shouldn't continue normal processing after printing out the help\-\-\-so \&\f(CW\*(C`GetOptions\*(C'\fR returns 0 in this case. You can always fetch the error class with \f(CW\*(C`GetError\*(C'\fR if you want to treat real errors differently from help requests. .SH "HELP TEXT" .IX Header "HELP TEXT" One of Getopt::Tabular's niftier features is the ability to generate and format a pile of useful help text from the snippets of help you include in your option table. The best way to illustrate this is with a couple of brief examples. First, it's helpful to know how the user can trigger a help display. This is quite simple: by default, \f(CW\*(C`GetOptions\*(C'\fR always has a \*(L"\-help\*(R" option, presence of which on the command line triggers a help display. (Actually, the help option is really your preferred option prefix plus \*(L"help\*(R". So, if you like to make GNU-style options to take precedence as follows: .PP .Vb 1 \& &Getopt::Tabular::SetOptionPatterns qw|(\-\-)([\ew\-]+) (\-)(\ew+)|; .Ve .PP then the help option will be \*(L"\-\-help\*(R". There is only one help option available, and you can set it by calling \f(CW&SetHelpOption\fR (another optional export). .PP Note that in addition to the option help embedded in the option table, \&\f(CW\*(C`GetOptions\*(C'\fR can optionally print out two other messages: a descriptive text (usually a short paragraph giving a rough overview of what your program does, possibly referring the user to the fine manual page), and a usage text. These are both supplied by calling \f(CW&SetHelp\fR, e.g. .PP .Vb 6 \& $Help = <"], \& ["\-threshold", "float", 2, \e@Threshold, \& "only consider values between and ", \& " "]); .Ve .PP Assuming you haven't supplied long help or usage strings, then when \&\f(CW\*(C`GetOptions\*(C'\fR encounters the help option, it will immediately stop parsing arguments and print out the following option summary: .PP .Vb 10 \& Summary of options: \& \-verbose be noisy [default] \& \-quiet opposite of \-verbose \& \-clobber overwrite existing files \& \-noclobber opposite of \-clobber [default] \& \-infile specify the input file from which to read a large and \& sundry variety of data, to which many interesting \& operations will be applied \& \-threshold \& only consider values between and [default: 0 1] .Ve .PP There are a number of interesting things to note here. First, there are three option table fields that affect the generation of help text: \&\fBoption\fR, \fBhelp_string\fR, and \fBargdesc\fR. Note how the \fBargdesc\fR strings are simply option placeholders, usually used to 1) indicate how many values are expected to follow an option, 2) (possibly) imply what form they take (although that's not really shown here), and 3) explain the exact meaning of the values in the help text. \fBargdesc\fR is just a string like the help string; you can put whatever you like in it. What I've shown above is just my personal preference (which may well evolve). .PP A new feature with version 0.3 of Getopt::Tabular is the inclusion of default values with the help for certain options. A number of conditions must be fulfilled for this to happen for a given option: first, the option type must be one of the \*(L"argument-driven\*(R" types, such as \f(CW\*(C`integer\*(C'\fR, \f(CW\*(C`float\*(C'\fR, \f(CW\*(C`string\*(C'\fR, or a user-defined type. Second, the option data field must refer either to a defined scalar value (for scalar-valued options) or to a list of one or more defined values (for vector-valued options). Thus, in the above example, the \f(CW\*(C`\-infile\*(C'\fR option doesn't have its default printed because the \f(CW$InFile\fR scalar is undefined. Likewise, if the \f(CW@Threshold\fR array were the empty list \&\f(CW\*(C`()\*(C'\fR, or a list of undefined values \f(CW\*(C`(undef,undef)\*(C'\fR, then the default value for \f(CW\*(C`\-threshold\*(C'\fR also would not have been printed. .PP The formatting is done as follows: enough room is made on the right hand side for the longest option name, initially omitting the argument placeholders. Then, if an option has placeholders, and there is room for them in between the option and the help string, everything (option, placeholders, help string) is printed together. An example of this is the \f(CW\*(C`\-infile\*(C'\fR option: here, \*(L"\-infile \*(R" is just small enough to fit in the 12\-character column (10 characters because that is the length of the longest option, and 2 blanks), so the help text is placed right after it on the same line. However, the \f(CW\*(C`\-threshold\*(C'\fR option becomes too long when its argument placeholders are appended to it, so the help text is pushed onto the next line. .PP In any event, the help string supplied by the caller starts at the same column, and is filled to make a nice paragraph of help. \f(CW\*(C`GetOptions\*(C'\fR will fill to the width of the terminal (or 80 columns if it fails to find the terminal width). .PP Finally, you can have pseudo entries of type \fBsection\fR, which are important to make long option lists readable (and one consequence of using Getopt::Tabular is programs with ridiculously long option lists \*(-- not altogether a bad thing, I suppose). For example, this table fragment: .PP .Vb 10 \& @argtbl = (..., \& ["\-foo", "integer", 1, \e$Foo, \& "set the foo value", "f"], \& ["\-enterfoomode", "call", 0, \e&enter_foo_mode, \& "enter foo mode"], \& ["Non\-foo related options", "section"], \& ["\-bar", "string", 2, \e@Bar, \& "set the bar strings (which have nothing whatsoever " . \& "to do with foo", " "], \& ...); .Ve .PP results in the following chunk of help text: .PP .Vb 2 \& \-foo f set the foo value \& \-enterfoomode enter foo mode \& \& \-\- Non\-foo related options \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& \-bar b1 b2 set the bar strings (which have nothing \& whatsoever to do with foo .Ve .PP (This example also illustrates a slightly different style of argument placeholder. Take your pick, or invent your own!) .SH "SPOOF MODE" .IX Header "SPOOF MODE" Since callbacks from the command line (\f(CW\*(C`call\*(C'\fR and \f(CW\*(C`eval\*(C'\fR options) can do anything, they might be quite expensive. In certain cases, then, you might want to make an initial pass over the command line to ensure that everything is \s-1OK\s0 before parsing it \*(L"for real\*(R" and incurring all those expensive callbacks. Thus, \f(CW\*(C`Getopt::Tabular\*(C'\fR provides a \*(L"spoof\*(R" mode for parsing a command line without side-effects. In the simplest case, you can access spoof mode like this: .PP .Vb 5 \& use Getopt::Tabular qw(SpoofGetOptions GetOptions); \& . \& . \& . \& &SpoofGetOptions (\e@options, \e@ARGV, \e@newARGV) || exit 1; .Ve .PP and then later on, you would call \f(CW\*(C`GetOptions\*(C'\fR with the \fIoriginal\fR \&\f(CW@ARGV\fR (so it can do what \f(CW\*(C`SpoofGetOptions\*(C'\fR merely pretended to do): .PP .Vb 1 \& &GetOptions (\e@options, \e@ARGV, \e@newARGV) || exit 1; .Ve .PP For most option types, any errors that \f(CW\*(C`GetOptions\*(C'\fR would catch should also be caught by \f(CW\*(C`SpoofGetOptions\*(C'\fR \*(-- so you might initially think that you can get away without that \f(CW\*(C`|| exit 1\*(C'\fR after calling \&\f(CW\*(C`GetOptions\*(C'\fR. However, it's a good idea for a couple of reasons. First, you might inadvertently changed \f(CW@ARGV\fR \*(-- this is usually a bug and a silly thing to do, so you'd probably want your program to crash loudly rather than fail mysteriously later on. Second, and more likely, some of those expensive operations that you're initially avoiding by using \f(CW\*(C`SpoofGetOptions\*(C'\fR might themselves fail \*(-- which would cause \&\f(CW\*(C`GetOptions\*(C'\fR to return false where \f(CW\*(C`SpoofGetOption\*(C'\fR completes without a problem. (Finally, there's the faint possiblity of bugs in \&\f(CW\*(C`Getopt::Tabular\*(C'\fR that would cause different behaviour in spoof mode and real mode \*(-- this really shouldn't happen, though.) .PP In reality, using spoof mode requires a bit more work. In particular, the whole reason for spoof argument parsing is to avoid expensive callbacks, but since callbacks can eat any number of command line arguments, you have to emulate them in some way. It's not possible for \&\f(CW\*(C`SpoofGetOptions\*(C'\fR to do this for you, so you have to help out by supplying \*(L"spoof\*(R" callbacks. As an example, let's say you have a callback option that eats one argument (a filename) and immediately reads that file: .PP .Vb 1 \& @filedata = (); \& \& sub read_file \& { \& my ($opt, $args) = @_; \& \& warn ("$opt option requires an argument\en"), return 0 unless @$args; \& my $file = shift @$args; \& open (FILE, $file) || \& (warn ("$file: $!\en"), return 0); \& push (@filedata, ); \& close (FILE); \& return 1; \& } \& \& @options = \& ([\*(Aq\-read_file\*(Aq, \*(Aqcall\*(Aq, undef, \e&read_file]); .Ve .PP Since \f(CW\*(C`\-read_file\*(C'\fR could occur any number of times on the command line, we might end up reading an awful lot of files, and thus it might be a long time before we catch errors late in the command line. Thus, we'd like to do a \*(L"spoof\*(R" pass over the command line to catch all errors. A simplistic approach would be to supply a spoof callback that just eats one argument and returns success: .PP .Vb 8 \& sub spoof_read_file \& { \& my ($opt, $args) = @_; \& (warn ("$opt option requires an argument\en"), return 0) \& unless @$args; \& shift @$args; \& return 1; \& } .Ve .PP Then, you have to tell \f(CW\*(C`Getopt::Tabular\*(C'\fR about this alternate callback with no side-effects (apart from eating that one argument): .PP .Vb 1 \& &Getopt::Tabular::SetSpoofCodes (\-read_file => \e&spoof_read_file); .Ve .PP (\f(CW\*(C`SetSpoofCodes\*(C'\fR just takes a list of key/value pairs, where the keys are \f(CW\*(C`call\*(C'\fR or \f(CW\*(C`eval\*(C'\fR options, and the values are the \*(L"no side-effects\*(R" callbacks. Naturally, the replacement callback for an \f(CW\*(C`eval\*(C'\fR option should be a string, and for a \f(CW\*(C`call\*(C'\fR option it should be a code reference. This is not actually checked, however, until you call \&\f(CW\*(C`SpoofGetOptions\*(C'\fR, because \f(CW\*(C`SetSpoofCodes\*(C'\fR doesn't know whether options are \f(CW\*(C`call\*(C'\fR or \f(CW\*(C`eval\*(C'\fR or what.) .PP A more useful \f(CW\*(C`spoof_read_file\*(C'\fR, however, would actually check if the requested file exists \*(-- i.e., we should try to catch as many errors as possible, as early as possible: .PP .Vb 10 \& sub spoof_read_file \& { \& my ($opt, $args) = @_; \& warn ("$opt option requires an argument\en"), return 0 \& unless @$args; \& my $file = shift @$args; \& warn ("$file does not exist or is not readable\en"), return 0 \& unless \-r $file; \& return 1; \& } .Ve .PP Finally, you can frequently merge the \*(L"real\*(R" and \*(L"spoof\*(R" callback into one subroutine: .PP .Vb 3 \& sub read_file \& { \& my ($opt, $args, $spoof) = @_; \& \& warn ("$opt option requires an argument\en"), return 0 unless @$args; \& my $file = shift @$args; \& warn ("$file does not exist or is not readable\en"), return 0 \& unless \-r $file; \& return 1 if $spoof; \& open (FILE, $file) || \& (warn ("$file: $!\en"), return 0); \& push (@filedata, ); \& close (FILE); \& return 1; \& } .Ve .PP And then, when specifying the replacement callback to \f(CW\*(C`SetSpoofCodes\*(C'\fR, just create an anonymous sub that calls \f(CW\*(C`read_file\*(C'\fR with \f(CW$spoof\fR true: .PP .Vb 2 \& &Getopt::Tabular::SetSpoofCodes \& (\-read_file => sub { &read_file (@_[0,1], 1) }); .Ve .PP Even though this means a bigger and more complicated callback, you only need \fIone\fR such callback \*(-- the alternative is to carry around both \&\f(CW\*(C`read_file\*(C'\fR and \f(CW\*(C`spoof_read_file\*(C'\fR, which might do redundant processing of the argument list. .SH "AUTHOR" .IX Header "AUTHOR" Greg Ward .PP Started in July, 1995 as ParseArgs.pm, with John Ousterhout's Tk_ParseArgv.c as a loose inspiration. Many many features added over the ensuing months; documentation written in a mad frenzy 16\-18 April, 1996. Renamed to Getopt::Tabular, revamped, reorganized, and documentation expanded 8\-11 November, 1996. .PP Copyright (c) 1995\-97 Greg Ward. All rights reserved. This is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "BUGS" .IX Header "BUGS" The documentation is bigger than the code, and I still haven't covered option patterns or extending the type system (apart from pattern types). Yow! .PP No support for list-valued options, although you can roll your own with \&\fBcall\fR options. (See the demo program included with the distribution for an example.) .PP Error messages are hard-coded to English.