How to Parse Options in Perl Using Getopt

(0 votes, average 0 out of 5)

Being able to parse perl options in a user-friendly manner is not immediately obvious task but it is quite easy thanks to the built-in Perl library Getopt.  Getopt is a very robust function with lots of options -- see the official documentation for an exhaustive manual.  This article will serve as a crash-course for quickly using Getopt to obtain both boolean and value-oriented options.  

Let's begin by supposing we want to parse the following options in a script like so:

my_script_name.pl -foo -bar "Hello world!"
my_script_name.pl -f -b "Hello world!"

First, include the library and declare the variables where your option values will reside (use of strict is assumed here).   

1
2
3
4
use Getopt::Long;
 
my $foo;
my $bar;

In this example, foo is a switch (we are only interested if the switch exists or not), and bar is an option which includes some value.  Now we will call GetOptions, which will actually parse the command line and retrieve these values:

5
6
7
die unless GetOptions ( "f|foo"      => \$foo,
                        "b|bar=s"    => \$bar,
                         );

The letter before the | symbol indicates the shorthand name for the option, the word following it is the full switch name.  The "=s" addition on the bar indicates that this option takes a string as a parameter.  Now, our variables hold the values!  Below is an example where we print the message passed in using bar, and then print an additional message if the foo switch is passed in.

8
9
10
11
print $bar."\n";
if($foo){    
    print "Foo switch received!\n";
}

You can easily check if an option exists or not by checking if it is defined.  For instance, if bar was a necessary option, we can do the following:

if(!defined($bar)){die "Please provide a string using -bar \n";}

Now let's put it all together!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use strict;
use Getopt::Long;
 
my $foo;
my $bar;
 
die unless GetOptions ( "f|foo"      => \$foo,
                        "b|bar=s"    => \$bar,
                         );
 
if(!defined($bar)){die "Please provide a string using -bar \n";}
 
print $bar."\n";
if($foo){
    print "Foo switch received!\n";
}

 


 

Partner Links:
Last Updated on Monday, 13 February 2012 15:49  
Related Articles

» How to Determine Which Programs are Using Which Ports

Sometimes a program is using a particular port, preventing another program from capturing and using it. It can be difficult to trace down which process exactly is using the port. Fortunately, there are a couple helpful utilities which can link each process with the port(s) it is using.WindowsOn Windows, download and run . It will display a table of the currently running processes and the ports they are using. It is possible to sort by process name or the port number to make it easy to find the...

» Mac Fix: No Audio (Grey Icon)

When the audio icon in the upper right-hand of your Mac laptop screen turns grey and you can’t get the audio to work by pressing any keys, chances are your computer's audio is in "optical out" mode. To test this, plug in a pair of headphones into the audio output port. If the icon goes to black and the volume works, then the optical output switch is definitely the problem.First, try removing the headphones or other audio device slowly. There is a chance that this will fix the problem. If that...

» How to Show Filename in Title Bar in Emacs

Adding the following snippet into your .emacs file (typically located in your home directory) will cause the filename of the current buffer to appear in the title bar.  This is a quick fix that can make managing multiple emacs windows much easier. ;; Show filename in title bar(setq frame-title-format "%b - Emacs")