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?

3 Likes

Does this work for you?

From the command line:

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

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

From the REPL:

julia> ARGS = ["hello", "world"]
       include("test.jl")
args = String["hello","world"]
8 Likes

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.

1 Like

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.

1 Like

Merging in to duplicate

1 Like

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

5 Likes

I couldn’t run my program from the REPL.
When I try this:

julia> ARGS = ["hello", "world"] include("test.jl")
error: ERROR: syntax: extra token "include" after end of expression

How can you make it 2 lines:
If I try to run first:

ARGS = ["hello", "world"] 
Error: ERROR: cannot assign variable Base.ARGS from module main

But command line is working with out any problem

3 Likes

The format include("script.jl", ARGS=["--foo", "123"]) looks good to pass arguments. Rigth now we get an error if we try to redefine ARGS, or use @Michael_Eastwood suggestion in a single line.

1 Like

Hello everybody.

I would like to ask you: how to enter the argument(s) for only one of the many functions I have in my script from command line?

Thank you!

Please provide an MWE. Also, it may be best to just start a new topic since your question may not be related to this one.

Thank you.
I opened a new topic because I think you are right and my question is not exactly related to this one of this thread.

Here the new post:
link

Cheers

If I include a ā€˜peace of code’ it just share the ā€˜includer’ ARGS dictionary. Include have the same effect than just copying the included code.

MWE:

echo "@show ARGS" > /tmp/test.jl
echo 'ARGS = ["Hello World"]' > /tmp/test2.jl
echo 'include("/tmp/test.jl")' >> /tmp/test2.jl
julia /tmp/test2.jl

This will fail with the error:

ERROR: LoadError: cannot assign a value to variable Base.ARGS from module Main
Stacktrace:
[1] top-level scope at /tmp/test2.jl:1
in expression starting at /tmp/test2.jl:1

I’d very much like to know a work around other than wrap the target script in a function since I don’t think that would work in my use case without cluttering up my working directory with virtually empty files that just wrap other files.

edit: Actually, I just thought of a possible work around, though I don’t have time to test it right now. Later I’ll see if maybe I can pass the ARGS via stdout. In the target script (the one being included), I would check to see if ARGS is defined. If not, it will try reading from stdin.

This still feels unnecessary, but I think that should work.

edit2: Probably a better work around would be to check if args is defined. If it is, define variables based on that, otherwise look in ARGS. I’ll probably do that.

You can modify ARGS rather than creating a new variable named ARGS

$ echo "@show ARGS" > test.jl
$ echo 'push!(empty!(ARGS), "Hello World")' > test2.jl
$ echo 'include("test.jl")' >> test2.jl
$ julia test2.jl
ARGS = ["Hello World"]

BTW, the code should not error. This is a bug that seems to have been introduced in Julia 1.5. Edit: https://github.com/JuliaLang/julia/issues/39259

4 Likes

Thanks! That makes sense.

As for this being a bug, it actually seemed semi-reasonable for ARGS to be protected. I didn’t even think to check to see if it was a feature or a bug.

Wouldn’t it be simpler if the include function could consider an optional tuple argument with the command line parameters? ARGS would be evaluated per file.

I don’t think this is a common use case. The workaround (modify ARGS) is sufficient for debugging scripts.