[tpm] Command line option processing

Tom Legrady tom at legrady.ca
Sat Jan 5 15:06:50 PST 2008


Sorry, Indy, you're being masochistic.

I can sort-of understand someone using GetOptions in the option-name  
=> variable-to-store-it-in style, though I agree with Uri, far better  
to pass in a hash into which all the options are stored. You're doing  
that, the hard way, passing in the hash keys one by one. Why not  
specify the whole hash, once?

GetOptions( \%opt,
                        "verbose|v",
                        "test",
                         "debug|d",
                         "takes_a_string=s",
                       );

You stored 'v' in $opt{verbose}; I've achieved the same thing by  
specifying 'verbose' and 'v' as aliases for the same thing, similarly  
'debug' and 'd'. Mind you, by default Getopt::Long ignores case and  
allows abbreviation of long option names, so long as the option is  
uniquely identified.  So you can specify

GetOptions( \%opt,
                        "verbose",
                        "test",
                         "debug",
                         "takes_a_string=s",
                       );

and then invoke the program with   -v -d ... and since there is only  
one option beginning with a V and only one beginning with a D, it  
knows which one you want.

Tom

On 5-Jan-08, at 5:31 PM, Indy Singh wrote:

> Here is one way to do it:
>
> #!/usr/bin/perl -w
> use strict;
> use Getopt::Long;
>
> my %opt;
>
> GetOptions(
>            "v" => \$opt{verbose},
>            "test" => \$opt{test},
>            "d" => \$opt{debug},
>            "debug" => \$opt{debug},
>            );
>
>
> if ($opt{verbose}) {
>  print "VERBOSE\n";
> }
> else {
>  print "quiet\n";
> }
>
>
>
>
> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
>
>
> ----- Original Message -----
> From: <arocker at vex.net>
> To: <tpm at to.pm.org>
> Sent: Saturday, January 05, 2008 4:53 PM
> Subject: [tpm] Command line option processing
>
>
>>
>> I'm trying to implement a simple check of a command line option (just
>> present or absent, no arguments). The shell equivalent, which works,
>> is:
>>
>> while getopts v opt
>> do
>>   case $opt
>>   in
>>   v) echo "Hi"
>>    ;;
>>   esac
>> done
>>
>> The perl, which doesn't:
>>
>> #! /usr/bin/perl
>> use warnings;
>> use Getopt::Std;
>>
>> getopt ("v");           # primitive help facility -v is only option
>>
>> if ( $Getopt::Std::opt_v ) {
>>   print Hi\n";
>> }
>>
>> and changing the test to if ( $opt ) doesn't do any better.
>>
>> I've delved into the Camel, the Cookbook, Nutshell and every other
>> grimoire I can find, so public humiliation is the only route left.
>> What
>> idiotic error am I making?
>>
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>

Tom Legrady
tom at legrady.ca
My photo gallery ... http://picasaweb.google.com/legrady
Photo flipbook ...http://www.youtube.com/watch?v=E2n2yV5DL2U

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/toronto-pm/attachments/20080105/3e37e392/attachment.html 


More information about the toronto-pm mailing list