parsing the command line option

V

Vahid Moghaddasi

Hi all,
I need some help to change my perl script to accept more of the same
command line option, for example: myscript.pl -s server1 -s server2 -s
server3 OR myscript.pl -s server1 server2 server3 etc..
The following will only accept one option.
....
while ( $_ = $ARGV[0]) {
shift;
if (/^-s/) { $servername = &getoptions("-server");
}
elsif (/^-u/) { $username =
&getoptions("-username"); }
and so on ...
}
sub getoptions {
$result = $ARGV[0];
shift @ARGV;
return $result;
}

Thanks for your help,
Vahid.
 
B

Bob Walton

Vahid Moghaddasi wrote:

....
I need some help to change my perl script to accept more of the same
command line option, for example: myscript.pl -s server1 -s server2 -s
server3 OR myscript.pl -s server1 server2 server3 etc..
The following will only accept one option.
...
while ( $_ = $ARGV[0]) {
shift;
if (/^-s/) { $servername = &getoptions("-server");
}
elsif (/^-u/) { $username =
&getoptions("-username"); }
and so on ...
}
sub getoptions {
$result = $ARGV[0];
shift @ARGV;
return $result;
} ....
Vahid.

Modules are your friend. Check out

use Getopt::Long;

and

use Getopt::Std; .
 
X

xicheng

how about the following lines?
------------------------------------------------------
while ($_ = shift @ARGV) {
if (/^-s$/) { $servername = shift @ARGV; print
"$servername\n"};
if (/^-u$/) { $username = shift @ARGV; print "$username\n"};
#........;
}
 
J

Jürgen Exner

Vahid said:
Hi all,
I need some help to change my perl script to accept more of the same
command line option,

Don't reinvent the wheel in a square shape. Use the standard module Getopt
instead.
perldoc Getopt::long
if (/^-s/) { $servername = &getoptions("-server");

I suppose you know why you are calling a function using the & notation and
what it does to the call...

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top