Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: using "Getopt::Long" how to check parameter mandatory

by VinsWorldcom (Prior)
on Aug 24, 2010 at 14:45 UTC ( [id://856953]=note: print w/replies, xml ) Need Help??


in reply to using "Getopt::Long" how to check parameter mandatory

Just check @ARGV after you parse your options:

use Getopt::Long; my $data = "file1"; my $length = 4; my $verbose; GetOptions ( "length=i" => \$length, "file=s" => \$data, "verbose" => \$verbose ); if (!@ARGV) { print "$0: Argument required.\n"; exit 1 }

I also like to use POD to document my code; thus, pod2usage can be invoked to produce 'nicer' usage output:

use Pod::Usage; ... GetOptions( ... ) or pod2usage(-verbose => 0); ... # Make sure at least one argument provided if (!@ARGV) { pod2usage(-verbose => 0, -message => "$0: argument required\n") }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2026-04-10 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.