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

Re: Examples for using Getopt::Long

by ikegami (Patriarch)
on May 09, 2005 at 15:29 UTC ( [id://455203]=note: print w/replies, xml ) Need Help??


in reply to Examples for using Getopt::Long

(Here's an answer to the updated question.)

why do we need to create variables like data and length when the arguments will be passed from command line.

Those were just an example. Instead, you'd call your variables something like $user, $passwd, etc.

use Getopt::Long (); sub usage { my $message = $_[0]; if (defined $message && length $message) { $message .= "\n" unless $message =~ /\n$/; } my $command = $0; $command =~ s#^.*/##; print STDERR ( $message, "usage: $command -u username [-p password] -s hostname:port\n" . " ...\n" . " ...\n" . " ...\n" ); die("\n") } my $user; my $passwd; my $host; my $port; Getopt::Long::GetOptions( 'u=s' => \$user, 'p=s' => \$passwd, 's=s' => sub { local *_ = \$_[1]; /^([^:]+):(\d+)$/ or die("Invalid format for option s.\n"); $host = $1; $port = $2; }, ) or usage("Invalid commmand line options."); usage("The user name must be specified.") unless defined $user; usage("The server hostname and port must be specified.") unless defined $host; if (!defined $passwd) { # Prompt for password here... }

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found