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

大憨熊 提交于 2020-01-15 06:56:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!