@@ -12,47 +12,48 @@ The default command line interface of Perl 6 scripts consists of three parts:
12
12
= head2 Parsing the command line parameters into a L < capture|/type/Capture >
13
13
14
14
This looks at the values in L < @*ARGS|/language/variables#index-entry-@*ARGS > ,
15
- interprets these according to some policy, and creates a C < Capture > object
16
- out of that. An alternative way of parsing may be provided by the developer
15
+ interprets these according to some policy, and creates a L < Capture/type/Capture >
16
+ object out of that. An alternative way of parsing may be provided by the developer
17
17
or installed using a module.
18
18
19
19
= head2 Calling a provided C < MAIN > subroutine using that capture
20
20
21
21
Standard L < multi dispatch|/language/functions#index-entry-declarator_multi-Multi-dispatch >
22
- is used to call the MAIN subroutine with the generated C < Capture > object.
23
- This means that your MAIN subroutine may be a C < multi sub > , each candidate
22
+ is used to call the C < MAIN > subroutine with the generated C < Capture > object.
23
+ This means that your C < MAIN > subroutine may be a C < multi sub > , each candidate
24
24
of which is responsible for some part of processing the given command line
25
25
arguments.
26
26
27
27
= head2 Creating / showing usage information if calling C < MAIN > failed
28
28
29
29
If multi dispatch failed, then the user of the script should be informed as
30
- well as possible as to why it failed. By default, this is done by inspecting
31
- the signature of each MAIN candidate sub, and any associated pod information.
30
+ well as possible as to why it failed. By default, this is done by inspecting
31
+ the signature of each C < MAIN > candidate sub, and any associated Pod information.
32
32
The result is then shown to the user on STDERR (or on STDOUT if C < --help >
33
- was specified). An alternative way of generating the usage information may
33
+ was specified). An alternative way of generating the usage information may
34
34
be provided by the developer or installed using a module.
35
35
36
36
X < |MAIN >
37
37
= head1 sub MAIN
38
38
39
39
The sub with the special name C < MAIN > will be executed after all relevant entry
40
40
phasers (C < BEGIN > , C < CHECK > , C < INIT > , C < PRE > , C < ENTER > ) have been run and
41
- the mainline of the script has been executed. No error will occur if there
42
- is no MAIN sub: your script will then just have to do the work, such as
43
- argument parsing, in the mainline of the script.
44
-
45
- Any normal exit from the MAIN sub will result in an exit code of C < 0 > ,
46
- indicating success. Any return value of the MAIN sub will be ignored.
47
- If an exception is thrown that is not handled inside the MAIN sub, then the
48
- exit code will be C < 1 > . If the dispatch to MAIN failed, a usage message
41
+ the L < mainline|/language/glossary#index-entry-Mainline > of the script has been
42
+ executed. No error will occur if there is no C < MAIN > sub: your script will
43
+ then just have to do the work, such as argument parsing, in the mainline of
44
+ the script.
45
+
46
+ Any normal exit from the C < MAIN > sub will result in an exit code of C < 0 > ,
47
+ indicating success. Any return value of the C < MAIN > sub will be ignored.
48
+ If an exception is thrown that is not handled inside the C < MAIN > sub, then the
49
+ exit code will be C < 1 > . If the dispatch to C < MAIN > failed, a usage message
49
50
will be displayed on STDERR and the exit code will be C < 2 > .
50
51
51
52
The command line parameters are present in the C < @*ARGS > dynamic variable
52
53
and may be altered in the mainline of the script before the C < MAIN > unit is
53
54
called.
54
55
55
- The signature of (the candidates of the multi) sub MAIN determines which
56
+ The signature of (the candidates of the multi) sub C < MAIN > determines which
56
57
candidate will actually be called using the standard
57
58
L < multi dispatch|/language/glossary#index-entry-Multi-Dispatch > semantics.
58
59
@@ -63,7 +64,8 @@ A simple example:
63
64
say "Hello $name, how are you?"
64
65
}
65
66
66
- If you call that script without any parameters:
67
+ If you call that script without any parameters, you get the following
68
+ usage message:
67
69
68
70
= begin code :lang<shell>
69
71
$ perl6 hello.p6
@@ -95,10 +97,10 @@ Another way to do this is to make C<sub MAIN> a C<multi sub>:
95
97
multi sub MAIN() { say "Hello bashful, how are you?" }
96
98
multi sub MAIN($name) { say "Hello $name, how are you?" }
97
99
98
- Which would give the same output as the examples above. Whether you should
100
+ Which would give the same output as the examples above. Whether you should
99
101
use either method to achieve the desired goal is entirely up to you.
100
102
101
- A more complicated example using a single positional and multiple
103
+ A more complicated example using a single positional and multiple
102
104
named parameters:
103
105
104
106
= for code :method<False>
@@ -141,7 +143,7 @@ Usage:
141
143
= end code
142
144
143
145
Although you don't have to do anything in your code to do this, it may still
144
- be regarded as a bit terse. But there's an easy way to make that usage
146
+ be regarded as a bit terse. But there's an easy way to make that usage
145
147
message better by providing hints using pod features:
146
148
147
149
= for code :method<False>
@@ -197,7 +199,7 @@ X<|%*SUB-MAIN-OPTS>
197
199
= head2 %*SUB-MAIN-OPTS
198
200
199
201
It's possible to alter how arguments are processed before they're passed
200
- to C < sub MAIN {} > by setting options in the C < %*SUB-MAIN-OPTS > hash. Due to
202
+ to C < sub MAIN {} > by setting options in the C < %*SUB-MAIN-OPTS > hash. Due to
201
203
the nature of dynamic variables, it is required to set up the
202
204
C < %*SUB-MAIN-OPTS > hash and fill it with the appropriate settings.
203
205
For instance:
@@ -216,7 +218,7 @@ X<|named-anywhere>
216
218
= head3 named-anywhere
217
219
218
220
By default, named arguments passed to the program (i.e., C < MAIN > )
219
- cannot appear after any positional argument. However, if
221
+ cannot appear after any positional argument. However, if
220
222
C « %*SUB-MAIN-OPTS < named-anywhere > » is set to a true value, named arguments
221
223
can be specified anywhere, even after positional parameter. For example,
222
224
the above program can be called with:
@@ -229,9 +231,9 @@ X<|hidden-from-USAGE>
229
231
= head2 is hidden-from-USAGE
230
232
231
233
Sometimes you want to exclude a C < MAIN > candidate from being shown in any
232
- automatically generated usage message. This can be achieved by adding
233
- a C < hidden-from-USAGE > trait to the specification of the MAIN candidate
234
- you do not want to show. Expanding on an earlier example:
234
+ automatically generated usage message. This can be achieved by adding
235
+ a C < hidden-from-USAGE > trait to the specification of the C < MAIN > candidate
236
+ you do not want to show. Expanding on an earlier example:
235
237
236
238
# inside file 'hello.p6'
237
239
multi sub MAIN() is hidden-from-USAGE {
@@ -323,10 +325,10 @@ X<|ARGS-TO-CAPTURE>
323
325
The C < ARGS-TO-CAPTURE > subroutine should accept two parameters: a
324
326
L < Callable|/type/Callable > representing the C < MAIN > unit to be executed (so it
325
327
can be introspected if necessary) and an array with the arguments from the
326
- command line. It should return a L < Capture|/type/Capture > object that will be
327
- used to dispatch the C < MAIN > unit. A B < very > contrived example that will create
328
- a C < Capture > depending on some keyword that was entered (which can be handy
329
- during testing of a command line interface of a script):
328
+ command line. It should return a L < Capture|/type/Capture > object that will be
329
+ used to dispatch the C < MAIN > unit. The following is a B < very > contrived example
330
+ that will create a C < Capture > depending on some keyword that was entered (which
331
+ can be handy during testing of a command line interface of a script):
330
332
331
333
sub ARGS-TO-CAPTURE(&main, @args --> Capture) {
332
334
# if we only specified "frobnicate" as an argument
@@ -357,14 +359,14 @@ Defined as:
357
359
358
360
sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles)
359
361
360
- This routine allows complete control over the handling of C < MAIN > . It gets a
361
- C < Callable > that is the MAIN that should be executed, the return value of the
362
- mainline execution and additional named variables: C < :in-as-argsfiles > which
362
+ This routine allows complete control over the handling of C < MAIN > . It gets a
363
+ C < Callable > that is the C < MAIN > that should be executed, the return value of the
364
+ mainline execution and additional named variables: C < :in-as-argsfiles > which
363
365
will be C < True > if STDIN should be treated as C < $*ARGFILES > .
364
366
365
367
If C < RUN-MAIN > is not provided, a default one will be run that looks for
366
- subroutines of the old interface, such as C < MAIN_HELPER > and C < USAGE > . If
367
- found, will execute following the "old" semantics.
368
+ subroutines of the old interface, such as C < MAIN_HELPER > and C < USAGE > . If
369
+ found, it will execute following the "old" semantics.
368
370
369
371
= begin code
370
372
class Hero {
@@ -391,9 +393,9 @@ X<|GENERATE-USAGE>
391
393
392
394
The C < GENERATE-USAGE > subroutine should accept a C < Callable > representing the
393
395
C < MAIN > subroutine that didn't get executed because the dispatch failed.
394
- This can be used for introspection. All the other parameters are the
395
- parameters that were set up to be sent to C < MAIN > . It should return the
396
- string of the usage information you want to be shown to the user. An example
396
+ This can be used for introspection. All the other parameters are the
397
+ parameters that were set up to be sent to C < MAIN > . It should return the
398
+ string of the usage information you want to be shown to the user. An example
397
399
that will just recreate the C < Capture > that was created from processing the
398
400
arguments:
399
401
@@ -423,13 +425,13 @@ An older interface enabled one to intercept the calling to C<MAIN> completely.
423
425
This depended on the existence of a C < MAIN_HELPER > subroutine that would be
424
426
called if a C < MAIN > subroutine was found in the mainline of a program.
425
427
426
- This interface was never documented. However, any programs using this
427
- undocumented interface will continue to function until C < v6.e > . From v6.d
428
+ This interface was never documented. However, any programs using this
429
+ undocumented interface will continue to function until C < v6.e > . From v6.d
428
430
onward, the use of the undocumented API will cause a C < DEPRECATED > message.
429
431
430
432
Ecosystem modules can provide both the new and the old interface for
431
433
compatibility with older versions of Perl 6: if a newer Perl 6 recognizes
432
- the new (documented) interface, it will use that. If there is no new
434
+ the new (documented) interface, it will use that. If there is no new
433
435
interface subroutine available, but the old C < MAIN_HELPER > interface is,
434
436
then it will use the old interface.
435
437
0 commit comments