modern getopt for command line processing in perl
DESCRIPTION
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.TRANSCRIPT
![Page 1: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/1.jpg)
Modern Getopt forCommand Line Processing
Nick Patch
YAPC::NA
28 June 2011
![Page 2: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/2.jpg)
"Getting command line options anddealing with them is a colossal pain,and every module that does it sucks
and offends someone."
— Ricardo Signes (rjbs)
![Page 3: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/3.jpg)
Getopt::Abridged Getopt::ArgvFile Getopt::AsDocumented Getopt::Attribute Getopt::AutoConf Getopt::Awesome Getopt::Base Getopt::CallingName Getopt::Casual Getopt::Chain Getopt::Clade
Getopt::Compact Getopt::Compact::WithCmd Getopt::Complete Getopt::constant Getopt::Declare Getopt::Easy Getopt::Euclid Getopt::EvaP Getopt::ExPar Getopt::Fancy Getopt::FileConfig
Getopt::Flex Getopt::Function Getopt::GetArgs Getopt::GUI::Long Getopt::Helpful Getopt::Inherited Getopt::Janus Getopt::Lazy
Getopt::LL Getopt::Long Getopt::Long::Descriptive Getopt::Long::DescriptivePod Getopt::Long::GUI Getopt::LongUsage Getopt::Lucid Getopt::Mixed Getopt::Mixed::Help Getopt::Modular
Getopt::OO Getopt::Param Getopt::Param::Tiny Getopt::Plus Getopt::Regex Getopt::Simple Getopt::Std::Strict
Getopt::Std::WithCheck Getopt::Tabular Getopt::Tiny Getopt::Tree Getopt::Usaginator Getopt::Whatever Getopt::WonderBra Getopt::XML
Getopt::Yagow Getopt_Auto CBSSports::Getopt CGI::Getopt MooseX::Getopt MooseX::Getopt::Defanged MouseX::Getopt Tk::Getopt
![Page 4: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/4.jpg)
that's 63 Getopt modules
![Page 5: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/5.jpg)
Getopt::Easy or Getopt::Simple?
![Page 6: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/6.jpg)
Getopt::Tiny or Getopt::Compact?
![Page 7: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/7.jpg)
maybe Getopt::Awesome?
![Page 8: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/8.jpg)
Getopt::WonderBra?!
![Page 9: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/9.jpg)
Getopt::WonderBra?!
"Lift and Separate Command Line Options"
![Page 10: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/10.jpg)
dependents
454 Getopt::Long 64 MooseX::Getopt 28 Getopt::Long::Descriptive 18 Getopt::ArgvFile 11 Getopt::Std::Strict 10 Getopt::Lucid 7 Getopt::Euclid 5 Getopt::Attribute 5 Getopt::Mixed 5 Getopt::Usaginator
![Page 11: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/11.jpg)
minus same authors
446 Getopt::Long 62 MooseX::Getopt 19 Getopt::Long::Descriptive 17 Getopt::ArgvFile 7 Getopt::Euclid 7 Getopt::Lucid 5 Getopt::Mixed
![Page 12: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/12.jpg)
plus first release date
446 Getopt::Long 1995 62 MooseX::Getopt 2007 19 Getopt::Long::Descriptive 2005 17 Getopt::ArgvFile 1999 7 Getopt::Euclid 2005 7 Getopt::Lucid 2005 5 Getopt::Mixed 1996
![Page 13: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/13.jpg)
Getopt::Lucid
![Page 14: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/14.jpg)
$ munge --in refuse.log --out allure.json
![Page 15: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/15.jpg)
$ munge --in refuse.log --out allure.json
my $opt = Getopt::Lucid->getopt({ Param('in'), Param('out'),});
![Page 16: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/16.jpg)
$ munge --in refuse.log --out allure.json
my $opt = Getopt::Lucid->getopt({ Param('in')->required, Param('out')->required,});
![Page 17: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/17.jpg)
$ 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;
![Page 18: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/18.jpg)
$ frobnicate --calibrate$ frobnicate -c
![Page 19: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/19.jpg)
$ frobnicate --calibrate$ frobnicate -c
my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'),});
![Page 20: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/20.jpg)
$ frobnicate --calibrate$ frobnicate -c
my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'),});
print "Calibrating the frobnicator!\n" if $opt->get_calibrate;
![Page 21: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/21.jpg)
$ frobnicate --calibrate$ frobnicate -c
use 5.010;
my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'),});
say 'Calibrating the frobnicator!' if $opt->get_calibrate;
![Page 22: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/22.jpg)
$ 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);
![Page 23: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/23.jpg)
$ perlping -v -s 512 4.2.2.2
![Page 24: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/24.jpg)
$ perlping -v -s 512 4.2.2.2
my $opt = Getopt::Lucid->getopt({ Param('size|s'), Param('ttl|t'), Switch('verbose|v'),});
![Page 25: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/25.jpg)
$ perlping -v -s 512 4.2.2.2
my $opt = Getopt::Lucid->getopt({ Param('size|s')->default(64), Param('ttl|t')->default(50), Switch('verbose|v'),});
![Page 26: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/26.jpg)
$ perlping -v -s 512 4.2.2.2
my $opt = Getopt::Lucid->getopt({ Param('size|s')->default(64), Param('ttl|t')->default(50), Switch('verbose|v'),});
my $destination = shift @ARGV;
![Page 27: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/27.jpg)
MooseX::Getopt
![Page 28: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/28.jpg)
MooseX::Getoptor
MouseX::Getopt
![Page 29: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/29.jpg)
$ munge --in refuse.log --out allure.json
![Page 30: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/30.jpg)
$ munge --in refuse.log --out allure.json
package File::Munge;use Moose;with 'MooseX::Getopt';
![Page 31: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/31.jpg)
$ 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');
![Page 32: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/32.jpg)
$ 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);
![Page 33: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/33.jpg)
$ 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;}
![Page 34: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/34.jpg)
#!/usr/bin/perluse File::Munge;
my $munger = File::Munge->new_with_options;
$munger->munge;
![Page 35: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/35.jpg)
$ frobnicate --calibrate$ frobnicate -c
![Page 36: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/36.jpg)
$ frobnicate --calibrate$ frobnicate -c
has calibrate => ( is => 'rw', isa => 'Bool', traits => ['Getopt'], cmd_aliases => 'c',);
![Page 37: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/37.jpg)
$ 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);}
![Page 38: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/38.jpg)
$ perlping -v -s 512 4.2.2.2
![Page 39: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/39.jpg)
$ perlping -v -s 512 4.2.2.2
has size => (is => 'rw', isa => 'Int');has ttl => (is => 'rw', isa => 'Int');has verbose => (is => 'rw', isa => 'Bool');
![Page 40: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/40.jpg)
$ perlping -v -s 512 4.2.2.2
has size => (is => 'rw', isa => 'Int', default => 64);has ttl => (is => 'rw', isa => 'Int', default => 50);has verbose => (is => 'rw', isa => 'Bool');
![Page 41: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/41.jpg)
$ perlping -v -s 512 4.2.2.2
has size => (is => 'rw', isa => 'Int', default => 64);has ttl => (is => 'rw', isa => 'Int', default => 50);has verbose => (is => 'rw', isa => 'Bool');
has _destination => ( is => 'rw', isa => 'Str', default => sub { my $self = shift; return shift @{ $self->extra_argv }; });
![Page 42: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/42.jpg)
$ perlping -v -s 512 4.2.2.2
use 5.014;
has size => (is => 'rw', isa => 'Int', default => 64);has ttl => (is => 'rw', isa => 'Int', default => 50);has verbose => (is => 'rw', isa => 'Bool');
has _destination => ( is => 'rw', isa => 'Str', default => sub { my $self = shift; return shift $self->extra_argv; });
![Page 43: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/43.jpg)
my suggestions
![Page 44: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/44.jpg)
my suggestions
Getopt::Lucid or Getopt::Long::Descriptive
for one-off scripts
![Page 45: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/45.jpg)
my suggestions
Getopt::Lucid or Getopt::Long::Descriptive
for one-off scripts
MooseX::Getopt or MouseX::Getoptfor OO projects
![Page 46: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/46.jpg)
my suggestions
Getopt::Lucid or Getopt::Long::Descriptive
for one-off scripts
MooseX::Getopt or MouseX::Getoptfor OO projects
App::Cmd, MooseX::App::Cmdor MouseX::App::Cmd
for command-driven scripts
![Page 47: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/47.jpg)
questions?
![Page 48: Modern Getopt for Command Line Processing in Perl](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce97d8b42aaf158b4c04/html5/thumbnails/48.jpg)
slides at:nickpatch.net