Modern Getopt for Command Line Processing in Perl
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Modern Getopt for Command Line Processing in Perl

on

  • 2,067 views

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 ...

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.

Statistics

Views

Total Views
2,067
Views on SlideShare
2,055
Embed Views
12

Actions

Likes
3
Downloads
18
Comments
0

1 Embed 12

http://lanyrd.com 12

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Modern Getopt for Command Line Processing in Perl Presentation Transcript

  • 1. Modern Getopt forCommand Line Processing Nick Patch YAPC::NA 28 June 2011
  • 2. "Getting command line options anddealing with them is a colossal pain, and every module that does it sucks and offends someone." — Ricardo Signes (rjbs)
  • 3. 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::TreeGetopt::Usaginator Getopt::Whatever Getopt::WonderBra Getopt::XML Getopt::Yagow Getopt_Auto CBSSports::Getopt CGI::GetoptMooseX::Getopt MooseX::Getopt::Defanged MouseX::Getopt Tk::Getopt
  • 4. thats 63 Getopt modules
  • 5. Getopt::Easy or Getopt::Simple ?
  • 6. Getopt::Tiny or Getopt::Compact ?
  • 7. maybe Getopt::Awesome ?
  • 8. Getopt::WonderBra ?!
  • 9. Getopt::WonderBra ?!"Lift and Separate Command Line Options"
  • 10. dependents454 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
  • 11. minus same authors446 Getopt::Long 62 MooseX::Getopt 19 Getopt::Long::Descriptive 17 Getopt::ArgvFile 7 Getopt::Euclid 7 Getopt::Lucid 5 Getopt::Mixed
  • 12. plus first release date446 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
  • 13. Getopt::Lucid
  • 14. $ munge --in refuse.log --out allure.json
  • 15. $ munge --in refuse.log --out allure.jsonmy $opt = Getopt::Lucid->getopt({ Param(in), Param(out),});
  • 16. $ munge --in refuse.log --out allure.jsonmy $opt = Getopt::Lucid->getopt({ Param(in)->required, Param(out)->required,});
  • 17. $ munge --in refuse.log --out allure.jsonmy $opt = Getopt::Lucid->getopt({ Param(in)->required, Param(out)->required,});open my $in_fh, <, $opt->get_in;open my $out_fh, >, $opt->get_out;
  • 18. $ frobnicate --calibrate$ frobnicate -c
  • 19. $ frobnicate --calibrate$ frobnicate -cmy $opt = Getopt::Lucid->getopt({ Switch(calibrate|c),});
  • 20. $ frobnicate --calibrate$ frobnicate -cmy $opt = Getopt::Lucid->getopt({ Switch(calibrate|c),});print "Calibrating the frobnicator!n" if $opt->get_calibrate;
  • 21. $ frobnicate --calibrate$ frobnicate -cuse 5.010;my $opt = Getopt::Lucid->getopt({ Switch(calibrate|c),});say Calibrating the frobnicator! if $opt->get_calibrate;
  • 22. $ frobnicate --calibrate$ frobnicate -cuse 5.010;my $opt = Getopt::Lucid->getopt({ Switch(calibrate|c),});say Calibrating the frobnicator! if $opt->get_calibrate;$opt->set_calibrate(0);
  • 23. $ perlping -v -s 512 4.2.2.2
  • 24. $ perlping -v -s 512 4.2.2.2my $opt = Getopt::Lucid->getopt({ Param(size|s), Param(ttl|t), Switch(verbose|v),});
  • 25. $ perlping -v -s 512 4.2.2.2my $opt = Getopt::Lucid->getopt({ Param(size|s)->default(64), Param(ttl|t)->default(50), Switch(verbose|v),});
  • 26. $ perlping -v -s 512 4.2.2.2my $opt = Getopt::Lucid->getopt({ Param(size|s)->default(64), Param(ttl|t)->default(50), Switch(verbose|v),});my $destination = shift @ARGV;
  • 27. MooseX::Getopt
  • 28. MooseX::Getopt orMouseX::Getopt
  • 29. $ munge --in refuse.log --out allure.json
  • 30. $ munge --in refuse.log --out allure.jsonpackage File::Munge;use Moose;with MooseX::Getopt;
  • 31. $ munge --in refuse.log --out allure.jsonpackage 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.jsonpackage 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.jsonpackage 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;}
  • 34. #!/usr/bin/perluse File::Munge;my $munger = File::Munge->new_with_options;$munger->munge;
  • 35. $ frobnicate --calibrate$ frobnicate -c
  • 36. $ frobnicate --calibrate$ frobnicate -chas calibrate => ( is => rw, isa => Bool, traits => [Getopt], cmd_aliases => c,);
  • 37. $ frobnicate --calibrate$ frobnicate -chas 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);}
  • 38. $ perlping -v -s 512 4.2.2.2
  • 39. $ perlping -v -s 512 4.2.2.2has size => (is => rw, isa => Int);has ttl => (is => rw, isa => Int);has verbose => (is => rw, isa => Bool);
  • 40. $ perlping -v -s 512 4.2.2.2has size => (is => rw, isa => Int, default => 64);has ttl => (is => rw, isa => Int, default => 50);has verbose => (is => rw, isa => Bool);
  • 41. $ perlping -v -s 512 4.2.2.2has 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 }; });
  • 42. $ perlping -v -s 512 4.2.2.2use 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; });
  • 43. my suggestions
  • 44. my suggestions Getopt::Lucid orGetopt::Long::Descriptive for one-off scripts
  • 45. my suggestions Getopt::Lucid or Getopt::Long::Descriptive for one-off scriptsMooseX::Getopt or MouseX::Getopt for OO projects
  • 46. my suggestions Getopt::Lucid or Getopt::Long::Descriptive for one-off scriptsMooseX::Getopt or MouseX::Getopt for OO projects App::Cmd, MooseX::App::Cmd or MouseX::App::Cmd for command-driven scripts
  • 47. questions?
  • 48. slides at:nickpatch.net