How to ask users ("readline()") to provide a range?

question

#1

I need to ask users to provide a range, in the form of Start:Step:End, from command line…
I tought to parse(StepRange,readline(()), but it doesn’t work:

print("Please specify a range in the form Start:Step:End (default to 20:1:25) ? ")
rawinput = readline()
rawinput = chomp(rawinput)
groups = []
if rawinput == ""
    groups = collect(20:1:25)
else
    groups = collect(parse(StepRange{Int64,Int64}, rawinput))
end
println(groups)

#2

got it…

print("Please specify a range in the form Start:Step:End (default to 20:1:25): ")
rawinput = readline()
rawinput = chomp(rawinput)
groups = []
if rawinput == ""
    groups = collect(20:1:25)
else
    groups= collect(eval(parse(rawinput)))
end
println(groups)

#3

eval feels unnecessary here. Can’t you just split on : and parse the left and right side as ints and then construct the range.


#4

Don’t ask them for input like this, just provide a function that they can call where they provide a range as the argument.


#5

I also think splitting on : (like Kristoffer proposed) is the better option. Remember, eval can be evil on user input…