-
Notifications
You must be signed in to change notification settings - Fork 208
Description
Is there any interest to have a Python-like input() function?
Effectively, this would be a convenience function for the following pattern:
write(output_unit,'(A)',advance='no') 'Enter a value: '
read(input_unit,'(A)') buffer
str = trim(buffer)the purpose of which is to collect some user input together with a friendly prompt message. The Fortran interface could be something like:
impure function input(prompt,stat) result(str)
character(len=*), intent(in), optional :: prompt
!! Text that is displayed as a prompt.
integer, intent(out), optional :: stat
!! Status flag used to raise an exception.
character(len=:), allocatable :: str
end functionIn Python 2 they used to have two versions, input and raw_input. The first would try to evaluate the input argument and return it with the correct type, while raw_input would return a string. In Python 3 raw_input was removed and input returns a string. The user must then explicitly use eval or a type-conversion routine like int or float to get the desired value.
One issue I don't know how to deal with yet in Fortran are trailing whitespaces. The Python function preserves trailing whitespaces:
>>> s = input('Enter a value: ')
Enter a value: 4
>>> s
' 4 '
To achieve this in Fortran it might be necessary to interface with C.