Getopt modules 10: Getopt::Auto

About this mini-article series. Each day for 24 days, I will be reviewing a module that parses command-line options (such module is usually under the Getopt::* namespace). First article is here.

Getopt::Auto is a module that was first written in 2003 by Simon Cozens (SIMON) then revived in 2011, 2014 by Geoffrey Leach (GLEACH). There is currently only one CPAN distribution depending on this module.

% lcpan rdeps Getopt::Auto
+---------+----------+--------------+--------+--------------+-------------+
| phase   | rel      | dist         | author | dist_version | req_version |
+---------+----------+--------------+--------+--------------+-------------+
| runtime | requires | Pod-HtmlEasy | GLEACH | v1.1.11      | 1.009006    |
+---------+----------+--------------+--------+--------------+-------------+

It does not require you to write an options spec (but you can, if you want to). You can just do:

use Getopt::Auto;

and your command-line options will be collected in main‘s %options. You can also define option subroutine named like the option, e.g. foo() which will be called when the option is encountered. This is similar to supplying an option handler coderef in Getopt::Long, except in a non-global-clean way.

Of course, when not using a spec there’s the usual issue of syntax ambiguity. Getopt::Auto resolves this by assuming that all options in the form of --foo
are flag options that do not take values, and options that want to take value must use the --foo=value syntax.

If you want to specify a spec, you can, in the form of POD (like Getopt::Euclid). However, unlike Getopt::Euclid, the rules are far fewer and simpler. You just provide a header (it can be =head2, =head3, or some other level) in this format:

=head2 -a, --add - Some summary

And that’s it.

There’s no way of specifying that an option always/never takes value, or whether an option is required, or its default value.

Getopt::Auto offers another way of supplying the spec: via import argument. For example:

use Getopt::Auto ([
    ['--add', 'Add integer to count', "The integer argument is not checked.\n", \&add],
    ['-a', 'Add integer to count', "The integer argument is not checked.\n", \&add],
    ['--inc', 'Bump count by 1', undef, undef],
]);

This way, you can supply option subroutine in a global-clean way. But for some reason, when it accepts spec this way, you must specify each alias separately. You can’t say “-a, –add” like you can in POD. This should probably be rectified in the future.

Another peculiarity is that option subroutine must accept argument in $ARGV
instead of $_. Another non-global-clean way.

Verdict: Getopt::Auto had some potential but is still plagued with enough eccentricities that prevent me from recommending it for general use.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s