Slide 1

Slide 1 text

Modern Getopt for Command Line Processing Nick Patch YAPC::NA 28 June 2011

Slide 2

Slide 2 text

"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)

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

that's 63 Getopt modules

Slide 5

Slide 5 text

Getopt::Easy or Getopt::Simple?

Slide 6

Slide 6 text

Getopt::Tiny or Getopt::Compact?

Slide 7

Slide 7 text

maybe Getopt::Awesome?

Slide 8

Slide 8 text

Getopt::WonderBra?!

Slide 9

Slide 9 text

Getopt::WonderBra?! "Lift and Separate Command Line Options"

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Getopt::Lucid

Slide 14

Slide 14 text

$ munge --in refuse.log --out allure.json

Slide 15

Slide 15 text

$ munge --in refuse.log --out allure.json my $opt = Getopt::Lucid->getopt({ Param('in'), Param('out'), });

Slide 16

Slide 16 text

$ munge --in refuse.log --out allure.json my $opt = Getopt::Lucid->getopt({ Param('in')->required, Param('out')->required, });

Slide 17

Slide 17 text

$ 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;

Slide 18

Slide 18 text

$ frobnicate --calibrate $ frobnicate -c

Slide 19

Slide 19 text

$ frobnicate --calibrate $ frobnicate -c my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'), });

Slide 20

Slide 20 text

$ frobnicate --calibrate $ frobnicate -c my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'), }); print "Calibrating the frobnicator!\n" if $opt->get_calibrate;

Slide 21

Slide 21 text

$ frobnicate --calibrate $ frobnicate -c use 5.010; my $opt = Getopt::Lucid->getopt({ Switch('calibrate|c'), }); say 'Calibrating the frobnicator!' if $opt->get_calibrate;

Slide 22

Slide 22 text

$ 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);

Slide 23

Slide 23 text

$ perlping -v -s 512 4.2.2.2

Slide 24

Slide 24 text

$ perlping -v -s 512 4.2.2.2 my $opt = Getopt::Lucid->getopt({ Param('size|s'), Param('ttl|t'), Switch('verbose|v'), });

Slide 25

Slide 25 text

$ 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'), });

Slide 26

Slide 26 text

$ 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;

Slide 27

Slide 27 text

MooseX::Getopt

Slide 28

Slide 28 text

MooseX::Getopt or MouseX::Getopt

Slide 29

Slide 29 text

$ munge --in refuse.log --out allure.json

Slide 30

Slide 30 text

$ munge --in refuse.log --out allure.json package File::Munge; use Moose; with 'MooseX::Getopt';

Slide 31

Slide 31 text

$ 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');

Slide 32

Slide 32 text

$ 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);

Slide 33

Slide 33 text

$ 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; }

Slide 34

Slide 34 text

#!/usr/bin/perl use File::Munge; my $munger = File::Munge->new_with_options; $munger->munge;

Slide 35

Slide 35 text

$ frobnicate --calibrate $ frobnicate -c

Slide 36

Slide 36 text

$ frobnicate --calibrate $ frobnicate -c has calibrate => ( is => 'rw', isa => 'Bool', traits => ['Getopt'], cmd_aliases => 'c', );

Slide 37

Slide 37 text

$ 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); }

Slide 38

Slide 38 text

$ perlping -v -s 512 4.2.2.2

Slide 39

Slide 39 text

$ 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');

Slide 40

Slide 40 text

$ 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');

Slide 41

Slide 41 text

$ 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 }; } );

Slide 42

Slide 42 text

$ 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; } );

Slide 43

Slide 43 text

my suggestions

Slide 44

Slide 44 text

my suggestions Getopt::Lucid or Getopt::Long::Descriptive for one-off scripts

Slide 45

Slide 45 text

my suggestions Getopt::Lucid or Getopt::Long::Descriptive for one-off scripts MooseX::Getopt or MouseX::Getopt for OO projects

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

questions?

Slide 48

Slide 48 text

slides at: nickpatch.net