Getopt modules, such as Getopt::Long, are used for processing command line options. There are over sixty Getopt modules on CPAN, which can be intimidating to select from. This talk highlights some of the Getopt pearls that have been released in the past few years.
Presented at YAPC::NA 2011, June 28, Asheville, NC.
2. "Getting command line options and
dealing with them is a colossal pain,
and every module that does it sucks
and offends someone."
— Ricardo Signes (rjbs)
17. $ munge --in refuse.log --out allure.json
my $opt = Getopt::Lucid->getopt({
Param('in')->required,
Param('out')->required,
});
open my $in_fh, '<', $opt->get_in;
open my $out_fh, '>', $opt->get_out;
20. $ frobnicate --calibrate
$ frobnicate -c
my $opt = Getopt::Lucid->getopt({
Switch('calibrate|c'),
});
print "Calibrating the frobnicator!n"
if $opt->get_calibrate;
21. $ frobnicate --calibrate
$ frobnicate -c
use 5.010;
my $opt = Getopt::Lucid->getopt({
Switch('calibrate|c'),
});
say 'Calibrating the frobnicator!'
if $opt->get_calibrate;
22. $ frobnicate --calibrate
$ frobnicate -c
use 5.010;
my $opt = Getopt::Lucid->getopt({
Switch('calibrate|c'),
});
say 'Calibrating the frobnicator!'
if $opt->get_calibrate;
$opt->set_calibrate(0);
30. $ munge --in refuse.log --out allure.json
package File::Munge;
use Moose;
with 'MooseX::Getopt';
31. $ munge --in refuse.log --out allure.json
package File::Munge;
use Moose;
with 'MooseX::Getopt';
has in => (is => 'rw', isa => 'Str');
has out => (is => 'rw', isa => 'Str');
32. $ munge --in refuse.log --out allure.json
package File::Munge;
use Moose;
with 'MooseX::Getopt';
has in => (is => 'rw', isa => 'Str', required => 1);
has out => (is => 'rw', isa => 'Str', required => 1);
33. $ munge --in refuse.log --out allure.json
package File::Munge;
use Moose;
with 'MooseX::Getopt';
has in => (is => 'rw', isa => 'Str', required => 1);
has out => (is => 'rw', isa => 'Str', required => 1);
sub munge {
my $self = shift;
open my $in_fh, '<', $self->in;
open my $out_fh, '>', $self->out;
}
36. $ frobnicate --calibrate
$ frobnicate -c
has calibrate => (
is => 'rw',
isa => 'Bool',
traits => ['Getopt'],
cmd_aliases => 'c',
);
37. $ frobnicate --calibrate
$ frobnicate -c
has calibrate => (
is => 'rw',
isa => 'Bool',
traits => ['Getopt'],
cmd_aliases => 'c',
);
sub frob {
my $self = shift;
say 'Calibrating the frobnicator!'
if $self->calibrate;
$self->calibrate(0);
}
44. my suggestions
Getopt::Lucid or
Getopt::Long::Descriptive
for one-off scripts
45. my suggestions
Getopt::Lucid or
Getopt::Long::Descriptive
for one-off scripts
MooseX::Getopt or MouseX::Getopt
for OO projects
46. my suggestions
Getopt::Lucid or
Getopt::Long::Descriptive
for one-off scripts
MooseX::Getopt or MouseX::Getopt
for OO projects
App::Cmd, MooseX::App::Cmd
or MouseX::App::Cmd
for command-driven scripts