问题描述
我想知道是否可以将参数传递给include("file.jl")
.例如,我们解析file.jl
中的ARGS并在其中使用它们.类似于我们在命令行中通过传递参数进行的操作.
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.
推荐答案
重新分配ARGS
以使file.jl
认为它已收到参数是可行的,但是会导致警告(因为它会覆盖Base.ARGS
).更好的方法也许是在file.jl
中使用ARGS
之前,先使用isdefined
检查参数的不同来源.
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
.
例如,文件main.jl
为:
newARGS = String["adios","amigos"]
include("file.jl")
和file.jl
将是:
localARGS = isdefined(:newARGS) ? newARGS : ARGS
@show localARGS
现在:
$ 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.
这篇关于Julia:将参数传递给include("file.jl")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!