85647

Julia passing arguments, reading the command line

Question:

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.

Answer1:

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 <a href="https://docs.julialang.org/en/v1/base/strings/#Base.chomp" rel="nofollow">here</a>.

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"

<strong>Command Line Args Method</strong>

As the <a href="https://docs.julialang.org/en/latest/manual/getting-started/#man-getting-started-1" rel="nofollow">docs</a> 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.

Answer2:

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

Recommend

  • How to convert bool to int in MySql
  • Why does my test of sign always report “negative”?
  • correct method to get the unsigned char pointer length
  • How to cast variable to array
  • Reading text file in Matlab results in unknown spaces within characters
  • Dilemma in deciding how to create CSS for H1, H2, H3 etc
  • Sorting an array of files for rename
  • MS Access 2010: Adding transaction management into a form
  • Gradle Thrift Plugin by Example
  • Is it expensive to create the Thread object or to actually start the thread?
  • Read stdin in chunks in Bash pipe
  • System call time out?
  • How do I prepend to a stream in Bash?
  • How can I count unique terms in a plaintext file case-insensitively?
  • Interpreting STRACE output - pipes and forks
  • Installed module is empty
  • Bad request using file_get_contents for PUT request in PHP
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Can Jackson SerializationFeature be overridden per field or class?
  • SSO with signing and signature validation doesn't work
  • Validaiting emails with Net.Mail MailAddress
  • Deserializing XML into class C#
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Arrays break string types in Julia
  • How to disable jQuery.jplayer autoplay?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • costura.fody for a dll that references another dll
  • How to push additional view controllers onto NavigationController but keep the TabBar?