I’m working on a little script in Perl and I have problems with parameters from the command line. I’m using GetOps to parse them as shown in the code below:
%params = (
"help" => "",
"no_inline" => "",
"no_dupl" => ""
);
¶mError if (!GetOptions(
"help" => \$params{"help"},
"no-inline" => \$params{"no_inline"},
"no-duplicates" => \$params{"no_dupl"},
));
I then run the script like this:
> script.pl --no-inline # ok, valid parameter
> script.pl --no-inline blahblah # blahblah is not valid
> script.pl --no-i # not valid
Problem is that in the second and third example GetOps says the parameters are valid. What should I do in order do make these parameters invalid?
Your third example is valid.
GetOptionsaccepts abbreviated names as long as they are not ambiguous.If you had for example an option called
no-indent, your third example would have been rejected because it is ambiguous, but--no-inlwould still be accepted.To disable this, use:
See Configuring Getopt::Long for other options.
Your second example is perfectly normal.
blahblahwill be left in@ARGVafter theGetOptionscall. If your script only accepts the options you specified, and cannot take other arguments (like filenames or whatever), just check that@ARGVis empty after the call.