SlideShare a Scribd company logo
1 of 9
Perl Brown Bag


             Command
                Line
             Arguments

              Shaun Griffith
               May 8, 2006

05/13/12        ATI Confidential   1
Agenda

TIMTOWTDI
•Argument Types
•DIY (Do It Yourself)
•-s Builtin
•Getopt::Long
•Getopt::Declare



05/13/12                 2
Argument Types
What kinds of arguments to expect?
     -xyz            toggle switch
     -xyz 17 value
     -xyz=17 value (parsing)
     -wx             combined (-w –x)
     -x 17 24 list
     --x             turn off
     --long          double dash
     value           bare value
     --              end of arguments
Where are the arguments?
  @ARGV


05/13/12                                3
Criteria for Solution?
What do we want a solution to
accomplish?
     • Easy to use
     • Argument validation
        • integers
        • file names
     • Self-documenting
     • Usage statement (help)
     • Flexible
     • Powerful


05/13/12                            4
Do It Yourself
Handle arguments manually
     while (@ARGV)
     { my $argv = shift(@ARGV);
       if ($argv =~ /^-x$/){$x=1;next}
       if ($argv =~ /^-y$/)
       { $y = shift(@ARGV)
              or die;
         next; }
       if ($argv =~ /^--$/) { last } # stop
       push @files, $argv;
     }
     @ARGV = (@files, @ARGV);
     while (<>)
     { do something here }
Pros: full control
Cons: tedious, check arguments manually
       documentation?
       extensibility?
05/13/12                                      5
-s Option
Perl has a builtin option for basic
argument handling: -s
In the script, use it on the shebang line:
#!/usr/local/bin/perl –s

…enabling this behavior:
 Option            Variables
 -xyz              $xyz = 1
 -xyz=blue         $xyz = “blue”


  Pros: easy
  Cons: documentation?
        shebang
        combined options?
        check arguments manually
05/13/12                                     6
Getopt::Long
use Getopt::Long;
my $data = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions
(
   "length=i" => $length, # numeric
   "file=s"       => $data,   # string
   "verbose" => $verbose # flag
);
Pros: argument checking
Cons: 24 pages of docs for G:L
      script documentation?
      wordy



05/13/12                                  7
Getopt::Declare
use Getopt::Declare;
my $spec = qq{
   [nocase] # case insenstive options
   -in <in:if>          input file
   -out <out:of>        output file
   [mutex –in –out]
   -off <offset:i>      offset (int)
   -b[ells+]w[histles] -bw also works
   { $::BELLS = 1; } }
my $options = Getopt::Declare->new($spec);
Pros: Functionality!
      Spec is usage
      Argument checking
      Full Parser!
Cons: Functionality!
      32 pages of docs for G:D!
      Subtle gotchas (like [])
      Object oriented (more work?)
05/13/12                                     8
Next Time?

Suggestions?




05/13/12                    9

More Related Content

What's hot

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for DummiesElizabeth Smith
 
Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandirpycon
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in GoAmr Hassan
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go ProgrammingLin Yo-An
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and coPierre Joye
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09Elizabeth Smith
 
F# Type Provider for R Statistical Platform
F# Type Provider for R Statistical PlatformF# Type Provider for R Statistical Platform
F# Type Provider for R Statistical PlatformHoward Mansell
 
Introduction to the rust programming language
Introduction to the rust programming languageIntroduction to the rust programming language
Introduction to the rust programming languageNikolay Denev
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPatrick Allaert
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]LivePerson
 
Vim Script Programming
Vim Script ProgrammingVim Script Programming
Vim Script ProgrammingLin Yo-An
 
Parse, scale to millions
Parse, scale to millionsParse, scale to millions
Parse, scale to millionsFlorent Vilmart
 
Value protocols and codables
Value protocols and codablesValue protocols and codables
Value protocols and codablesFlorent Vilmart
 
Github.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-phpGithub.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-phpSan jay
 

What's hot (20)

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 
F# Type Provider for R Statistical Platform
F# Type Provider for R Statistical PlatformF# Type Provider for R Statistical Platform
F# Type Provider for R Statistical Platform
 
Introduction to the rust programming language
Introduction to the rust programming languageIntroduction to the rust programming language
Introduction to the rust programming language
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]
 
Vim Script Programming
Vim Script ProgrammingVim Script Programming
Vim Script Programming
 
Parse, scale to millions
Parse, scale to millionsParse, scale to millions
Parse, scale to millions
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Value protocols and codables
Value protocols and codablesValue protocols and codables
Value protocols and codables
 
Github.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-phpGithub.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-php
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 

Similar to Perl Intro 9 Command Line Arguments

Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingShaun Griffith
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongIan Kluft
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 SubroutinesShaun Griffith
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovyIsuru Samaraweera
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptxNiladriDey18
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationWorkhorse Computing
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshopAdam Davis
 
Reproducible Computational Research in R
Reproducible Computational Research in RReproducible Computational Research in R
Reproducible Computational Research in RSamuel Bosch
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshopadam1davis
 
DPC 2012 : PHP in the Dark Workshop
DPC 2012 : PHP in the Dark WorkshopDPC 2012 : PHP in the Dark Workshop
DPC 2012 : PHP in the Dark WorkshopJeroen Keppens
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scriptsMichael Boelen
 

Similar to Perl Intro 9 Command Line Arguments (20)

Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::Long
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshop
 
Reproducible Computational Research in R
Reproducible Computational Research in RReproducible Computational Research in R
Reproducible Computational Research in R
 
Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshop
 
DPC 2012 : PHP in the Dark Workshop
DPC 2012 : PHP in the Dark WorkshopDPC 2012 : PHP in the Dark Workshop
DPC 2012 : PHP in the Dark Workshop
 
PigHive.pptx
PigHive.pptxPigHive.pptx
PigHive.pptx
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scripts
 

Perl Intro 9 Command Line Arguments

  • 1. Perl Brown Bag Command Line Arguments Shaun Griffith May 8, 2006 05/13/12 ATI Confidential 1
  • 2. Agenda TIMTOWTDI •Argument Types •DIY (Do It Yourself) •-s Builtin •Getopt::Long •Getopt::Declare 05/13/12 2
  • 3. Argument Types What kinds of arguments to expect? -xyz toggle switch -xyz 17 value -xyz=17 value (parsing) -wx combined (-w –x) -x 17 24 list --x turn off --long double dash value bare value -- end of arguments Where are the arguments? @ARGV 05/13/12 3
  • 4. Criteria for Solution? What do we want a solution to accomplish? • Easy to use • Argument validation • integers • file names • Self-documenting • Usage statement (help) • Flexible • Powerful 05/13/12 4
  • 5. Do It Yourself Handle arguments manually while (@ARGV) { my $argv = shift(@ARGV); if ($argv =~ /^-x$/){$x=1;next} if ($argv =~ /^-y$/) { $y = shift(@ARGV) or die; next; } if ($argv =~ /^--$/) { last } # stop push @files, $argv; } @ARGV = (@files, @ARGV); while (<>) { do something here } Pros: full control Cons: tedious, check arguments manually documentation? extensibility? 05/13/12 5
  • 6. -s Option Perl has a builtin option for basic argument handling: -s In the script, use it on the shebang line: #!/usr/local/bin/perl –s …enabling this behavior: Option Variables -xyz $xyz = 1 -xyz=blue $xyz = “blue” Pros: easy Cons: documentation? shebang combined options? check arguments manually 05/13/12 6
  • 7. Getopt::Long use Getopt::Long; my $data = "file.dat"; my $length = 24; my $verbose; $result = GetOptions ( "length=i" => $length, # numeric "file=s" => $data, # string "verbose" => $verbose # flag ); Pros: argument checking Cons: 24 pages of docs for G:L script documentation? wordy 05/13/12 7
  • 8. Getopt::Declare use Getopt::Declare; my $spec = qq{ [nocase] # case insenstive options -in <in:if> input file -out <out:of> output file [mutex –in –out] -off <offset:i> offset (int) -b[ells+]w[histles] -bw also works { $::BELLS = 1; } } my $options = Getopt::Declare->new($spec); Pros: Functionality! Spec is usage Argument checking Full Parser! Cons: Functionality! 32 pages of docs for G:D! Subtle gotchas (like []) Object oriented (more work?) 05/13/12 8