Alex Rivera | Logout

Easy way to check for valid command line parameters?

Asked 2011-02-16T21:30:42.497
15

I'm looking for an easy way to check for a correct number of command line parameters, displaying a usage message if an error occurs and then immediately exit.

I thought of something like

if (@ARGV < 3) {
  print STDERR "Usage: $0 PATTERN [FILE...]\n";
  exit 1;
}

Is this a valid pattern?

Edit
Report

1 Answer

6

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

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

answered 2011-02-16T21:32:01.170

Your Answer