Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

using getopts::long and testing for command line options

by c (Hermit)
on Aug 22, 2001 at 18:44 UTC ( #106965=perlquestion: print w/replies, xml ) Need Help??

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

I'd like to be able to test and see if a user has entered any command line flags for a script. If they do not, I'd like to echo an error, however, if they do, I'd like the script to continue forward. I have:

#!/usr/bin/perl -w use strict; use Getopt::Long; my $in; GetOptions ( 'in=s' => \$in ); if ($#ARGV < 0) { print "you need to put in some command line options!\n"; exit(1); }

however, even when i run ./script --in something the warning is still echoed to the screen. Am i misinterpretting $#ARGV or does getopts::long decrease the value of $#ARGV as it reads in options?

humbly -c

Replies are listed 'Best First'.
Re: using getopts::long and testing for command line options
by ozone (Friar) on Aug 22, 2001 at 18:51 UTC
    according to the GetOpt cpan page:
    "The command line options are taken from array @ARGV. Upon completion of GetOptions, @ARGV will contain the rest (i.e. the non-options) of the command line."

    So your @ARGV should be empty

Re: using getopts::long and testing for command line options
by idnopheq (Chaplain) on Aug 22, 2001 at 18:50 UTC
    Move the GetOptions line after the @ARGV test. The Getopt modules clear their elements from @ARGV when they process.

    HTH
    --
    idnopheq
    Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Re: using getopts::long and testing for command line options
by MZSanford (Curate) on Aug 22, 2001 at 18:57 UTC
    If you want to check for any incorrect options, look at the return code from GetOptions(), and it will be 0 if there were any unknown options (or incorrect ones) provided.
    can't sleep clowns will eat me
    -- MZSanford

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://106965]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-06-26 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (85 votes). Check out past polls.

    Notices?