Julia:将参数传递给include("file.jl") [英] Julia:passing argument to the `include("file.jl")`

查看:169
本文介绍了Julia:将参数传递给include("file.jl")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将参数传递给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")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆