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

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)

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)

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

1 Like

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

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

1 Like