On 2020-05-10 23:47, Peter Pentchev wrote:
On Sun, May 10, 2020 at 06:20:02PM -0700, ToddAndMargo via perl6-users wrote:
Hi All,
https://modules.raku.org/dist/Getopt::Long:cpan:LEONT
How do I handle stray entries (unknown options) without
crashing? Is there some grab bag I can stick all
those into?
When something signals an error using "die", you can catch it
using a CATCH block, especially if you know that the exception
thrown will be of a particular type. Now, the Getopt::Long
documentation does not explicitly mention it, but the module does
indeed define its own exception type, so you can do something
like this:
use v6.d;
use Getopt::Long;
get-options('length=i' => my $length);
CATCH { when Getopt::Long::Exception { .message.note; exit 1 } };
if defined $length {
say "Whee, length is $length";
} else {
say 'No length specified';
}
The "when" condition within the CATCH block will make it only handle
these exceptions; any other errors will be handled (or passed through) in
their own way.
For more information about exceptions, see the documentation at
https://docs.raku.org/language/exceptions
G'luck,
Peter
I was actually hope to pick up the extra entries and use them
programname.pl6 --abc def ghi
I wanted to pick up ghi and use it
I also wanted to pick up an extra pair not on the list
(unknown option) and complain about them to the user