KEMBAR78
Command Line Arguments with Getopt::Long | PDF
Command Line Arguments
with Getopt::Long
By Ian Kluft
Part of short Lightning Talks session
Silicon Valley Perl
November 12, 2015
Santa Clara, California
Command-Line Arguments
● Many programs use command line interface
– Software builds
– Automated tools
– Experimental calculations
– Anything there isn't time to make a GUI
● Don't write new code to process CLI arguments
– Standards compliance comes with Getopt::Long
– It's in CPAN
Getopt::Long usage (1 of 2)
From the manpage:
use Getopt::Long;
      my $data   = "file.dat";
      my $length = 24;
      my $verbose;
      GetOptions ("length=i" => $length,    # numeric
                  "file=s"   => $data,      # string
                  "verbose"  => $verbose)   # flag
      or die("Error in command line argumentsn");
● Note these things:
● Getopt::Long and its function GetOptions
● Initialization of variables before using them
● Specification and destination variable pairs
Getopt::Long usage (2 of 2)
● GetOptions processes the command line
● It removes the Unix-standard CLI options
– Like --optname, --optname=value, etc
– Single dash prefixes single-character options
– These can be in any order
● It leaves everything else in @ARGV list
– Such as filenames or text
– Whatever you use them for
Option specifications (1 of 2)
● GetOptions takes specification/destination pairs
– Specification: name/format of argument
– Destination: reference to variable to store data
● Boolean
– GetOptions ( “flag” => $flag );
– Stores true (1) if --flag is present
– With “!flag”, it stores false if --noflag is present
– $flag is not modified if --flag is not present
●
So don't forget to initialize it!
Option specifications (2 of 2)
● GetOptions ( “stringarg=s” => $stringarg );
– Use “=s” for mandatory string argument
– Use “:s” for optional string argument
● GetOptions ( “integerarg=i” => $integerarg );
– Use “=i” for mandatory integer argument
– Use “:i” for optional integer argument
● GetOptions ( “floatarg=f” => $floatarg );
– Use “=f” for mandatory floating point argument
– Use “:f” for optional floating point argument
Lists from arguments
● GetOptions ("library=s" => @libfiles);
– Multiple occurrences of --library go in @libfiles
● GetOptions ("library=s@" => $libfiles);
– Same thing, but $libfiles is a reference to an array
● Example: use the argument more than once
– prog --library lib/stdlib --library lib/extlib
Hashes from arguments
● GetOptions ("define=s" => %defines);
– key=value pair is placed in %defines hash
● GetOptions ("define=s%" => $defines);
– Same thing, except $defines is a ref to a hash
● Example:
– prog --define os=linux --define vendor=redhat
Similar CPAN modules
● Getopt::LL – wider set of prcoessing rules
● Getopt::XML – read XML in Getopt::Long style
● MooseX::Getopt – Getopt for Moose
● Many other smaller modules
– Search CPAN for “Getopt”

Command Line Arguments with Getopt::Long

  • 1.
    Command Line Arguments withGetopt::Long By Ian Kluft Part of short Lightning Talks session Silicon Valley Perl November 12, 2015 Santa Clara, California
  • 2.
    Command-Line Arguments ● Manyprograms use command line interface – Software builds – Automated tools – Experimental calculations – Anything there isn't time to make a GUI ● Don't write new code to process CLI arguments – Standards compliance comes with Getopt::Long – It's in CPAN
  • 3.
    Getopt::Long usage (1of 2) From the manpage: use Getopt::Long;       my $data   = "file.dat";       my $length = 24;       my $verbose;       GetOptions ("length=i" => $length,    # numeric                   "file=s"   => $data,      # string                   "verbose"  => $verbose)   # flag       or die("Error in command line argumentsn"); ● Note these things: ● Getopt::Long and its function GetOptions ● Initialization of variables before using them ● Specification and destination variable pairs
  • 4.
    Getopt::Long usage (2of 2) ● GetOptions processes the command line ● It removes the Unix-standard CLI options – Like --optname, --optname=value, etc – Single dash prefixes single-character options – These can be in any order ● It leaves everything else in @ARGV list – Such as filenames or text – Whatever you use them for
  • 5.
    Option specifications (1of 2) ● GetOptions takes specification/destination pairs – Specification: name/format of argument – Destination: reference to variable to store data ● Boolean – GetOptions ( “flag” => $flag ); – Stores true (1) if --flag is present – With “!flag”, it stores false if --noflag is present – $flag is not modified if --flag is not present ● So don't forget to initialize it!
  • 6.
    Option specifications (2of 2) ● GetOptions ( “stringarg=s” => $stringarg ); – Use “=s” for mandatory string argument – Use “:s” for optional string argument ● GetOptions ( “integerarg=i” => $integerarg ); – Use “=i” for mandatory integer argument – Use “:i” for optional integer argument ● GetOptions ( “floatarg=f” => $floatarg ); – Use “=f” for mandatory floating point argument – Use “:f” for optional floating point argument
  • 7.
    Lists from arguments ●GetOptions ("library=s" => @libfiles); – Multiple occurrences of --library go in @libfiles ● GetOptions ("library=s@" => $libfiles); – Same thing, but $libfiles is a reference to an array ● Example: use the argument more than once – prog --library lib/stdlib --library lib/extlib
  • 8.
    Hashes from arguments ●GetOptions ("define=s" => %defines); – key=value pair is placed in %defines hash ● GetOptions ("define=s%" => $defines); – Same thing, except $defines is a ref to a hash ● Example: – prog --define os=linux --define vendor=redhat
  • 9.
    Similar CPAN modules ●Getopt::LL – wider set of prcoessing rules ● Getopt::XML – read XML in Getopt::Long style ● MooseX::Getopt – Getopt for Moose ● Many other smaller modules – Search CPAN for “Getopt”