Perl
  Home arrow Perl arrow Page 2 - Using Getopt::Long: More Command Line Options in Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Using Getopt::Long: More Command Line Options in Perl
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-05-19


    Table of Contents:
  • Using Getopt::Long: More Command Line Options in Perl
  • Handling values
  • Optional values and multiple values
  • Responding to command line arguments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Getopt::Long: More Command Line Options in Perl - Handling values
    ( Page 2 of 4 )

     

    What if we need for a command line option to take a value, though? This behavior is, of course, supported by the module, but we need to specify that a command line option can take a value. We also need to specify what kind of value the option can take. 

    Suppose we want to add a command line option to our program called “server.” This option will take a value, the address of some server. This value will, of course, be a string. In order to specify all of this, we simply need to add a little bit to the option's specifications (now you know why they are specifications, not names). All we need to add is an equals sign followed by the letter s, which indicates that we're looking for a string:

     

    GetOptions('force' =>  $force, 'server=s' =>  $server);

     

    Before this, though, we need to declare the variable and give it a default value:

     

    my $server = '';

     

    You should also adjust the program so that it prints out the value of the option. 

    Now the program can be called with the new command line option:

     

    $ program --server=devshed.com

     

    What happens if we need spaces in the string, though? In that case, we would simply surround the string with quotation marks. For example, consider a command line option named “name” that takes the name of a person:

     

    $ program --name=”John Smith”

     

    We can specify other types of values, too: integers, floating point numbers and “extended integers.” This last type simply corresponds to integers as might be used in Perl. That is, they can be specified in hexadecimal, binary, or whatever. In order to specify that an option takes an integer value, use i, and in order to specify that an option takes a floating point value, use f. Extended integers may be specified by using the letter o. 

    Here are three more command line options. The first takes an integer, the second takes a floating point number, and the third takes an extended integer:

     

    my $retries = 0;my $somefloat = 0;my $einteger = 0;...GetOptions('force' =>  $force, 'server=s' =>  $server, 'retries=i' =>  $retries, 'somefloat=f' => 
     $somefloat, 
    'einteger=o'
     =>  $einteger);

     

    Now we can call the program like this:

     

    $ program --retries=3 –somefloat=3.14 --einteger=0xFF

     

    Say we try to pass the wrong kind of value to an option, though, like this:

     

    $ program --retires=enough

     

    We're passing a string where the program expects a float. If you run this, though, you'll notice that Getopt::Long complains:

     

    Value "hi" invalid for option somefloat (real number expected)

     

    In this case, the value is not assigned to the variable. 



     
     
    >>> More Perl Articles          >>> More By Peyton McCullough
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek