Skip to content

Commit b6c0d10

Browse files
stdlib_io: add Python-like input() function
1 parent 34723c7 commit b6c0d10

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

doc/specs/stdlib_io.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,52 @@ Read a whole line from a formatted unit into a string variable
246246
```fortran
247247
{!example/io/example_get_line.f90!}
248248
```
249+
## `input` — read a line from standard input
250+
251+
### Status
252+
253+
Experimental
254+
255+
### Description
256+
257+
Reads a line from standard input, optionally displaying a prompt.
258+
This is similar to Python’s `input()` function.
259+
260+
The function returns the input as an allocatable character string.
261+
Trailing spaces and tabs are preserved.
262+
No numeric conversion is performed.
263+
264+
### Syntax
265+
266+
`line = ` [[stdlib_io(module):input(function)]] `([prompt][, iostat][, iomsg])`
267+
268+
### Arguments
269+
270+
`prompt` (optional):
271+
A character expression containing a prompt to be displayed before reading input.
272+
273+
`iostat` (optional):
274+
Default integer, contains status of reading from standard input.
275+
Zero indicates success.
276+
277+
`iomsg` (optional):
278+
Deferred-length character variable containing an error message if `iostat` is non-zero.
279+
280+
### Return value
281+
282+
Returns a deferred-length allocatable `character` variable containing the input line.
283+
284+
### Notes
285+
286+
- Trailing spaces and tabs are preserved
287+
- No type conversion is performed
288+
- To convert to numbers, use `read(line, *)`
289+
290+
### Example
291+
292+
```fortran
293+
{!example/io/example_input.f90!}
294+
```
249295

250296
## Formatting constants
251297

example/io/example_input.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name = input("Enter your name: ")
2+
print *, "Hello:", name

src/stdlib_io.fypp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module stdlib_io
1616
implicit none
1717
private
1818
! Public API
19-
public :: loadtxt, savetxt, open, get_line, get_file
19+
public :: loadtxt, savetxt, open, get_line, get_file , input
2020

2121
!! version: experimental
2222
!!
@@ -82,6 +82,13 @@ module stdlib_io
8282
module procedure :: get_line_input_string
8383
end interface get_line
8484

85+
!> Version: experimental
86+
!>
87+
!> Read a line from standard input with an optional prompt
88+
interface input
89+
module procedure :: input_char
90+
end interface input
91+
8592
interface loadtxt
8693
!! version: experimental
8794
!!
@@ -597,6 +604,40 @@ contains
597604
call get_line(input_unit, line, iostat, iomsg)
598605
end subroutine get_line_input_char
599606

607+
!> Version: experimental
608+
!>
609+
!> Read a line from standard input with an optional prompt.
610+
!! Similar to Python's input().
611+
!!
612+
!! - Preserves trailing whitespace
613+
!! - Returns allocatable character string
614+
!! - Does not perform type conversion
615+
!! - Does not stop on error unless caller chooses to
616+
function input_char(prompt, iostat, iomsg) result(line)
617+
use, intrinsic :: iso_fortran_env, only : output_unit
618+
character(len=*), intent(in), optional :: prompt
619+
integer, intent(out), optional :: iostat
620+
character(len=:), allocatable, optional :: iomsg
621+
character(len=:), allocatable :: line
622+
623+
integer :: stat
624+
625+
! Print prompt without newline
626+
if (present(prompt)) then
627+
write(output_unit, '(a)', advance='no') prompt
628+
end if
629+
630+
! Read line from stdin
631+
call get_line_input_char(line, stat, iomsg)
632+
633+
if (present(iostat)) then
634+
iostat = stat
635+
else if (stat /= 0) then
636+
call error_stop("input: error reading from standard input")
637+
end if
638+
end function input_char
639+
640+
600641
!> Version: experimental
601642
!>
602643
!> Read a whole line from the standard input into a string variable

test/io/test_input.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
call write_test_input(" abc ")
2+
s = input()
3+
call assert_equal(s, " abc ")

0 commit comments

Comments
 (0)