Skip to content

Commit f2284c8

Browse files
stdlib_io: add Python-like input() function Fixes #259
1 parent b6c0d10 commit f2284c8

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

doc/specs/stdlib_io.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,17 @@ No numeric conversion is performed.
268268
### Arguments
269269

270270
`prompt` (optional):
271-
A character expression containing a prompt to be displayed before reading input.
271+
A character expression containing a prompt to be displayed before reading input.
272+
This argument is `intent(in)`.
272273

273274
`iostat` (optional):
274275
Default integer, contains status of reading from standard input.
275276
Zero indicates success.
277+
This argument is `intent(out)`.
276278

277279
`iomsg` (optional):
278280
Deferred-length character variable containing an error message if `iostat` is non-zero.
281+
This argument is `intent(out)`.
279282

280283
### Return value
281284

example/io/example_input.f90

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
name = input("Enter your name: ")
2-
print *, "Hello:", name
1+
program example_input
2+
use stdlib_io, only : input
3+
implicit none
4+
5+
character(len=:), allocatable :: name
6+
7+
name = input("Enter your name: ")
8+
print *, "Hello:", name
9+
10+
end program example_input

src/stdlib_io.fypp

Lines changed: 1 addition & 2 deletions
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 , input
19+
public :: loadtxt, savetxt, open, get_line, get_file, input
2020

2121
!! version: experimental
2222
!!
@@ -637,7 +637,6 @@ contains
637637
end if
638638
end function input_char
639639

640-
641640
!> Version: experimental
642641
!>
643642
!> Read a whole line from the standard input into a string variable

test/io/test_input.f90

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,85 @@
1-
call write_test_input(" abc ")
2-
s = input()
3-
call assert_equal(s, " abc ")
1+
module test_input
2+
use testdrive, only : new_unittest, unittest_type, error_type, assert_equal
3+
use stdlib_io, only : input
4+
use stdlib_test_utils, only : write_test_input
5+
implicit none
6+
private
7+
public :: collect
8+
9+
contains
10+
11+
subroutine collect(tests)
12+
type(unittest_type), allocatable, intent(out) :: tests(:)
13+
14+
tests = [ &
15+
new_unittest("input preserves whitespace", test_input_whitespace), &
16+
new_unittest("input with prompt", test_input_prompt), &
17+
new_unittest("input with iostat", test_input_iostat), &
18+
new_unittest("input with iomsg", test_input_iomsg), &
19+
new_unittest("input without optional args", test_input_no_args) &
20+
]
21+
end subroutine collect
22+
23+
24+
subroutine test_input_whitespace(error)
25+
type(error_type), allocatable, intent(out) :: error
26+
character(len=:), allocatable :: s
27+
28+
call write_test_input(" abc ")
29+
s = input()
30+
call assert_equal(error, s, " abc ")
31+
end subroutine test_input_whitespace
32+
33+
34+
subroutine test_input_prompt(error)
35+
type(error_type), allocatable, intent(out) :: error
36+
character(len=:), allocatable :: s
37+
38+
call write_test_input("abc")
39+
s = input("Enter value: ")
40+
call assert_equal(error, s, "abc")
41+
end subroutine test_input_prompt
42+
43+
44+
subroutine test_input_iostat(error)
45+
type(error_type), allocatable, intent(out) :: error
46+
character(len=:), allocatable :: s
47+
integer :: ios
48+
49+
call write_test_input("abc")
50+
s = input(iostat=ios)
51+
call assert_equal(error, ios, 0)
52+
call assert_equal(error, s, "abc")
53+
end subroutine test_input_iostat
54+
55+
56+
subroutine test_input_iomsg(error)
57+
type(error_type), allocatable, intent(out) :: error
58+
character(len=:), allocatable :: s
59+
character(len=:), allocatable :: msg
60+
61+
call write_test_input("abc")
62+
s = input(iomsg=msg)
63+
call assert_equal(error, s, "abc")
64+
end subroutine test_input_iomsg
65+
66+
67+
subroutine test_input_no_args(error)
68+
type(error_type), allocatable, intent(out) :: error
69+
character(len=:), allocatable :: s
70+
71+
call write_test_input("abc")
72+
s = input()
73+
call assert_equal(error, s, "abc")
74+
end subroutine test_input_no_args
75+
76+
end module test_input
77+
78+
79+
program run_test_input
80+
use testdrive, only : run_testsuite
81+
use test_input, only : collect
82+
implicit none
83+
84+
call run_testsuite(collect)
85+
end program run_test_input

0 commit comments

Comments
 (0)