OptionParser parses command line options from a tool’s POD. By default it parses a description and usage from the POD’s SYNOPSIS section and command line options from the OPTIONS section.
The SYNOPSIS section should look like,
=head1 SYNOPSIS Usage: mk-archiver [OPTION...] --source DSN --where WHERE mk-archiver nibbles records from a MySQL table. The --source and --dest arguments use DSN syntax; if COPY is yes, --dest defaults to the key's value from --source. Examples: ...
The key, required parts are the “Usage:” line and the following description paragraph.
The OPTIONS section shoud look like,
=head1 OPTIONS Optional rules, one per line. =over =item --analyze type: string Run ANALYZE TABLE afterwards on L<"--source"> and/or L<"--dest">. ect.
The option’s full name is given as the “=item”. The next, optional para is the option’s attributes. And the next, required para is the option’s description (the first period-terminated sentence).
OptionParser | OptionParser parses command line options from a tool’s POD. |
Variables | |
$POD_link_re | |
Functions | |
new | |
get_specs | Read and parse options from the OPTIONS section of the POD. |
DSNParser | Return the DSNParser object automatically created for DSN type opts. |
get_defaults_files | Return the program’s defaults files. |
_pod_to_specs() | Parse basic specs for each option. |
_parse_specs | Parse option specs and rules. |
_get_participants | Extract option names from a string. |
opts | A copy of the internal opts hash |
short_opts | A copy of the internal short_opts hash |
set_defaults | Set default values for options. |
get_defaults | |
get_groups | |
_set_option | Getopt::Long calls this sub for each opt it finds on the cmd line. |
get_opts | Get command line options and enforce option rules. |
_check_opts | Check options against rules and group restrictions. |
_validate_type | Validate special option types like sizes and DSNs. |
get | Get an option’s value. |
got | Test if an option was explicitly given on the command line. |
has | Test if an option exists (i.e. |
set | Set an option’s value. |
save_error | Save an error message to be reported later by usage_or_errors(). |
errors | Used for testing. |
usage | |
descr | |
usage_or_errors | |
print_errors | |
print_usage | |
prompt_noecho | |
read_para_after | Read the POD paragraph after a magical regex. |
sub new
%args | Arguments |
file | Filename to parse POD stuff from. Several subs take a $file param mostly for testing purposes. This arg provides a “global” default for even easier testing. |
description | Tool’s description (overrides description from SYNOPSIS). |
usage | Tool’s usage line (overrides Usage from SYNOPSIS). |
head1 | head1 heading under which options are listed |
skip_rules | Don’t read paras before options as rules |
item | Regex pattern to match options after =item |
attributes | Hashref of allowed option attributes |
parse_attributes | Coderef for parsing option attributes |
OptionParser object
sub get_specs
Read and parse options from the OPTIONS section of the POD. This sub should be called first, then get_opts(). _pod_to_specs() and _parse_specs() do most of the work. If the POD has a DSN OPTIONS section then a DSNParser object is created which can be accessed with DSNParser().
$file | File name to read, FILE if none given |
sub _pod_to_specs
Parse basic specs for each option. Each opt spec is a hashref like:
{ spec => GetOpt::Long specification, desc => short description for --help group => option group (default: 'default') }
This is step 1 of 2 of parsing the POD opts. The second is C<_parse_specs()>.
$file | File name to read, FILE if none given |
Array of opt spec hashrefs to pass to _parse_specs().
sub _parse_specs
Parse option specs and rules. The opt specs and rules are returned by _pod_to_specs(). The following attributes are added to each opt spec:
short => the option's short key (-A for --charset) is_cumulative => true if the option is cumulative is_negatable => true if the option is negatable is_required => true if the option is required type => the option's type, one of $self->{types} got => true if the option was given explicitly on the cmd line value => the option's value
@specs | Opt specs and rules from _pod_to_specs() |
sub _get_participants
Extract option names from a string. This is used to find the “participants” of option rules (i.e. the options to which a rule applies).
$str | String containing option names like “Options L<”--[no]foo”> and --bar are mutually exclusive.” |
Array of option names
sub get_opts
Get command line options and enforce option rules. Option values are saved internally in $self->{opts} and accessed later by get(), got(), and set(). Call get_specs() before calling this sub.
sub save_error
Save an error message to be reported later by usage_or_errors().
$error | Error message |
Read the POD paragraph after a magical regex. This is used, for exmaple, to get default CREATE TABLE from the POD. We write something like:
This is the default MAGIC_foo_table: CREATE TABLE `foo` (i INT) Blah blah...
Then to get that CREATE TABLE, you pass “MAGIC_foo_table” as the magical regex.
$file | File to read |
$regex | Regex to find something magical before the desired POD paragraph |
POD paragraph after magical regex
my $POD_link_re
sub new
Read and parse options from the OPTIONS section of the POD.
sub get_specs
Return the DSNParser object automatically created for DSN type opts.
sub DSNParser
Return the program’s defaults files.
sub get_defaults_files
Parse basic specs for each option.
sub _pod_to_specs
Parse option specs and rules.
sub _parse_specs
Extract option names from a string.
sub _get_participants
A copy of the internal opts hash
sub opts
A copy of the internal short_opts hash
sub short_opts
Set default values for options.
sub set_defaults
sub get_defaults
sub get_groups
Getopt::Long calls this sub for each opt it finds on the cmd line.
sub _set_option
Get command line options and enforce option rules.
sub get_opts
Check options against rules and group restrictions.
sub _check_opts
Validate special option types like sizes and DSNs.
sub _validate_type
Get an option’s value.
sub get
Test if an option was explicitly given on the command line.
sub got
Test if an option exists (i.e.
sub has
Set an option’s value.
sub set
Save an error message to be reported later by usage_or_errors().
sub save_error
sub usage_or_errors
Used for testing.
sub errors
sub usage
sub descr
sub print_errors
sub print_usage
sub prompt_noecho