Skip to content

Commit

Permalink
Add support for determining name of script JuliaLang#14109
Browse files Browse the repository at this point in the history
Note that PROGRAM_FILE was chosen as the name for this variable as it
matches the help for the Julia CLI.
  • Loading branch information
omus committed Dec 3, 2015
1 parent e6aba04 commit 9315f9b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ New language features

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

* `PROGRAM_FILE` global is now available for determining the name of the running script ([#14114]).

Language changes
----------------

Expand Down Expand Up @@ -1585,6 +1587,7 @@ Too numerous to mention.
[#7917]: https://github.com/JuliaLang/julia/issues/7917
[#7992]: https://github.com/JuliaLang/julia/issues/7992
[#8011]: https://github.com/JuliaLang/julia/issues/8011
[#8036]: https://github.com/JuliaLang/julia/issues/8036
[#8089]: https://github.com/JuliaLang/julia/issues/8089
[#8113]: https://github.com/JuliaLang/julia/issues/8113
[#8135]: https://github.com/JuliaLang/julia/issues/8135
Expand Down Expand Up @@ -1726,12 +1729,15 @@ Too numerous to mention.
[#12727]: https://github.com/JuliaLang/julia/issues/12727
[#12739]: https://github.com/JuliaLang/julia/issues/12739
[#13062]: https://github.com/JuliaLang/julia/issues/13062
[#13232]: https://github.com/JuliaLang/julia/issues/13232
[#13338]: https://github.com/JuliaLang/julia/issues/13338
[#13387]: https://github.com/JuliaLang/julia/issues/13387
[#13440]: https://github.com/JuliaLang/julia/issues/13440
[#13465]: https://github.com/JuliaLang/julia/issues/13465
[#13496]: https://github.com/JuliaLang/julia/issues/13496
[#13480]: https://github.com/JuliaLang/julia/issues/13480
[#13496]: https://github.com/JuliaLang/julia/issues/13496
[#13542]: https://github.com/JuliaLang/julia/issues/13542
[#13680]: https://github.com/JuliaLang/julia/issues/13680
[#13681]: https://github.com/JuliaLang/julia/issues/13681
[#13897]: https://github.com/JuliaLang/julia/issues/13897
[#14114]: https://github.com/JuliaLang/julia/issues/14114
16 changes: 8 additions & 8 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## client.jl - frontend handling command line options, environment setup,
## and REPL

PROGRAM_FILE = UTF8String("")
const ARGS = UTF8String[]

const text_colors = AnyDict(
Expand Down Expand Up @@ -209,10 +210,9 @@ function init_bind_addr()
LPROC.bind_port = UInt16(bind_port)
end

function process_options(opts::JLOptions, args::Vector{UTF8String})
if !isempty(args)
arg = first(args)
idxs = find(x -> x == "--", args)
function process_options(opts::JLOptions)
if !isempty(ARGS)
idxs = find(x -> x == "--", ARGS)
if length(idxs) > 1
println(STDERR, "julia: redundant option terminator `--`")
exit(1)
Expand Down Expand Up @@ -266,15 +266,15 @@ function process_options(opts::JLOptions, args::Vector{UTF8String})
eval(Main, parse_input_line(bytestring(opts.postboot)))
end
# load file
if !isempty(args) && !isempty(args[1])
if !isempty(ARGS) && !isempty(ARGS[1])
# program
repl = false
# remove filename from ARGS
shift!(ARGS)
global PROGRAM_FILE = UTF8String(shift!(ARGS))
if !is_interactive
ccall(:jl_exit_on_sigint, Void, (Cint,), 1)
end
include(args[1])
include(PROGRAM_FILE)
end
break
end
Expand Down Expand Up @@ -374,7 +374,7 @@ function _start()
append!(ARGS, Core.ARGS)
opts = JLOptions()
try
(quiet,repl,startup,color_set,history_file) = process_options(opts,copy(ARGS))
(quiet,repl,startup,color_set,history_file) = process_options(opts)

local term
global active_repl
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export
JULIA_HOME,
LOAD_PATH,
OS_NAME,
PROGRAM_FILE,
STDERR,
STDIN,
STDOUT,
Expand Down
15 changes: 9 additions & 6 deletions doc/manual/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ argument to the julia command::

As the example implies, the following command-line arguments to julia
are taken as command-line arguments to the program ``script.jl``, passed
in the global constant ``ARGS``. ``ARGS`` is also set when script code
is given using the ``-e`` option on the command line (see the ``julia``
help output below). For example, to just print the arguments given to a
script, you could do this::
in the global constant ``ARGS``. The name of the script itself is passed
in as the global ``PROGRAM_FILE``. Note that ``ARGS`` is also set when script
code is given using the ``-e`` option on the command line (see the ``julia``
help output below) but ``PROGRAM_FILE`` will be empty. For example, to just
print the arguments given to a script, you could do this::

$ julia -e 'println(PROGRAM_FILE); for x in ARGS; println(x); end' foo bar

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

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

$ echo 'for x in ARGS; println(x); end' > script.jl
$ echo 'println(PROGRAM_FILE); for x in ARGS; println(x); end' > script.jl
$ julia script.jl foo bar
script.jl
foo
bar

Expand Down
4 changes: 4 additions & 0 deletions doc/stdlib/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Constants
A symbol representing the name of the operating system. Possible values
are ``:Linux``, ``:Darwin`` (OS X), or ``:Windows``.

.. data:: PROGRAM_FILE

A string containing the script name passed to Julia.

.. data:: ARGS

An array of the command line arguments passed to Julia, as strings.
Expand Down
16 changes: 11 additions & 5 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,26 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
@test !success(`$exename --worker=true`)

# test passing arguments
let testfile = tempname()
let testfile = tempname(), altfile = tempname()
try
# write a julia source file that just prints ARGS to STDOUT and exits
# write a julia source file that just prints PROGRAM_FILE and ARGS to STDOUT and exits
open(testfile, "w") do io
println(io, "println(PROGRAM_FILE)")
println(io, "println(ARGS)")
println(io, "exit(0)")
end
@test readchomp(`$exename $testfile foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
@test readchomp(`$exename $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
@test readchomp(`$exename -L $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
open(altfile, "w") do io
println(io, "println(PROGRAM_FILE)")
end
@test split(readchomp(`$exename $testfile foo -bar --baz`), '\n') == ["$testfile", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
@test split(readchomp(`$exename $testfile -- foo -bar --baz`), '\n') == ["$testfile", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
@test split(readchomp(`$exename -L $testfile -- foo -bar --baz`), '\n') == ["", "UTF8String[\"foo\",\"-bar\",\"--baz\"]"]
@test split(readchomp(`$exename -L $altfile $testfile`), '\n') == ["", "$testfile", "UTF8String[]"]
@test !success(`$exename --foo $testfile`)
@test !success(`$exename -L $testfile -- foo -bar -- baz`)
finally
rm(testfile)
rm(altfile)
end
end

Expand Down

0 comments on commit 9315f9b

Please sign in to comment.