Skip to content

Commit e1c0ac8

Browse files
lmiqnsajkoLilithHafner
authored
Update command-line-interface.md explanation of (@main). (#59573)
Explain in simple terms the usage of `(@main)` and note that the return value must be an `Int32` or `nothing`, in the docs. Related to: #58984 --------- Co-authored-by: Neven Sajko <4944410+nsajko@users.noreply.github.com> Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
1 parent 5c6d66e commit e1c0ac8

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

doc/src/manual/command-line-interface.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,24 @@ but at the conclusion of executing a script or expression, `julia` will attempt
4646
`Main.main(Base.ARGS)` if such a function `Main.main` has been defined and this behavior was opted into
4747
by using the `@main` macro.
4848

49-
This feature is intended to aid in the unification
50-
of compiled and interactive workflows. In compiled workflows, loading the code that defines the `main`
49+
To see this feature in action, consider the following definition:
50+
```julia
51+
(@main)(args) = println("Hello $(args[1])!")
52+
```
53+
Executing the above script with `julia script.jl "Buddy"` will automatically run `(@main)` and print "Hello Buddy!",
54+
despite there being no explicit call to `(@main)`.
55+
56+
The return value of the `(@main)` function must either be `nothing`, resulting in exit code
57+
`0`, or convertible to a `Cint` which will be the exit code:
58+
```
59+
$ julia -e "(@main)(args) = nothing"; echo $?0
60+
0
61+
$ julia -e "(@main)(args) = 1"; echo $?
62+
1
63+
```
64+
Typically exit codes are in the range `0:255`, although the interpretation of the return value might be OS dependent.
65+
66+
This feature is intended to aid in the unification of compiled and interactive workflows. In compiled workflows, loading the code that defines the `main`
5167
function may be spatially and temporally separated from the invocation. However, for interactive workflows,
5268
the behavior is equivalent to explicitly calling `exit(main(ARGS))` at the end of the evaluated script or
5369
expression.
@@ -56,14 +72,6 @@ expression.
5672
The special entry point `Main.main` was added in Julia 1.11. For compatibility with prior julia versions,
5773
add an explicit `@isdefined(var"@main") ? (@main) : exit(main(ARGS))` at the end of your scripts.
5874

59-
To see this feature in action, consider the following definition, which will execute the print function despite there being no explicit call to `main`:
60-
61-
```
62-
$ julia -e '(@main)(args) = println("Hello World!")'
63-
Hello World!
64-
$
65-
```
66-
6775
Only the `main` binding in the `Main` module has this behavior and only if
6876
the macro `@main` was used within the defining module.
6977

0 commit comments

Comments
 (0)