Skip to content

Conversation

@Brijesh-Thakkar
Copy link

Adds a minimal Python-like input(prompt, stat) function to stdlib_io.
Reads a full line, optional prompt + optional stat, follows stdlib error semantics, no parsing.
Includes docs, tests, example, and CMake updates.
Small, safe, ergonomic addition aligned with stdlib goals.
Ready for review.

Copilot AI review requested due to automatic review settings December 8, 2025 20:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a Python-like input() convenience function to stdlib_io for reading user input from standard input with an optional prompt. The implementation reuses the existing get_line_input_char infrastructure and follows the module's error handling patterns. The addition includes documentation, an example program, and basic compilation tests.

Key Changes

  • New input(prompt, stat) function that displays an optional prompt and reads a line from stdin
  • Example program demonstrating both simple usage and error handling with status checking
  • Documentation section describing the new function with syntax and usage examples

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/stdlib_io.fypp Adds input_character function implementation and interface, plus general whitespace cleanup throughout the file
test/io/test_input.f90 Adds basic compilation test for the new function
test/io/CMakeLists.txt Registers the new test in the build system
example/io/example_input.f90 Provides example demonstrating simple and error-checked usage
example/io/CMakeLists.txt Builds the example executable (not run as test due to interactive nature)
doc/specs/stdlib_io.md Documents the new function with status, description, syntax, and examples
.gitignore Adds .venv/ for Python virtual environment directories

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 22 to 37
subroutine test_input_compilation(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error

! Check if we can reference the function pointer
! This ensures the interface is correct and the symbol is available
! procedure(input_interface), pointer :: ptr => null()
! ptr => input
! call check(error, associated(ptr))

! Simple check that we can call the user stub (which calls input)
! We don't actually run it to avoid blocking, but just referencing it
! ensures it was compiled and linked.
call check(error, .true.)

end subroutine test_input_compilation
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inadequate test coverage: The test only verifies compilation and does not test actual functionality of the input() function. While interactive input testing can be challenging, consider adding tests that:

  1. Read from a pre-populated file or pipe instead of stdin
  2. Test the prompt output to a redirected output
  3. Test error handling with closed units
  4. Test that trailing whitespace is preserved as documented

Other similar functions like get_line have comprehensive functional tests (see test/io/test_get_line.f90).

Copilot uses AI. Check for mistakes.
Comment on lines 256 to 273
### Description

`input(prompt)` displays `prompt` (if provided) on the same line and returns the full user input as a string. Trailing spaces and tabs are preserved. Optionally the call can provide an `iostat`-like integer to capture the status.

### Syntax

`s = input(prompt [, iostat])`

### Example

```fortran
use stdlib_io, only: input
character(len=:), allocatable :: name
integer :: st
name = input('Enter your name: ')
name = input('Enter name (with status): ', st)
```
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Arguments section: The documentation should include an ### Arguments section describing the parameters, similar to other functions in this file (e.g., get_line). This should document:

  • prompt: Optional character input for the prompt message (intent(in))
  • iostat (or stat depending on the final parameter name): Optional integer output for the status (intent(out))
  • Return value: Deferred-length character string containing the input

Copilot uses AI. Check for mistakes.

### Syntax

`s = input(prompt [, iostat])`
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter name inconsistency: The documentation refers to the parameter as iostat but the implementation uses stat. These should match for clarity. Consider updating the documentation to use stat to match the implementation, or update the implementation to use iostat to match the documentation and align with standard Fortran conventions.

Suggested change
`s = input(prompt [, iostat])`
`s = input(prompt [, stat])`

Copilot uses AI. Check for mistakes.
This commit adds a minimal Python-like input(prompt, iostat) convenience function to stdlib_io.

Files changed:
- src/stdlib_io.fypp: new generic interface and input_character implementation
- doc/specs/stdlib_io.md: documentation, Arguments and examples
- test/io/test_input.f90: compilation + CI-safe functional tests
- test/io/CMakeLists.txt: register input tests
- example/io/example_input.f90: example

Behavior:
- Optional prompt printed without newline
- Trailing whitespace preserved
- Optional iostat returns 0 on success, non-zero on EOF/I/O error
- If iostat not provided, follows stdlib error semantics and calls error_stop on error

This fixes Issue fortran-lang#259.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant