diff --git a/HISTORY.md b/HISTORY.md index 55bbfa1335a0e..8d876766c0aab 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/base/client.jl b/base/client.jl index 3ca2d54745290..83998e0b0e22b 100644 --- a/base/client.jl +++ b/base/client.jl @@ -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 ``` @@ -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 @@ -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` diff --git a/doc/src/manual/command-line-interface.md b/doc/src/manual/command-line-interface.md index 50002801e06f8..cb33b76b7d95d 100644 --- a/doc/src/manual/command-line-interface.md +++ b/doc/src/manual/command-line-interface.md @@ -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 @@ -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! $ ``` @@ -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! $ ``` @@ -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 ``` diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 3fbfdd9bc1257..e18fe3d026251 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -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 diff --git a/test/project/Rot13/src/Rot13.jl b/test/project/Rot13/src/Rot13.jl index 0672799d61f24..1d19cbbe6df91 100644 --- a/test/project/Rot13/src/Rot13.jl +++ b/test/project/Rot13/src/Rot13.jl @@ -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