Julia:passing argument to the `include(“file.jl”)`

2019-07-25 14:27发布

I was wondering if it is possible to pass arguments to include("file.jl"). For example we parse the ARGS in the file.jl and use them in there. Similar to what we do in a command line by passing arguments.

1条回答
Emotional °昔
2楼-- · 2019-07-25 15:12

Reassigning ARGS to make file.jl think it received arguments works, but leads to a warning (because it overwrites Base.ARGS). A better methods perhaps is to use isdefined to check for a different source of parameters before using ARGS in file.jl.

For example, file main.jl would be:

newARGS = String["adios","amigos"]
include("file.jl")

and file.jl would be:

localARGS = isdefined(:newARGS) ? newARGS : ARGS
@show localARGS

Now:

$ julia file.jl hello world
localARGS = String["hello","world"]

$ julia main.jl 
localARGS = String["adios","amigos"]

This also allows communicating deeper through several levels of inclusion.

查看更多
登录 后发表回答