Skip to content

Commit 3fd548d

Browse files
committed
Add support for determining name of script #14109
Note that PROGRAM_FILE was chosen as the name for this variable as it matches the help for the Julia CLI.
1 parent e6aba04 commit 3fd548d

File tree

7 files changed

+42
-21
lines changed

7 files changed

+42
-21
lines changed

NEWS.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ New language features
151151

152152
* `@__LINE__` special macro now available to reflect invocation source line number ([#12727]).
153153

154+
* `PROGRAM_FILE` global is now available for determining the name of the running script ([#14114]).
155+
154156
Language changes
155157
----------------
156158

@@ -1585,6 +1587,7 @@ Too numerous to mention.
15851587
[#7917]: https://github.com/JuliaLang/julia/issues/7917
15861588
[#7992]: https://github.com/JuliaLang/julia/issues/7992
15871589
[#8011]: https://github.com/JuliaLang/julia/issues/8011
1590+
[#8036]: https://github.com/JuliaLang/julia/issues/8036
15881591
[#8089]: https://github.com/JuliaLang/julia/issues/8089
15891592
[#8113]: https://github.com/JuliaLang/julia/issues/8113
15901593
[#8135]: https://github.com/JuliaLang/julia/issues/8135
@@ -1726,12 +1729,15 @@ Too numerous to mention.
17261729
[#12727]: https://github.com/JuliaLang/julia/issues/12727
17271730
[#12739]: https://github.com/JuliaLang/julia/issues/12739
17281731
[#13062]: https://github.com/JuliaLang/julia/issues/13062
1732+
[#13232]: https://github.com/JuliaLang/julia/issues/13232
17291733
[#13338]: https://github.com/JuliaLang/julia/issues/13338
17301734
[#13387]: https://github.com/JuliaLang/julia/issues/13387
17311735
[#13440]: https://github.com/JuliaLang/julia/issues/13440
17321736
[#13465]: https://github.com/JuliaLang/julia/issues/13465
1733-
[#13496]: https://github.com/JuliaLang/julia/issues/13496
17341737
[#13480]: https://github.com/JuliaLang/julia/issues/13480
1738+
[#13496]: https://github.com/JuliaLang/julia/issues/13496
17351739
[#13542]: https://github.com/JuliaLang/julia/issues/13542
17361740
[#13680]: https://github.com/JuliaLang/julia/issues/13680
17371741
[#13681]: https://github.com/JuliaLang/julia/issues/13681
1742+
[#13897]: https://github.com/JuliaLang/julia/issues/13897
1743+
[#14114]: https://github.com/JuliaLang/julia/issues/14114

base/client.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## client.jl - frontend handling command line options, environment setup,
44
## and REPL
55

6+
PROGRAM_FILE = UTF8String("")
67
const ARGS = UTF8String[]
78

89
const text_colors = AnyDict(
@@ -209,10 +210,9 @@ function init_bind_addr()
209210
LPROC.bind_port = UInt16(bind_port)
210211
end
211212

212-
function process_options(opts::JLOptions, args::Vector{UTF8String})
213-
if !isempty(args)
214-
arg = first(args)
215-
idxs = find(x -> x == "--", args)
213+
function process_options(opts::JLOptions)
214+
if !isempty(ARGS)
215+
idxs = find(x -> x == "--", ARGS)
216216
if length(idxs) > 1
217217
println(STDERR, "julia: redundant option terminator `--`")
218218
exit(1)
@@ -266,15 +266,15 @@ function process_options(opts::JLOptions, args::Vector{UTF8String})
266266
eval(Main, parse_input_line(bytestring(opts.postboot)))
267267
end
268268
# load file
269-
if !isempty(args) && !isempty(args[1])
269+
if !isempty(ARGS) && !isempty(ARGS[1])
270270
# program
271271
repl = false
272272
# remove filename from ARGS
273-
shift!(ARGS)
273+
global PROGRAM_FILE = UTF8String(shift!(ARGS))
274274
if !is_interactive
275275
ccall(:jl_exit_on_sigint, Void, (Cint,), 1)
276276
end
277-
include(args[1])
277+
include(PROGRAM_FILE)
278278
end
279279
break
280280
end
@@ -374,7 +374,7 @@ function _start()
374374
append!(ARGS, Core.ARGS)
375375
opts = JLOptions()
376376
try
377-
(quiet,repl,startup,color_set,history_file) = process_options(opts,copy(ARGS))
377+
(quiet,repl,startup,color_set,history_file) = process_options(opts)
378378

379379
local term
380380
global active_repl

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export
182182
JULIA_HOME,
183183
LOAD_PATH,
184184
OS_NAME,
185+
PROGRAM_FILE,
185186
STDERR,
186187
STDIN,
187188
STDOUT,

doc/manual/getting-started.rst

+9-6
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,22 @@ argument to the julia command::
5151

5252
As the example implies, the following command-line arguments to julia
5353
are taken as command-line arguments to the program ``script.jl``, passed
54-
in the global constant ``ARGS``. ``ARGS`` is also set when script code
55-
is given using the ``-e`` option on the command line (see the ``julia``
56-
help output below). For example, to just print the arguments given to a
57-
script, you could do this::
54+
in the global constant ``ARGS``. The name of the script itself is passed
55+
in as the global ``PROGRAM_FILE``. Note that ``ARGS`` is also set when script
56+
code is given using the ``-e`` option on the command line (see the ``julia``
57+
help output below) but ``PROGRAM_FILE`` will be empty. For example, to just
58+
print the arguments given to a script, you could do this::
59+
60+
$ julia -e 'println(PROGRAM_FILE); for x in ARGS; println(x); end' foo bar
5861

59-
$ julia -e 'for x in ARGS; println(x); end' foo bar
6062
foo
6163
bar
6264

6365
Or you could put that code into a script and run it::
6466

65-
$ echo 'for x in ARGS; println(x); end' > script.jl
67+
$ echo 'println(PROGRAM_FILE); for x in ARGS; println(x); end' > script.jl
6668
$ julia script.jl foo bar
69+
script.jl
6770
foo
6871
bar
6972

doc/stdlib/constants.rst

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Constants
1414
A symbol representing the name of the operating system. Possible values
1515
are ``:Linux``, ``:Darwin`` (OS X), or ``:Windows``.
1616

17+
.. data:: PROGRAM_FILE
18+
19+
A string containing the script name passed to Julia from the command line. Note that the
20+
script name remains unchanged from within included files. Alternatively see :data:`@__FILE__`.
21+
1722
.. data:: ARGS
1823

1924
An array of the command line arguments passed to Julia, as strings.

doc/stdlib/file.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320

321321
.. Docstring generated from Julia source
322322
323-
``@__FILE__`` expands to a string with the absolute path and file name of the script being run. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ .
323+
``@__FILE__`` expands to a string with the absolute path and file name of the containing the macro. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ . Alternatively see :data:`PROGRAM_FILE`.
324324

325325
.. function:: @__LINE__ -> Int
326326

test/cmdlineargs.jl

+11-5
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,26 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
207207
@test !success(`$exename --worker=true`)
208208

209209
# test passing arguments
210-
let testfile = tempname()
210+
let testfile = tempname(), altfile = tempname()
211211
try
212-
# write a julia source file that just prints ARGS to STDOUT and exits
212+
# write a julia source file that just prints PROGRAM_FILE and ARGS to STDOUT and exits
213213
open(testfile, "w") do io
214+
println(io, "println(PROGRAM_FILE)")
214215
println(io, "println(ARGS)")
215216
println(io, "exit(0)")
216217
end
217-
@test readchomp(`$exename $testfile foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
218-
@test readchomp(`$exename $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
219-
@test readchomp(`$exename -L $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
218+
open(altfile, "w") do io
219+
println(io, "println(PROGRAM_FILE)")
220+
end
221+
@test split(readchomp(`$exename $testfile foo -bar --baz`), '\n') == ["$testfile", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
222+
@test split(readchomp(`$exename $testfile -- foo -bar --baz`), '\n') == ["$testfile", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
223+
@test split(readchomp(`$exename -L $testfile -- foo -bar --baz`), '\n') == ["", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
224+
@test split(readchomp(`$exename -L $altfile $testfile`), '\n') == ["", "$testfile", "UTF8String[]"]
220225
@test !success(`$exename --foo $testfile`)
221226
@test !success(`$exename -L $testfile -- foo -bar -- baz`)
222227
finally
223228
rm(testfile)
229+
rm(altfile)
224230
end
225231
end
226232

0 commit comments

Comments
 (0)