问题
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:
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.
来源:https://stackoverflow.com/questions/44967240/juliapassing-argument-to-the-includefile-jl