Terminal I/O
Terminal I/O
Section titled “Terminal I/O”Read from stdin and write to stdout/stderr for CLI applications.
Loading
Section titled “Loading”local io = require("io")Writing to Stdout
Section titled “Writing to Stdout”Write strings to stdout without newline:
local ok, err = io.write("text", "more")| Parameter | Type | Description |
|---|---|---|
... | any | Variable number of values to write (coerced to string) |
Returns: boolean, error
Print with Newline
Section titled “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
Section titled “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
Section titled “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
Section titled “Reading a Line”Read a line from stdin up to newline:
local line, err = io.readline()Returns: string, error
Raw Mode
Section titled “Raw Mode”Enable or disable raw terminal mode (disables line buffering and echo):
local ok, err = io.raw(true) -- enablelocal ok, err = io.raw(false) -- disable| Parameter | Type | Description |
|---|---|---|
enable | boolean | true to enable, false to disable (default: true) |
Returns: boolean, error
Raw mode is reference-counted — each io.raw(true) must be matched by an io.raw(false). The terminal resets to normal mode automatically on process exit.
Flushing Output
Section titled “Flushing Output”Flush stdout buffer:
local ok, err = io.flush()Returns: boolean, error
Command Line Arguments
Section titled “Command Line Arguments”Get command line arguments:
local args = io.args()Returns: string[]
Errors
Section titled “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.