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

**URL:** https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731
**Category:** New to Julia
**Tags:** question, repl
**Created:** [July 7, 2017, 11:47pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731 "2017-07-07T23:47:04Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_de\_la\_Torre](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@Luis\_de\_la\_Torre](https://discourse.julialang.org/u/Luis_de_la_Torre)
#### Post date: [July 7, 2017, 11:47pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/1 "2017-07-07T23:47:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)
#### Post date: [July 8, 2017, 12:03am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/2 "2017-07-08T00:03:54Z")

</div>

Does this work for you?

From the command line:

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

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

```

From the REPL:

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

```

---

<div class="post-metadata">

### Author: ![Luis\_de\_la\_Torre](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@Luis\_de\_la\_Torre](https://discourse.julialang.org/u/Luis_de_la_Torre)
#### Post date: [July 10, 2017, 3:56pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/3 "2017-07-10T15:56:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ephemeris](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@ephemeris](https://discourse.julialang.org/u/ephemeris)
#### Post date: [July 10, 2017, 4:00pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/4 "2017-07-10T16:00:30Z")

</div>

There is [that question on StackOverflow](https://stackoverflow.com/questions/44967240/juliapassing-argument-to-the-includefile-jl). 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.

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [July 10, 2017, 4:00pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/5 "2017-07-10T16:00:18Z")

</div>

Merging in to duplicate

> [@How to pass command line arguments to include("\<julia file\>")?](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731):
>
> 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?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 11, 2017, 8:34pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/6 "2017-07-11T20:34:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![orhan\_abar](https://avatars.discourse-cdn.com/v4/letter/o/e5b9ba/32.png) [@orhan\_abar](https://discourse.julialang.org/u/orhan_abar)
#### Post date: [October 21, 2018, 2:24am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/7 "2018-10-21T02:24:12Z")

</div>

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

```julia
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:

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

```

But command line is working with out any problem

---

<div class="post-metadata">

### Author: ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)
#### Post date: [January 18, 2019, 10:20pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/8 "2019-01-18T22:20:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![flalom](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@flalom](https://discourse.julialang.org/u/flalom)
#### Post date: [January 24, 2020, 9:21pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/9 "2020-01-24T21:21:11Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 25, 2020, 7:48am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/10 "2020-01-25T07:48:39Z")

</div>

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.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![flalom](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@flalom](https://discourse.julialang.org/u/flalom)
#### Post date: [January 25, 2020, 11:21am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/11 "2020-01-25T11:21:08Z")

</div>

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](https://discourse.julialang.org/t/provide-a-specific-argument-to-a-specific-function-from-a-series-of-functions-from-command-line/33777)

Cheers

---

<div class="post-metadata">

### Author: ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)
#### Post date: [September 19, 2020, 4:59am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/12 "2020-09-19T04:59:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nstgc](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nstgc](https://discourse.julialang.org/u/nstgc)
#### Post date: [January 14, 2021, 6:08pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/13 "2021-01-14T18:08:31Z")

</div>

MWE:

```julia
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 `include`d), 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.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [January 14, 2021, 9:11pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/14 "2021-01-14T21:11:38Z")

</div>

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

```nohighlight
$ 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](https://github.com/JuliaLang/julia/issues/39259)

---

<div class="post-metadata">

### Author: ![nstgc](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nstgc](https://discourse.julialang.org/u/nstgc)
#### Post date: [January 14, 2021, 9:30pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/15 "2021-01-14T21:30:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)
#### Post date: [January 23, 2021, 4:16pm UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/16 "2021-01-23T16:16:05Z")

</div>

> [@fredrikekre](#):
>
> `push!(empty!(ARGS), "Hello World")`

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.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 30, 2021, 8:37am UTC](https://discourse.julialang.org/t/how-to-pass-command-line-arguments-to-include-julia-file/4731/17 "2021-01-30T08:37:25Z")

</div>

> [@RaulDurand](#):
>
> simpler if the include function could consider an optional tuple argument with the command line parameters?

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