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

I'm just starting to use Getopt::Long "for real". I'd like to be able to call a sub and output useage info if the user puts in bad options or no options.

I tried this:
#Print useage info sub useage { print "\nNifty usage info should go here\n"; exit; } my ($pass, $user, $config_file); # Get our options from the commandline; print options if not correct &usage if not (GetOptions('pass=s' => \$pass, 'user=s' => \$user, 'con +fig=s' => \$config_file));
but apparently that's no good as GetOptions returns true even if no options are passed!

I suppose I can check the values of every option, but that seems like a kludge. What do the wise monks suggest?

Thanks,
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Making Getopt::Long tell me if no parameters passed
by djantzen (Priest) on Dec 17, 2002 at 05:06 UTC

    It may be worth moving to the more robust version of GetOptions that takes a hash reference as a first argument. For example:

    my %opts; GetOptions(\%opts, 'user=s', 'pass=s', 'config=s');

    This will store your options in the hash, and you can test whether anything came through with:

    usage() unless keys %opts;

    I'd recommend this version for use in most cases actually; it seems cleaner to me than a whole list of arguments to scalar references.

Re: Making Getopt::Long tell me if no parameters passed
by Enlil (Parson) on Dec 17, 2002 at 03:44 UTC
    At the very bottom of the docs in the troubleshooting section you will find the following line:

    GetOptions does not return a false result when an option is not supplied
    That's why they're called 'options'.

    What you might want to try is to set a default variable to 0 that is changed to a 1 when any option is passed. (or vice versa), and then calling the usage sub appropriately.

    -enlil

      Thanks.

      I decided to go with
      &usage if not defined $user; &usage if not defined $pass; &usage if not defined $config;
      until I could figure out something more elegant.

      Cheers,
      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
        fever pointed out the right thing to do, but just for completeness' sake, your approach could be condensed, too. The obvious, naive version: &usage unless (defined $user) or (defined $pass) or (defined $config); And the Once And Only Once version: &usage unless grep defined, $user, $pass, $config; That said, go with passing GetOptions a hashref.

        Makeshifts last the longest.

Re: Making Getopt::Long tell me if no parameters passed
by herveus (Prior) on Dec 17, 2002 at 12:42 UTC
    Howdy!

    It immediately occurred to me that GetOptions will remove items from @ARGV as it processes them. A cursory glance at the man page does not contradict that recollection.

    If you count the number of items in @ARGV before you call GetOptions, you can see if any are missing afterwards. If @ARGV didn't change, you can infer that no options were found. Similarly, if @ARGV is empty, there are no options to process.

    Methinks that the question you are asking GetOptions to answer is not one it tries to answer; other avenues of interrogation are called for.

    yours,
    Michael

Re: Making Getopt::Long tell me if no parameters passed
by PodMaster (Abbot) on Dec 17, 2002 at 22:27 UTC
    You don't need Getopt::Long to tell you that, you got your logic funny.
    use Getopt::Long; #Print useage info sub useage { print "\nNifty usage info should go here\n"; exit; } my ($pass, $user, $config_file); GetOptions( 'pass=s' => \$pass, 'user=s' => \$user, 'config=s' => \$config_file ); if(defined $config ) { # do something }elsif( defined $user and defined $pass){ # do something} }else{ useage(); }
    Anyway, I highly reccomend that you use Pod::Usage along with Getopt::Long. See The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Making Getopt::Long tell me if no parameters passed
by ibanix (Hermit) on Dec 17, 2002 at 03:17 UTC
    Correction: sub useage { should read sub usage { to match the sub call at the end.

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;