On 6/18/22 22:16, Bruce Gray wrote:


On Jun 18, 2022, at 10:42 PM, ToddAndMargo via perl6-users 
<perl6-users@perl.org> wrote:

On 6/16/22 10:10, Rick Bychowski wrote:
sub MAIN($n = 20) {
    .say for factors($n); # Nil
}


I thought `MAIN` was a reserved variable.  Am
I missing something?

MAIN has a special meaning as a sub name; it declares a CLI (command-line 
interface).

Just like this code declares a subroutine (that you can call within your 
program) that expects two filenames and an optional flag :
     sub do_it ( $file1, $file2, Bool :$dry-run ) {
         ...
     }
, the same signature in a sub named "MAIN" declares that your whole script is 
to be called on the command-line with two filenames and an optional flag :
     sub MAIN ( $file1, $file2, Bool :$dry-run ) {
         ...
     }

If I call that script like this:
     ./myscript.raku a.txt b.txt
, then MAIN gets 'a.txt' in $file1 and 'b.txt' in $file2. If I call it badly:
     ./myscript.raku a.txt b.txt c.txt
, then I get an error, with a usage message auto-generated by Raku:
     myscript.raku [--dry-run] <file1> <file2>

For full details, see:
https://docs.raku.org/language/create-cli#index-entry-MAIN



I can definitely see a usage for that!  Thank you!


I currently use something like this:


# Note: @*ARGS[x] starts counting at 0
if @*ARGS.elems > 0  {
   $Usage       = lc "@*ARGS[0]";    # lc = lower case
   if $Usage eq "--debug" || $Usage eq "debug" {
      $Debug = True;
      $Usage = "";
   } else {
      $RunSpecific = "@*ARGS[0]";
      $Debug       = True;
   }
}


if ( "$Usage" eq "--help" || "$Usage" eq "help" || "$Usage" eq "-?" || "$Usage" eq "/?" ) {
   PrintGreen "Usage: $IAm [ debug | --help ] [ Run_Specific_Module ] \n";
   PrintGreen "       $IAm debug Run_Specific_Module\n";
   PrintGreen "       $IAm debug GetADWCleaner\n\n";
PrintGreen " Note: module name is case sensititive; debug and help are not\n";
   exit;
}

Reply via email to