Terminal I/O
Read from stdin and write to stdout/stderr for CLI applications.
Loading
local io = require("io")
Writing to Stdout
Write strings to stdout without newline:
local ok, err = io.write("text", "more")
| Parameter | Type | Description |
|---|---|---|
... |
string | Variable number of strings to write |
Returns: boolean, error
Print with Newline
Write values to stdout with tabs between and newline at end:
io.print("value1", "value2", 123)
| Parameter | Type | Description |
|---|---|---|
... |
any | Variable number of values to print |
Returns: boolean, error
Writing to Stderr
Write values to stderr with tabs between and newline at end:
io.eprint("Error:", message)
| Parameter | Type | Description |
|---|---|---|
... |
any | Variable number of values to print |
Returns: boolean, error
Reading Bytes
Read up to n bytes from stdin:
local data, err = io.read(1024)
| Parameter | Type | Description |
|---|---|---|
n |
integer | Number of bytes to read (default: 1024, values <= 0 become 1024) |
Returns: string, error
Reading a Line
Read a line from stdin up to newline:
local line, err = io.readline()
Returns: string, error
Flushing Output
Flush stdout buffer:
local ok, err = io.flush()
Returns: boolean, error
Command Line Arguments
Get command line arguments:
local args = io.args()
Returns: string[]
Errors
| Condition | Kind | Retryable |
|---|---|---|
| No terminal context | errors.UNAVAILABLE |
no |
| Write operation failed | errors.INTERNAL |
no |
| Read operation failed | errors.INTERNAL |
no |
| Flush operation failed | errors.INTERNAL |
no |
See Error Handling for working with errors.