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

-- 
Peter Pentchev  r...@ringlet.net r...@debian.org p...@storpool.com
PGP key:        http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13

Attachment: signature.asc
Description: PGP signature

Reply via email to