Skip to content

Commit

Permalink
When given - as the program name, read program from stdin (JuliaLang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored and LilithHafner committed Feb 22, 2022
1 parent 591faab commit 5ebe36d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -762,3 +762,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

0 comments on commit 5ebe36d

Please sign in to comment.