-
Notifications
You must be signed in to change notification settings - Fork 208
Add input() convenience function to stdlib_io with documentation, tests, and example (Fixes #259) #1067
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
base: master
Are you sure you want to change the base?
Add input() convenience function to stdlib_io with documentation, tests, and example (Fixes #259) #1067
Conversation
There was a problem hiding this 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.
| 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 |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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:
- Read from a pre-populated file or pipe instead of stdin
- Test the prompt output to a redirected output
- Test error handling with closed units
- 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).
| ### 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) | ||
| ``` |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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(orstatdepending on the final parameter name): Optional integer output for the status (intent(out))- Return value: Deferred-length character string containing the input
|
|
||
| ### Syntax | ||
|
|
||
| `s = input(prompt [, iostat])` |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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.
| `s = input(prompt [, iostat])` | |
| `s = input(prompt [, stat])` |
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.
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.