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?
Also, I would STRONGLY suggest using the idiomatic way of processing command line arguments in Perl,
Getopt::Longmodule (and start using named parameters and not position-based ones).You don’t really CARE if you have <3 parameters. You usually care if you have parameters a, b and C present.
As far as command line interface design, 3 parameters is about where the cut-off is between positional parameters (
cmd <arg1> <arg2>) vs. named parameters in any order (cmd -arg1 <arg1> -arg2 <arg2>).So you are better off doing: