@@ -524,6 +524,46 @@ candidates and their parameters. As shown before, you can specify an
524524additional extended description for each candidate using a
525525C<#|(...)> Pod block to set L«C<WHY>|/routine/WHY».
526526
527+ 
528+ =head1 Intercepting usage message generation (2018.10, v6.d and later)
529+ 
530+ You can replace or augment the default way of usage message generation
531+ (after a failed dispatch to MAIN) by supplying a C<GENERATE-USAGE> subroutine
532+ yourself, or by importing one from any of the
533+ L<Getopt|https://raku.land/?q=getopt> modules available in the
534+ ecosystem.
535+ 
536+ =head2 X<sub GENERATE-USAGE|Subroutines,GENERATE-USAGE>
537+ 
538+ The C<GENERATE-USAGE> subroutine should accept a L<C<Callable>|/type/Callable> representing the
539+ C<MAIN> subroutine that didn't get executed because the dispatch failed.
540+ This can be used for introspection. All the other parameters are the
541+ parameters that were set up to be sent to C<MAIN>. It should return the
542+ string of the usage information you want to be shown to the user. An example
543+ that will just recreate the L<C<Capture>|/type/Capture> that was created from processing the
544+ arguments:
545+ 
546+     sub GENERATE-USAGE(&main, |capture) {
547+         capture<foo>:exists
548+           ?? "You're not allowed to specify a --foo"
549+           !! &*GENERATE-USAGE(&main, |capture)
550+     }
551+ 
552+ You can also use multi subroutines to create the same effect:
553+ 
554+     multi GENERATE-USAGE(&main, :$foo!) {
555+         "You're not allowed to specify a --foo"
556+     }
557+     multi GENERATE-USAGE(&main, |capture) {
558+         &*GENERATE-USAGE(&main, |capture)
559+     }
560+ 
561+ Note that the dynamic variable
562+ L<C<&*GENERATE-USAGE>|/language/variables#&*GENERATE-USAGE> is available to
563+ perform the default usage message generation so you don't have to reinvent the
564+ whole wheel if you don't want to.
565+ 
566+ 
527567=head1 Intercepting CLI argument parsing (2018.10, v6.d and later)
528568
529569You can replace or augment the default way of argument parsing by supplying an
@@ -555,21 +595,13 @@ L<C<&*ARGS-TO-CAPTURE>|/language/variables#&*ARGS-TO-CAPTURE> is available to
555595perform the default command line arguments to L<C<Capture>|/type/Capture> processing so you don't
556596have to reinvent the whole wheel if you don't want to.
557597
558- =head1 Intercepting usage message generation (2018.10, v6.d and later)
559- 
560- You can replace or augment the default way of usage message generation
561- (after a failed dispatch to MAIN) by supplying a C<GENERATE-USAGE> subroutine
562- yourself, or by importing one from any of the
563- L<Getopt|https://raku.land/?q=getopt> modules available in the
564- ecosystem.
565- 
566598=head2 X<sub RUN-MAIN|Subroutines,RUN-MAIN>
567599
568600    sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles)
569601
570602This routine allows complete control over the handling of C<MAIN>. It gets a
571603L<C<Callable>|/type/Callable> that is the C<MAIN> that should be executed, the return value of the
572- mainline execution and additional named variables : C<:in-as-argsfiles> which
604+ mainline execution and an  additional named variable : C<:in-as-argsfiles> which
573605will be C<True> if STDIN should be treated as C<$*ARGFILES>.
574606
575607If C<RUN-MAIN> is not provided, a default one will be run that looks for
@@ -596,36 +628,6 @@ RUN-MAIN( &new-main, Nil );
596628This will print the name (first argument) of the generated object.
597629
598630
599- =head2 X<sub GENERATE-USAGE|Subroutines,GENERATE-USAGE>
600- 
601- The C<GENERATE-USAGE> subroutine should accept a L<C<Callable>|/type/Callable> representing the
602- C<MAIN> subroutine that didn't get executed because the dispatch failed.
603- This can be used for introspection. All the other parameters are the
604- parameters that were set up to be sent to C<MAIN>. It should return the
605- string of the usage information you want to be shown to the user. An example
606- that will just recreate the L<C<Capture>|/type/Capture> that was created from processing the
607- arguments:
608- 
609-     sub GENERATE-USAGE(&main, |capture) {
610-         capture<foo>:exists
611-           ?? "You're not allowed to specify a --foo"
612-           !! &*GENERATE-USAGE(&main, |capture)
613-     }
614- 
615- You can also use multi subroutines to create the same effect:
616- 
617-     multi GENERATE-USAGE(&main, :$foo!) {
618-         "You're not allowed to specify a --foo"
619-     }
620-     multi GENERATE-USAGE(&main, |capture) {
621-         &*GENERATE-USAGE(&main, |capture)
622-     }
623- 
624- Note that the dynamic variable
625- L<C<&*GENERATE-USAGE>|/language/variables#&*GENERATE-USAGE> is available to
626- perform the default usage message generation so you don't have to reinvent the
627- whole wheel if you don't want to.
628- 
629631=head1 Intercepting MAIN calling (before 2018.10, v6.e)
630632
631633An older interface enabled one to intercept the calling to C<MAIN> completely.
0 commit comments