On 2020-05-11 11:48, Peter Pentchev wrote:
On Mon, May 11, 2020 at 12:40:05AM -0700, ToddAndMargo via perl6-users wrote:
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

If "ghi" is not an option or an argument to an option, then I believe
that Getopt::Long, like similar modules in other languages, will
just leave it in @ARGV.

I also wanted to pick up an extra pair not on the list
(unknown option) and complain about them to the user

Well, it seems that Getopt::Long's exceptions are not quite that
detailed for the moment. It could be done though, the module could
be touched up a little bit so that there are different exceptions
for different problems encountered, and some of them contained
additional details... but that's up to the author or to whoever
decides to fork and extend the module :)

G'luck,
Peter


Thank you for the words of wisdom!

I am after the functionality I see in Fedora.  For
example

    dnf [options] <command> [<args>...]
    $ dnf --enablerepo=* install firefox

But I will have to compromise

:-)

-T

Reply via email to