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

I want to enforce that a script returns usage information if a required option is not passed.But when I call my script without any arguments, it still prints the SQL string instead of dieing with usage information.
use Getopt::Long; use Pod::Usage; #use SQL::Generator; GetOptions ( 'table=s' => \$table, 'id_field=s' => \$email_field, 'state_field=s' => \$state_field, 'zip_field=s' => \$zip_field, 'dob_field=s' => \$dob_field, 'help|?' => \$help, man => \$man ) or die pod2usage(2) ; pod2usage(1) if $help; pod2usage(-exitstatus => 0, -verbose => 2) if $man; print <<EOSQL SELECT $id_field, $email_field, $state_field, $zip_field, $dob_field FROM $table WHERE $zip_field like '336%' or $zip_field like '335%' or $zip_field like '346%' or $zip_field like '337%' or $zip_field like '342%' EOSQL
[tbone@MDB 202025-007]$ perl sql.pl -? Usage: sql.pl [options] sql.pl --table=tabla --id_field=GID --state_field=STATE --dob_fie +ld=DOB Options: --table table --id_field id field --state_field state field --dob_field dob field -help -man [tbone@MDB 202025-007]$ perl sql.pl SELECT , , , , FROM WHERE like '336%' or like '335%' or like '346%' or like '337%' or like '342%' [tbone@MDB 202025-007]$

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re: Required options processing Getopt::Long
by particle (Vicar) on Aug 15, 2003 at 17:43 UTC

    you're not checking for required fields. for example:

    pod2usage(1) unless length $table and length $id_field;

    ~Particle *accelerates*

Re: Required options processing Getopt::Long
by Abigail-II (Bishop) on Aug 15, 2003 at 20:43 UTC
    As Johan Vromans, the author of Getopt::Long, likes to say, Getopt::Long is for parsing *options*. Arguments that are required are not options.

    Hence, calling the script without arguments isn't a failure according to GetOptions, and no usage message will be generated.

    You will have to do this checking yourself.

    Abigail

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Required options processing Getopt::Long
by antirice (Priest) on Aug 16, 2003 at 00:07 UTC

    Why not use the hashref option of GetOptions?

    my %options; my @required = qw(table id_field); GetOptions ( \%options, 'table=s', 'id_field=s', 'state_field=s', 'zip_field=s', 'dob_field=s', 'help|?', 'man' ) or die pod2usage(2); # dies if an unlisted option is given # if an option isn't specified, it isn't set in the hash so # we look for the ones we want and make certain they're there die pod2usage(2) if grep(!exists $options{$_},@required);

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Because there is no strictness on hash keys - if you mistype it somewhere, you get a mysteriously malfunctioning program rather than a loud complaint from strict.

      Makeshifts last the longest.