How to pass command line arguments to include("<julia file>")?



I have a Julia file that takes command line arguments and parses them using ArgParse. I want to run the program in the REPL (trying to debug with Gallium). How do I pass the command line arguments?


Does this work for you?

From the command line:

$ cat test.jl
function main(args)
    @show args

$ julia test.jl hello world
args = String["hello","world"]

From the REPL:

julia> ARGS = ["hello", "world"]
args = String["hello","world"]


Yes, thank you! There is one other change I had to make to accommodate ArgParse.jl
The ArgParse function parse_args(s) parses the command line input with parsing specified by s. I had to instead explicitly pass it ARGS as parse_args(ARGS, s) to use ArgParse in the REPL.


There is that question on StackOverflow. I was wondering if passing arguments as a Collection to include function is against some ideas behind julia. Because if not, I think it could be a useful feature. As of now you are either required to modify your script with the suggested isdefined() check or redefine ARGS explicitly which results in a warning.

I think It would be nice as it would for example streamline using others’ scripts that are not written as modules from within your own ones.


Merging in to duplicate


Let’s suppose the API is include("script.jl", ARGS=["--foo", "123"]). What would that do?