command line arguments with getopt::long

9
Command Line Arguments with Getopt::Long By Ian Kluft Part of short Lightning Talks session Silicon Valley Perl November 12, 2015 Santa Clara, California

Upload: ian-kluft

Post on 13-Apr-2017

511 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Command Line Arguments with Getopt::Long

Command Line Argumentswith Getopt::Long

By Ian Kluft

Part of short Lightning Talks sessionSilicon Valley Perl

November 12, 2015Santa Clara, California

Page 2: Command Line Arguments with Getopt::Long

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

Page 3: Command Line Arguments with Getopt::Long

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 arguments\n");

● Note these things:● Getopt::Long and its function GetOptions● Initialization of variables before using them● Specification and destination variable pairs

Page 4: Command Line Arguments with Getopt::Long

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

Page 5: Command Line Arguments with Getopt::Long

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!

Page 6: Command Line Arguments with Getopt::Long

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

Page 7: Command Line Arguments with Getopt::Long

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

Page 8: Command Line Arguments with Getopt::Long

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

Page 9: Command Line Arguments with Getopt::Long

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”