Julia passing arguments, reading the command line

2019-09-01 04:17发布

问题:

I am trying to prompt the user of my .jl file to enter user entered multiple destinations.

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)

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.

回答1:

The first item to note in the code in your question is:

(String)in = (String)Readline(STDIN)

Julia 1.0.0 now uses stdin instead of STDIN.

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

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) 

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"

Command Line Args Method

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

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

You can also print out the script file name as shown, PrintArgs.jl.



回答2:

After searching & testing I found one solution and decided to reply to it here. I had to declare my own function to be able to get the program to accept user input.

function input(prompt::AbstractString="")
    print(prompt)
    return chomp(readline())
end

I am not sure what the chomp function does, but I know it works for what I was asking. I am still curious if you can do something in Julia similar to java and C String args[], in which you pass extra information while you are telling your command to run. Something like the following.

Julia testFile.jl goHere.txt lookHere.txt


标签: input julia