GetOptions(\%param, 'file:s', 'name:s', 'any:s', 'other:s', 'params:s' +); foreach (qw(file name any other params)) { die "Option $_ requires an argument\n" if defined($param{$_}) && $pa +ram{$_} eq ''; }
Using the colon (e.g. 'file:s') in the GetOptions call instead of an equals sign makes a string parameter on the switch optional rather than required. As a result, when GetOptions sees '-file -name', it interprets this as 'switch -file with no optional string param, followed by switch -name with no optional string param', rather than as 'switch -file with string param -name'. Then the subsequent loop goes through and complains if one of those "optional" parameters in fact has an empty value.
Also, if you check the perldoc for Getopt::Long you'll find that you can define handler subroutines to be called when an individual switch is encountered, which would probably also let you trap the case that's bothering you. I haven't used that capability in a while and it seems like it would be more work, but perhaps I haven't addressed all the angles of your question.
Good luck!
In reply to Re: Re: Re: Getopts::Long -- changing its behavior
by seattlejohn
in thread Getopts::Long -- changing its behavior
by jonjacobmoon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |