@@ -24,7 +24,7 @@ This means that your MAIN subroutine may be a C<multi sub>, each candidate
24
24
of which responsible for some part of processing the given command line
25
25
arguments.
26
26
27
- = item creating / showing USAGE information calling MAIN failed
27
+ = item creating / showing USAGE information if calling MAIN failed
28
28
29
29
If multi dispatch failed, then the user of the script should be informed as
30
30
well as possible as to why it failed. By default, this is done by inspecting
@@ -279,6 +279,89 @@ candidates and their parameters. As shown before, you can specify an
279
279
additional extended description for each candidate using a
280
280
C < #|(...) > Pod block to set L « C < WHY > |/routine/WHY» .
281
281
282
+ = head1 Intercepting CLI argument parsing (2018.10, v6.d and later)
283
+
284
+ You can replace / augment the default way of argument parsing by supplying a
285
+ C < ARGS-TO-CAPTURE > subroutine yourself, or by importing one from any of
286
+ the L < Getopt|https://modules.perl6.org/search/?q=getopt > modules available
287
+ in the ecosystem.
288
+
289
+ X < |ARGS-TO-CAPTURE >
290
+ = head2 ARGS-TO-CAPTURE (2018.10, v6.d and later)
291
+
292
+ The C < ARGS-TO-CAPTURE > subroutine should accept two parameters: a Callable
293
+ representing the C < MAIN > unit to be executed (so it can be introspected if
294
+ necessary) and an array with the arguments from the command line. It
295
+ should return a L < Capture|/type/Capture > object that should be used to
296
+ dispatch the C < MAIN > unit. A B < very > contrived example that will create
297
+ a C < Capture > dependin on some keyword that was entered (which can be handy
298
+ during testing of a command line interface of a script):
299
+
300
+ sub ARGS-TO-CAPTURE(&main, @args --> Capture) {
301
+ # if we only specified "frobnicate" as an argument
302
+ @args == 1 && @args[0] eq 'frobnicate'
303
+ # then dispatch as MAIN("foo","bar",verbose => 2)
304
+ ?? Capture.new( list => <foo bar>, hash => { verbose => 2 } )
305
+ # otherwise, use default processing of args
306
+ !! &*ARGS-TO-CAPTURE(&main, @args)
307
+ }
308
+
309
+ Note that the dynamic variable L < &*ARGS-TO-CAPTURE > is available to perform
310
+ the default command line arguments to C < Capture > processing, so you don't
311
+ have to reinvent the whole wheel if you don't want to.
312
+
313
+ X < |&*ARGS-TO-CAPTURE >
314
+ = head2 &*ARGS-TO-CAPTURE (2018.10, v6.d and later)
315
+
316
+ A dynamic variable available inside any custom C < ARGS-TO-CAPTURE > subroutine
317
+ that can be used to perform the default argument parsing. Takes the same
318
+ parameters as are expected of the custom C < ARGS-TO-CAPTURE > subroutine.
319
+
320
+ = head1 Intercepting usage message generation (2018.10, v6.d and later)
321
+
322
+ You can replace / augment the default way of usage message generation
323
+ (after a failed dispatch to MAIN) by supplying a C < GENERATE-USAGE > subroutine
324
+ yourself, or by importing one from any of the
325
+ L < Getopt|https://modules.perl6.org/search/?q=getopt > modules available in the
326
+ ecosystem.
327
+
328
+ X < |GENERATE-USAGE >
329
+ = head2 GENERATE-USAGE (2018.10, v6.d and later)
330
+
331
+ The C < GENERATE-USAGE > subroutine should accept a Callable representing the
332
+ C < MAIN > subroutine that didn't get executed because the dispatch failed.
333
+ This can be used for introspection. All the other parameters are the
334
+ parameters that were set up to be sent to C < MAIN > . It should return the
335
+ string of the usage information you want to be shown to the user. An example
336
+ that will just recreate the C < Capture > that was created from processing the
337
+ arguments:
338
+
339
+ sub GENERATE-USAGE(&main, |capture) {
340
+ capture<foo>:exists
341
+ ?? "You're not allowed to specify a --foo"
342
+ !! &*GENERATE-USAGE(&main, |capture)
343
+ }
344
+
345
+ You can also use multi subroutine to create the same effect:
346
+
347
+ multi sub GENERATE-USAGE(&main, :$foo!) {
348
+ "You're not allowed to specify a --foo"
349
+ }
350
+ multi sub GENERATE-USAGE(&main, |capture) {
351
+ &*GENERATE-USAGE(&main, |capture)
352
+ }
353
+
354
+ Note that the dynamic variable L < &*GENERATE-USAGE > is available to perform
355
+ the default usage message generation, so you don't have to reinvent the
356
+ whole wheel if you don't want to.
357
+
358
+ X < |&*GENERATE-USAGE >
359
+ = head2 &*GENERATE-USAGE (2018.10, v6.d and later)
360
+
361
+ A dynamic variable available inside any custom C < GENERATE-USAGE > subroutine
362
+ that can be used to perform the default usage message creation. Takes the
363
+ same parameters as are expected of the custom C < GENERATE-USAGE > subroutine.
364
+
282
365
= end pod
283
366
284
367
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
0 commit comments