Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define entry point as main(args) instead of main(ARGS) #54288

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Compiler/Runtime improvements
Command-line option changes
---------------------------

* The entry point for Julia has been standardized to `Main.main(ARGS)`. This must be explicitly opted into using the `@main` macro
* The entry point for Julia has been standardized to `Main.main(Base.ARGS)`. This must be explicitly opted into using the `@main` macro
(see the docstring for further details). When opted-in, and julia is invoked to run a script or expression
(i.e. using `julia script.jl` or `julia -e expr`), julia will subsequently run the `Main.main` function automatically.
This is intended to unify script and compilation workflows, where code loading may happen
Expand Down
8 changes: 4 additions & 4 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,13 @@ The `@main` macro may be used standalone or as part of the function definition,
case, parentheses are required. In particular, the following are equivalent:

```
function (@main)(ARGS)
function (@main)(args)
println("Hello World")
end
```

```
function main(ARGS)
function main(args)
end
@main
```
Expand All @@ -601,7 +601,7 @@ imported into `Main`, it will be treated as an entrypoint in `Main`:
```
module MyApp
export main
(@main)(ARGS) = println("Hello World")
(@main)(args) = println("Hello World")
end
using .MyApp
# `julia` Will execute MyApp.main at the conclusion of script execution
Expand All @@ -611,7 +611,7 @@ Note that in particular, the semantics do not attach to the method
or the name:
```
module MyApp
(@main)(ARGS) = println("Hello World")
(@main)(args) = println("Hello World")
end
const main = MyApp.main
# `julia` Will *NOT* execute MyApp.main unless there is a separate `@main` annotation in `Main`
Expand Down
14 changes: 7 additions & 7 deletions doc/src/manual/command-line-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ See also [Scripting](@ref man-scripting) for more information on writing Julia s
## The `Main.main` entry point

As of Julia, 1.11, `Base` exports the macro `@main`. This macro expands to the symbol `main`,
but at the conclusion of executing a script or expression, `julia` will attempt to execute the function
`Main.main(ARGS)` if such a function has been defined and this behavior was opted into
but at the conclusion of executing a script or expression, `julia` will attempt to execute
`Main.main(Base.ARGS)` if such a function `Main.main` has been defined and this behavior was opted into
by using the `@main` macro.

This feature is intended to aid in the unification
Expand All @@ -59,7 +59,7 @@ expression.
To see this feature in action, consider the following definition, which will execute the print function despite there being no explicit call to `main`:

```
$ julia -e '(@main)(ARGS) = println("Hello World!")'
$ julia -e '(@main)(args) = println("Hello World!")'
Hello World!
$
```
Expand All @@ -70,19 +70,19 @@ the macro `@main` was used within the defining module.
For example, using `hello` instead of `main` will not result in the `hello` function executing:

```
$ julia -e 'hello(ARGS) = println("Hello World!")'
$ julia -e 'hello(args) = println("Hello World!")'
$
```

and neither will a plain definition of `main`:
```
$ julia -e 'main(ARGS) = println("Hello World!")'
$ julia -e 'main(args) = println("Hello World!")'
$
```

However, the opt-in need not occur at definition time:
```
$ julia -e 'main(ARGS) = println("Hello World!"); @main'
$ julia -e 'main(args) = println("Hello World!"); @main'
Hello World!
$
```
Expand All @@ -93,7 +93,7 @@ The `main` binding may be imported from a package. A *hello world* package defin
module Hello

export main
(@main)(ARGS) = println("Hello from the package!")
(@main)(args) = println("Hello from the package!")

end
```
Expand Down
8 changes: 4 additions & 4 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1124,14 +1124,14 @@ end
## `Main.main` entrypoint

# Basic usage
@test readchomp(`$(Base.julia_cmd()) -e '(@main)(ARGS) = println("hello")'`) == "hello"
@test readchomp(`$(Base.julia_cmd()) -e '(@main)(args) = println("hello")'`) == "hello"

# Test ARGS with -e
@test readchomp(`$(Base.julia_cmd()) -e '(@main)(ARGS) = println(ARGS)' a b`) == repr(["a", "b"])
@test readchomp(`$(Base.julia_cmd()) -e '(@main)(args) = println(args)' a b`) == repr(["a", "b"])

# Test import from module
@test readchomp(`$(Base.julia_cmd()) -e 'module Hello; export main; (@main)(ARGS) = println("hello"); end; using .Hello'`) == "hello"
@test readchomp(`$(Base.julia_cmd()) -e 'module Hello; export main; (@main)(ARGS) = println("hello"); end; import .Hello'`) == ""
@test readchomp(`$(Base.julia_cmd()) -e 'module Hello; export main; (@main)(args) = println("hello"); end; using .Hello'`) == "hello"
@test readchomp(`$(Base.julia_cmd()) -e 'module Hello; export main; (@main)(args) = println("hello"); end; import .Hello'`) == ""

# test --bug-report=rr
if Sys.islinux() && Sys.ARCH in (:i686, :x86_64) # rr is only available on these platforms
Expand Down
4 changes: 2 additions & 2 deletions test/project/Rot13/src/Rot13.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ end

rot13(str::AbstractString) = map(rot13, str)

function (@main)(ARGS)
foreach(arg -> print(rot13(arg), " "), ARGS)
function (@main)(args)
foreach(arg -> print(rot13(arg), " "), args)
return 0
end

Expand Down