Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: option on command line

by larsen (Parson)
on Jul 28, 2002 at 11:06 UTC ( [id://185833]=note: print w/replies, xml ) Need Help??


in reply to option on command line

For this kind of purposes, I use Getopt::Mixed.
#!/usr/bin/perl use strict; use Getopt::Mixed; use vars qw|$opt_u $opt_x $opt_m|; # Note that you have to use globals... Getopt::Mixed::getOptions( qq|u=s x:i m:i| ); # Option u wants (mandatory) a string # Option x accepts (not mandatory) an integer # Option m too... die "Hey man, you have to provide an username!\n" unless $opt_u ; print "Option 'u' is $opt_u\n";
Now your script will accept options from the command line, storing them in globals and complaining if you use options without their mandatory argument (i.e., you can't use a bare -u on the command line).

What makes Getopt::Mixed interesting is the possibility to specify longer synonyms for options. So, you can change a line of the script in this way:

Getopt::Mixed::getOptions( qq|u=s username>u x:i m:i| );
username is a synonym for u, so that you can call your script from commandline is these two ways with the same results:
# getopt.pl -u larsen
Option 'u' is larsen

# getopt.pl --username larsen
Option 'u' is larsen

Update: Added an error message in case the user does not provide -u option

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found