Seventh has asked for the wisdom of the Perl Monks concerning the following question:

Hello esteemed monks. :)

I have a script that I use Getopt with to call certain loops. It looks like so:

use Getopt::Long; use strict; use Data::Dumper; use IO::File; my ($read,$generate,$help); # option switches GetOptions ( "help" => \$help, "read" => \$read, "generate" => \$generate, );


Each one just points to a script that I put in an if loop, like so:
if ($read) { - code snippets - }

I added a few extra switches for single characters, like so:
my ($read,$generate,$help); # option switches GetOptions ( "h" => \$help, "help" => \$help, "r" => \$read, "read" => \$read, "g" => \$generate, "generate" => \$generate, );

What I'm curious to know is if there's a better way to do this. Right now running 'scriptname -read' or 'scriptname -r' do what I'd like them to do, but, for example, while 'scriptname -generate' does the same as 'scriptname -g', 'scriptname -gen' fires me back an unknown option error.

I'm assuming that I need to somehow use (chomp, perhaps?) on the input to just snip whatever the -option is down to the first letter, but I'm not familiar enough with Getopt to know how to accomplish this.

Any advice is as always really appreciated, and thanks again to everyone on here for all the help you've given me so far. :)

Replies are listed 'Best First'.
Re: Need help using Getopt, please.
by trammell (Priest) on Jan 21, 2005 at 16:32 UTC
    One thing you can do is supply alternate names for options, e.g.:
    GetOptions ( "help|h" => \$help, "read|r" => \$read, "generate|g" => \$generate, );
      That did it. Thanks very much!
Re: Need help using Getopt, please.
by davido (Cardinal) on Jan 21, 2005 at 16:40 UTC

    You don't need to enumerate abbreviations. Getopt::Long allows the user to specify options using the smallest unique abbreviation. For example... if you have an option called 'help', the user can specify '--h' on the commandline and it will automatically work. If you have an option 'help' and 'hop', the smallest unique abbreviations would be '--he' and '--ho'.

    This also works for '--no*' options. For example, if you have a switch called 'try', and you set up the capability for it to be negated with 'notry', you can also use the abbreviations '--t' and '--not'.

    Again, this is all automatic by default in Getopt::Long. It can be disabled, tweaked, etc., but by default, you get this behavior. So the ramification is that it's unnecessary to specify 'help|h' as an option, because if your option is specified as 'help', the user can enter 'help', 'hel', 'he', and 'h', as long as each of those abberviations is unique.


    Dave

      I think I'm going senile, or I must have fat-fingered something, because I'd wager a coffee that it wasn't working before, but it certainly is working now.

      Thanks very much to all of you, I really appreciate you taking the time to help my newbie self out. :)
Re: Need help using Getopt, please.
by bgreenlee (Friar) on Jan 21, 2005 at 16:41 UTC
    Getopt::Long will let you abbreviate to uniqueness automatically, so you just define the "generate" option, for example, and -generate, -gen, and -g will all work (with two dashes as wel).

    -b