On Mon, May 11, 2020 at 04:09:32PM -0700, ToddAndMargo via perl6-users wrote: > 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 > > :-)
Compromise how? Take a look at the attached source. G'luck, Peter -- Peter Pentchev [email protected] [email protected] [email protected] PGP key: http://people.FreeBSD.org/~roam/roam.key.asc Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
#!/usr/bin/env raku
use v6.d;
use Getopt::Long;
sub cmd_install($cmd, Array[Str] :$enablerepo = [], Bool :$y = False, *@rest
--> int)
{
say 'install';
dd $cmd;
dd @rest;
dd $enablerepo;
dd $y;
return 0;
}
sub cmd_list($cmd, Array[Str] :$enablerepo = [], *@rest --> int)
{
say 'list';
dd $cmd;
dd @rest;
dd $enablerepo;
return 42;
}
my %HANDLERS = (
install => &cmd_install,
# "list" doesn't care about the "-y" parameter, so skip it
list => -> $cmd, :$enablerepo = [], :$y = False, *@rest --> int {
cmd_list $cmd, :$enablerepo, |@rest
},
);
sub note-fatal(Str:D $msg)
{
$msg.note;
exit 1;
}
{
my $opts = get-options(
"enablerepo=s@",
"y",
);
CATCH { when Getopt::Long::Exception { .message.note; exit 1 } };
dd $opts;
note-fatal 'No command specified' unless $opts.elems;
my $cmd = $opts[0];
my $handler = %HANDLERS{$cmd};
note-fatal "Unknown command '$cmd'" unless $handler;
exit $handler(|$opts);
}
signature.asc
Description: PGP signature
