Chris Knipe wrote:
Hi all,
Just a quick question and a couple of lines of really simple code....
use Getopt::Long;
...
GetOptions ('h' => \$h,
'b=s' => \$s );
Sub ShowHelp() {
That should be
sub ShowHelp {
Perl isn't VB :)
print "this is help"
}
Sub DoSomethingWithString() {
...
}
If ($s) {
DoSomethingWithString();
} else {
ShowHelp();
}
Right. Now, whilst the above is not perhaps 100% correct, it goes about a
generality of Getopt::Long.
If I now run the application,
./blah.pl -h - I get the help screen
./blah.pl -s - I get a error, complaining that -s requires a value, and
THEN the help screen.
./blah.pl -s s - Everything is fine.
So, this is more of a block question I think, but how I can get the above
example to show the help screen FIRST, and THEN complain about the missing
value for -s ????
Well, this is a little tricky, because the complaint is issued by the
GetOptions() call. I would usually do something like:
# just show the complaint
GetOptions(...blah...) or exit 1;
or
# show the complaint, then the help
GetOptions(...blah...) or ShowHelp(), exit 1;
In order to show the help before the complaint, you need to "capture"
the complaint with $SIG{__WARN__} something like this:
my $msg = '';
do {
local $SIG{__WARN__} = sub { $msg .= shift };
GetOptions('h' => \$h, 'b=s' => \$s );
} or ShowHelp(), die $msg;
I would suggest sending the help text to STDERR, or unbuffering STDOUT
so that you are sure to get the help text shown before the complaint.
Also, you might look at Pod::Usage for an approach to handling the help
text.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>