On 2020-05-11 21:19, Peter Pentchev wrote:
#!/usr/bin/env rakuuse 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); }
How do I use it? Where are the options declared? $ GetOptLongTest2.pl6 Capture $opts = \() No command specified $ GetOptLongTest2.pl6 --help Unknown option help -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Computers are like air conditioners. They malfunction when you open windows ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
