You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: doc/src/manual/command-line-interface.md
+18-10Lines changed: 18 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,8 +46,24 @@ but at the conclusion of executing a script or expression, `julia` will attempt
46
46
`Main.main(Base.ARGS)` if such a function `Main.main` has been defined and this behavior was opted into
47
47
by using the `@main` macro.
48
48
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`
51
67
function may be spatially and temporally separated from the invocation. However, for interactive workflows,
52
68
the behavior is equivalent to explicitly calling `exit(main(ARGS))` at the end of the evaluated script or
53
69
expression.
@@ -56,14 +72,6 @@ expression.
56
72
The special entry point `Main.main` was added in Julia 1.11. For compatibility with prior julia versions,
57
73
add an explicit `@isdefined(var"@main") ? (@main) : exit(main(ARGS))` at the end of your scripts.
58
74
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
-
67
75
Only the `main` binding in the `Main` module has this behavior and only if
68
76
the macro `@main` was used within the defining module.
0 commit comments