有没有一种方法可以使用REPL的参数来运行julia脚本? [英] Is there a way to run julia script with arguments from REPL?
问题描述
我可以使用来自Powershell的参数以> julia test.jl 'a' 'b'
运行julia脚本.我可以使用include("test.jl")
从REPL运行脚本,但是include
仅接受一个参数-脚本的路径.
I can run julia script with arguments from Powershell as > julia test.jl 'a' 'b'
. I can run a script from REPL with include("test.jl")
but include
accepts just one argument - the path to the script.
从玩弄include
开始,它似乎将脚本作为代码块运行,并且所有变量都引用了current(?)范围,因此,如果我在REPL中显式重新定义ARGS
变量,它将抓住并显示相应的脚本结果:
From playing around with include
it seems that it runs a script as a code block with all the variables referencing the current(?) scope so if I explicitly redefine ARGS
variable in REPL it catches on and displays corresponding script results:
>ARGS="c","d"
>include("test.jl") # prints its arguments in a loop
c
d
但是,这为重新定义ARGS
提供了警告,并且似乎不是实现此目的的预期方式.还有另一种方法可以从REPL(或从另一个脚本)运行脚本,同时明确说明其参数?
This however gives a warning for redefining ARGS
and doesn't seem the intended way of doing that. Is there another way to run a script from REPL (or from another script) while stating its arguments explicitly?
推荐答案
您可能不想通过include
运行自包含脚本.有两种选择:
You probably don't want to run a self-contained script by include
ing it. There are two options:
-
如果脚本不在您的控制范围内,并且从命令行调用该脚本是规范的界面,只需在单独的Julia进程中对其进行调用.
run(`$JULIA_HOME/julia path/to/script.jl arg1 arg2`)
.有关更多详细信息,请参见运行外部命令.
If the script isn't in your control and calling it from the command-line is the canonical interface, just call it in a separate Julia process.
run(`$JULIA_HOME/julia path/to/script.jl arg1 arg2`)
. See running external commands for more details.
如果您可以控制脚本,则将脚本分为两部分可能更有意义:只定义Julia函数(但不运行任何分析)的类库文件,以及命令行文件,用于解析参数并调用库定义的函数.命令行界面和您现在编写的第二个脚本都可以include
库-或更好的是使类库文件成为完整的
If you have control over the script, it'd probably make more sense to split it up into two parts: a library-like file that just defines Julia functions (but doesn't run any analyses) and a command-line file that parses the arguments and calls the functions defined by the library. Both command-line interface and the second script your writing now can include
the library — or better yet make the library-like file a full-fledged package.
这篇关于有没有一种方法可以使用REPL的参数来运行julia脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!