[ACCEPTED]-Easy way to check for valid command line parameters?-perl

Accepted answer
Score: 31

Also, I would STRONGLY suggest using the 10 idiomatic way of processing command line 9 arguments in Perl, Getopt::Long module (and start using 8 named parameters and not position-based 7 ones).

You don't really CARE if you have 6 <3 parameters. You usually care if you 5 have parameters a, b and C present.

As far 4 as command line interface design, 3 parameters 3 is about where the cut-off is between positional 2 parameters (cmd <arg1> <arg2>) vs. named parameters in any 1 order (cmd -arg1 <arg1> -arg2 <arg2>).

So you are better off doing:

use Getopt::Long;
my %args;
GetOptions(\%args,
           "arg1=s",
           "arg2=s",
           "arg3=s",
) or die "Invalid arguments!";
die "Missing -arg1!" unless $args{arg1};
die "Missing -arg2!" unless $args{arg2};
die "Missing -arg3!" unless $args{arg3};
Score: 14

Another common way to do that is to use 2 die

die "Usage: $0 PATTERN [FILE...]\n" if @ARGV < 3;

You can get more help on the @ARGV special variable 1 at your command line:

perldoc -v @ARGV
Score: 6

Yes, it is fine. @ARGV contains the command-line 3 arguments and evaluates in scalar context 2 to their number.

(Though it looks like you 1 meant @ARGV < 2 or < 1 from your error message.)

Score: 1

Use $#ARGV to get total number of passed 9 argument to a perl script like so:

if (@#ARGV < 4)

I've used 8 before and worked as shown in http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/.

See the original 7 documentation at http://perldoc.perl.org/perlvar.html, it states that:

@ARGV

The 6 array @ARGV contains the command-line arguments 5 intended for the script. $#ARGV is generally 4 the number of arguments minus one, because 3 $ARGV[0] is the first argument, not the 2 program's command name itself. See $0 for 1 the command name.

More Related questions