-
Notifications
You must be signed in to change notification settings - Fork 208
stdlib_io: add Python-like input() function #1070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b6c0d10
f2284c8
92516a4
fb7572b
53a495d
82430b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| name = input("Enter your name: ") | ||
| print *, "Hello:", name | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ module stdlib_io | |
| implicit none | ||
| private | ||
| ! Public API | ||
| public :: loadtxt, savetxt, open, get_line, get_file | ||
| public :: loadtxt, savetxt, open, get_line, get_file , input | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| !! version: experimental | ||
| !! | ||
|
|
@@ -82,6 +82,13 @@ module stdlib_io | |
| module procedure :: get_line_input_string | ||
| end interface get_line | ||
|
|
||
| !> Version: experimental | ||
| !> | ||
| !> Read a line from standard input with an optional prompt | ||
| interface input | ||
| module procedure :: input_char | ||
| end interface input | ||
|
|
||
| interface loadtxt | ||
| !! version: experimental | ||
| !! | ||
|
|
@@ -597,6 +604,40 @@ contains | |
| call get_line(input_unit, line, iostat, iomsg) | ||
| end subroutine get_line_input_char | ||
|
|
||
| !> Version: experimental | ||
| !> | ||
| !> Read a line from standard input with an optional prompt. | ||
| !! Similar to Python's input(). | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| !! | ||
| !! - Preserves trailing whitespace | ||
| !! - Returns allocatable character string | ||
| !! - Does not perform type conversion | ||
| !! - Does not stop on error unless caller chooses to | ||
|
||
| function input_char(prompt, iostat, iomsg) result(line) | ||
| use, intrinsic :: iso_fortran_env, only : output_unit | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| character(len=*), intent(in), optional :: prompt | ||
| integer, intent(out), optional :: iostat | ||
| character(len=:), allocatable, optional :: iomsg | ||
| character(len=:), allocatable :: line | ||
|
|
||
| integer :: stat | ||
|
|
||
| ! Print prompt without newline | ||
| if (present(prompt)) then | ||
| write(output_unit, '(a)', advance='no') prompt | ||
| end if | ||
|
|
||
| ! Read line from stdin | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| call get_line_input_char(line, stat, iomsg) | ||
|
|
||
| if (present(iostat)) then | ||
| iostat = stat | ||
| else if (stat /= 0) then | ||
| call error_stop("input: error reading from standard input") | ||
| end if | ||
| end function input_char | ||
|
|
||
|
|
||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| !> Version: experimental | ||
| !> | ||
| !> Read a whole line from the standard input into a string variable | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| call write_test_input(" abc ") | ||
| s = input() | ||
| call assert_equal(s, " abc ") | ||
Brijesh-Thakkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.