Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to determine flag ? 3

Status
Not open for further replies.

slok

Programmer
Jul 2, 1999
108
SG
I have a script which I want to start from the command line.

Following are examples how I would probably start it..

1. myscript.pl -5
- accept only numeric input

2. myscript.pl /pathname
- give a pathname, if no pathname then it default to
current directory
- path can be absolute or relative

3. myscript.pl path1 /path2
- give more than 1 pathname, it should be able to accept
N path...
- path can be absolute or relative

4. myscript.pl -5 path1 /path2
- combination of inputs...

how can I go about accepting the inputs and doing some
form of validations?

thanks
-
 
This is from

With Perl, command-line arguments are stored in the array named @ARGV.

$ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc.

$#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line
is $#ARGV + 1.

Here's a simple program that prints the number of command-line arguments it's given, and the values of the
arguments:


#!/usr/bin/perl
#---------------------#
# PROGRAM: argv.pl #
#---------------------#

$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments.\n";

foreach $argnum (0 .. $#ARGV) {

print "$ARGV[$argnum]\n";

}


If you run this program from a Unix command-line like this

./argv.pl 1 2 3 4


or, from a DOS command-line like this

perl argv.pl 1 2 3 4


you'll get this result:

thanks, you gave me 4 command-line arguments.
1
2
3
4


Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
but given that some of my arguments are prefix with
"-" and some are paths that may or may not start with a "/"

how can I process the arguments to determine what kind
of arguments they are...?

thanks
 
just look for those characters at the beginning of each @ARGV:[tt]
foreach (@ARGV)
{
if (m/^-/) {}
if (m/^\//) {}
#etc...
}[/tt]
"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
stillflame,

after matching the character...
how do I assign the value of the argument to a variable..

eg

foreach (@ARGV) {
if ( m/^-/) { # if it matches dash
#assign number variable
}

if ( m/^\//) {
#assign to path variable
}
}
 
slok

you can assign command line arguments to variables with the command

$var = shift;

This command does two things -- it assigns the first parameter to the variable and then removes that parameter from the paramater list.

So you can use it repeatedly, like this:

$var1 = shift;
$var2 = shift;
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
MikeLacey,

the 'shift' suggestion works.

however, if I were to take it a step further...

for my script, I can potentially expect an infinite or
at least a lot of arguments ....

some of these arguments will be of the same type..
eg. multiple path listed as input...

what will be the best way to assign "dynamically"
all the "path argument" in @ARGV to variables dynamically?

thanks
 
I would write a loop that goes through the command line parameters and then does something appropriate with each of them. You would have to check the arg to find out if it looks like a path. (in other words - does it contain the directory delimiter character / or \ ) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top