# Pass command line arguments to a sub-script loaded by a script

**URL:** <https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242>\
**Category:** New to Julia\
**Created:** [July 10, 2019, 11:02pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242 "2019-07-10T23:02:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 10, 2019, 11:02pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/1 "2019-07-10T23:02:04Z")

</div>

Hi,  
I have scripts, which do some calculation and I have scripts which setup workers, on a local machine, on remote machines, with a machinefile, etc. I want to pass arguments from the command line, which are in Base.ARGS, to the second script, namely all arguments except e.g. the first - something like

```julia
# driver_script.jl

arg1 = ARGS[1]

println(arg1)

ARGS = [ARGS[2:end]...]
includ("calculation_script.jl")

```

does not work as we can not reset Base.ARGS. How do I pass arguments to “calculation\_script.jl”?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 11, 2019, 12:30am UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/2 "2019-07-11T00:30:40Z")

</div>

How about

```julia
run("julia calculation_script.jl $(join(ARGS, ' '))")

```

?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 11, 2019, 12:34am UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/3 "2019-07-11T00:34:33Z")

</div>

Though honestly, for anything complicated enough to require 2 files, I’d have all the actual calculations in a single script, and have the includes be just functions, types, and constants.

---

<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:** [July 11, 2019, 5:20am UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/4 "2019-07-11T05:20:21Z")

</div>

> [@jamblejoe](#):
>
> does not work as we can not reset Base.ARGS.

You can. You can for example use `popfirst!`:

```julia
arg1 = popfirst!(ARGS) # ARGS now contains just the rest

```

or you can use `empty!`:

```julia
arg1 = ARGS[1]
tmp = ARGS[2:end]
empty!(ARGS)
append!(ARGS, tmp)

```

etc.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 9:55am UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/5 "2019-07-11T09:55:50Z")

</div>

@fredrikekre Thanks for your answer! This is exactly what I was looking for! 🙂

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 11, 2019, 9:57am UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/6 "2019-07-11T09:57:19Z")

</div>

also you have the option to load the setup of workers script with the `-L` flag  
like

```julia
julia -L workers.jl script.jl args

```

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 1:35pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/7 "2019-07-11T13:35:04Z")

</div>

@BeastyBlacksmith Thanks for pointing this out as well! Can I pass arguments to `workers.jl` and `script.jl` seperately? E.g. number of workers to `workers.jl` and parameters to `script.jl`?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 11, 2019, 1:44pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/8 "2019-07-11T13:44:38Z")

</div>

From a quick test it seems to me that the arguments you pass are available to both files

```julia
julia -L test.jl test.jl a b
["a","b"]
["a","b"]

```

with test.jl:

```julia
println(ARGS)

```

So no seperate argument passing, but you can pick out, what you need.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 1:48pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/9 "2019-07-11T13:48:03Z")

</div>

OK. Can I pass arguments as key-value-pairs as well, like --worker\_count 4 and --param1 0.43 ? As far as I can see, I can only retrieve the arguments as positions in ARGS, e.g. ARGS[1], etc ?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 11, 2019, 1:50pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/10 "2019-07-11T13:50:05Z")

</div>

You might be interested in [GitHub - carlobaldassi/ArgParse.jl: Package for parsing command-line arguments to Julia programs.](https://github.com/carlobaldassi/ArgParse.jl)

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 2:10pm UTC](https://discourse.julialang.org/t/pass-command-line-arguments-to-a-sub-script-loaded-by-a-script/26242/11 "2019-07-11T14:10:22Z")

</div>

Looks promising! I will have a look into it!
