On 2020-05-05 12:28, yary wrote:
Oops my example was missing the important $*USAGE message. And it makes sense to show the wrong named args before the wrong list args.

example.raku:
   multi sub MAIN(:$these ="These", :$are="Are") { say "$these $are"; };
  multi sub MAIN(*@wrong-list,:$these ="These", :$are="Are",*%wrong-named) is hidden-from-USAGE
     {say "$*USAGE\nGot bad args " ~ %wrong-named ~ " "~ @wrong-list}'
raku example.raku -these=HIHIHI -foo=BABABA

I am extremely confused.

The man page makes no sense to me.  But they not suppose to
be for beginners, just refreshers for advanced users
who don't need them.

    https://docs.raku.org/routine/MAIN#is_hidden-from-USAGE

The example you gave me had an extra single quote at the
end that drove me nuts to figure out

This is what I have now:

<MainTest.pl6>

#!/usr/bin/env perl6

multi sub MAIN( Str:$these ="These", Str :$are="Are", Bool :$help=False ) {
   say "these = <$these>   are = <$are>   help = <$help>"; };

multi sub MAIN( *@wrong-list, :$these ="These", :$are="Are", *%wrong-named ) is hidden-from-USAGE { say "$*USAGE\nGot bad args <" ~ %wrong-named ~ "> ["~ @wrong-list ~ "]"; }

</MainTest.pl6>


$ MainTest.pl6 --are=our --these=those abc

Usage:
  MainTest.pl6 [--these=<Str>] [--are=<Str>] [--help]
Got bad args <> [abc]



$ MainTest.pl6 --are=our --these=those --notme=abc
Usage:
  MainTest.pl6 [--these=<Str>] [--are=<Str>] [--help]
Got bad args <notme  abc> []


So is `@wrong-list` is empty if there are stray pairs

And `%wrong-named` picks up and properly formed pairs
that on not part of the call.

Thank you!

-T

This is my keeper example on the subject:



#!/usr/bin/env perl6

# sub MAIN(:$these ="These", :$are="Are") { say "$these $are"; }


# Note: both these subs get called (multi sub). This one for a proper command line
multi sub MAIN( Str:$these ="These", Str :$are="Are", Bool :$help=False ) {
say "First multi sub: these = <$these> are = <$are> help = <$help>\n";
}


# Note: both these subs get called (multi sub). This one for a improper command line
#       both subs are required to form the $*USAGE variable
multi sub MAIN( *@wrong-list, :$these ="These", :$are="Are", Bool :$help=False, *%wrong-named )
      is hidden-from-USAGE  {

# Note: $*USAGE is the error message that would have been printed
   #         *%wrong-named    collects up stray pairs not in the list
   #         *@wrong-list     collects up stray entries that are not pairs

   say "Second multi sub:";
   say "$*USAGE\n";           # note: `Usage:\n` is part of the variable
   say "Stray entries:\n" ~
       "     Got unknows pairs     <" ~ %wrong-named ~ ">\n" ~
       "     Got stray argument(s) <"~ @wrong-list ~ ">\n";
   say "these = <$these>   are = <$are>   help = <$help>\n";
}


MAIN;

Reply via email to