朱莉娅传递参数,阅读命令行 [英] Julia passing arguments, reading the command line
问题描述
我试图提示我的.jl文件用户输入用户输入的多个目的地.
I am trying to prompt the user of my .jl file to enter user entered multiple destinations.
基本上,我需要用户输入&输出.我知道在Java中,您将使用扫描仪,或者在命令行中编译时接受参数.无论哪种选择我都很好.通过查看Julia文档,我认为我可以完成将变量分配给readline函数的第一种方法.
Basically I need the user to give an input & an output. I know in java you would use a scanner, or accept arguments when it is compiled in the command line. I am fine with either option. From what I have found looking through the Julia Documentation I think I could accomplish the first way of assigning a variable to the readline function.
(String)in = (String)Readline(STDIN)
据我了解,变量"in"现在应包含用户输入的字符串.我在编译.jl代码时遇到错误,因为我的命令提示符不会停止读取用户输入,而只是完成读取.jl代码.
From my understanding the variable 'in' should now contain a String of the user's input. I am encountering an error, in that when I compile my .jl code, because my command prompt does not stop to read user input, and just finishes reading the .jl code.
推荐答案
问题代码中要注意的第一项是:
The first item to note in the code in your question is:
(String)in = (String)Readline(STDIN)
Julia 1.0.0现在使用stdin
而不是STDIN
.
Julia 1.0.0 now uses stdin
instead of STDIN
.
接下来,(String)
类型转换不是您在Julia中需要或不想做的事情.
Next, the (String)
typecasting is not something you need or want to do in Julia.
因此您的代码可以读取(尽管会出现错误):
Thus your code could read (though we get an error):
julia> in = readline(stdin)
This is a test.
ERROR: cannot assign variable Base.in from module Main
因此,变量in
与Julia Base.in
变量冲突.只需使用另一个变量名即可.
So variable in
is in conflict with a Julia Base.in
variable. Just use a another variable name.
julia> response = readline(stdin)
This is a test.
"This is a test"
此代码现在可以运行,但是没有提示.您的答案提供了一个示例输入功能,并带有如下定义的提示:
This code is now working, but it has no prompt. Your answer provides an example input function with a prompt which you defined like this:
julia> function input(prompt::AbstractString="")
print(prompt)
return chomp(readline())
end
input (generic function with 2 methods)
chomp
函数从输入字符串中删除单个尾随的\n
换行符. Docs 此处.
The chomp
function removes a single trailing \n
newline character from the input string. Docs here.
此处使用该功能的示例:
Example use of the function here:
julia> input_file = input("Please enter input file name: ")
Please enter input file name: Data.txt
"Data.txt"
julia> output_file = input("Please enter output file name: ")
Please enter output file name: Output.txt
"Output.txt"
命令行Args方法
作为 docs 指出,仅打印出给脚本的参数,您可以执行以下操作:
As the docs point out, to just print out the arguments given to a script, you can do something like this:
println("Arguments passed to ", PROGRAM_FILE, ":")
for arg in ARGS
println(arg)
end
以下是在Windows命令行上运行的上述代码的示例:
Here is an example of above code running on the Windows command line:
c:\OS_Prompt>julia PrintArgs.jl Data.txt Output1.txt Output2.txt Output3.txt
Arguments passed to PrintArgs.jl:
Data.txt
Output1.txt
Output2.txt
Output3.txt
您还可以打印出脚本文件名,如PrintArgs.jl.
You can also print out the script file name as shown, PrintArgs.jl.
这篇关于朱莉娅传递参数,阅读命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!