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

When giving - as the program, read code from standard in. #43191

Merged
merged 1 commit into from
Jan 12, 2022
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Command-line option changes
* New option `--strip-ir` to remove the compiler's IR (intermediate representation) of source
code when building a system image. The resulting image will only work if `--compile=all` is
used, or if all needed code is precompiled ([#42925]).
* When the program file is `-` the code to be executed is read from standard in ([#43191]).

Multi-threading changes
-----------------------
Expand Down
6 changes: 5 additions & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ function exec_options(opts)
exit_on_sigint(true)
end
try
include(Main, PROGRAM_FILE)
if PROGRAM_FILE == "-"
include_string(Main, read(stdin, String), "stdin")
else
include(Main, PROGRAM_FILE)
end
catch
invokelatest(display_error, scrub_repl_backtrace(current_exceptions()))
if !is_interactive::Bool
Expand Down
19 changes: 19 additions & 0 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,22 @@ end

# issue #39259, shadowing `ARGS`
@test success(`$(Base.julia_cmd()) --startup-file=no -e 'ARGS=1'`)

@testset "- as program file reads from stdin" begin
for args in (`- foo bar`, `-- - foo bar`)
cmd = `$(Base.julia_cmd()) --startup-file=no $(args)`
io = IOBuffer()
open(cmd, io; write=true) do proc
write(proc, """
println(PROGRAM_FILE)
println(@__FILE__)
foreach(println, ARGS)
""")
end
lines = collect(eachline(seekstart(io)))
@test lines[1] == "-"
@test lines[2] == "stdin"
@test lines[3] == "foo"
@test lines[4] == "bar"
end
end