CLI Applications
CLI Applications
Section titled “CLI Applications”Build command-line tools that read input, write output, and interact with users.
What We’re Building
Section titled “What We’re Building”A simple CLI that greets the user:
$ wippy run -x app:cliHello from CLI!Project Structure
Section titled “Project Structure”cli-app/├── wippy.lock└── src/ ├── _index.yaml └── cli.luaStep 1: Create Project
Section titled “Step 1: Create Project”mkdir cli-app && cd cli-appmkdir srcStep 2: Entry Definitions
Section titled “Step 2: Entry Definitions”Create src/_index.yaml:
version: "1.0"namespace: app
entries: # Terminal host connects processes to stdin/stdout - name: terminal kind: terminal.host lifecycle: auto_start: true
# CLI process - name: cli kind: process.lua source: file://cli.lua method: main modules: - ioterminal.host bridges your Lua process to the terminal. Without it, io.print() has nowhere to write.
Step 3: CLI Code
Section titled “Step 3: CLI Code”Create src/cli.lua:
local io = require("io")
local function main() io.print("Hello from CLI!") return 0end
return { main = main }Step 4: Run It
Section titled “Step 4: Run It”wippy initwippy run -x app:cliOutput:
Hello from CLI!-x flag auto-detects your terminal.host and runs in silent mode for clean output.
Reading User Input
Section titled “Reading User Input”local io = require("io")
local function main() io.write("Enter your name: ") local name = io.readline()
if name and #name > 0 then io.print("Hello, " .. name .. "!") else io.print("Hello, stranger!") end
return 0end
return { main = main }Colored Output
Section titled “Colored Output”Use ANSI escape codes for colors:
local io = require("io")
local reset = "\027[0m"local function red(s) return "\027[31m" .. s .. reset endlocal function green(s) return "\027[32m" .. s .. reset endlocal function yellow(s) return "\027[33m" .. s .. reset endlocal function cyan(s) return "\027[36m" .. s .. reset endlocal function bold(s) return "\027[1m" .. s .. reset end
local function main() io.print(bold(cyan("Welcome!"))) io.write(yellow("Enter a number: "))
local input = io.readline() local n = tonumber(input)
if n then io.print("Squared: " .. green(tostring(n * n))) return 0 else io.print(red("Error: ") .. "not a number") return 1 endend
return { main = main }System Information
Section titled “System Information”Access runtime stats with the system module:
# Add to entry definitionmodules: - io - systemlocal io = require("io")local system = require("system")
local function main() io.print("Host: " .. system.process.hostname()) io.print("CPUs: " .. system.runtime.cpu_count()) io.print("Goroutines: " .. system.runtime.goroutines())
local mem = system.memory.stats() io.print("Memory: " .. string.format("%.1f MB", mem.heap_alloc / 1024 / 1024))
return 0end
return { main = main }Named Commands
Section titled “Named Commands”Instead of using -x app:cli, you can register your process as a named command:
- name: cli kind: process.lua meta: command: name: greet short: Greet the user source: file://cli.lua method: main modules: - ioNow run it by name:
wippy run greetList all available commands:
wippy run listAvailable commands: greet Greet the userExit Codes
Section titled “Exit Codes”Return from main() to set the exit code:
local function main() if error_occurred then return 1 -- Error end return 0 -- SuccessendI/O Reference
Section titled “I/O Reference”| Function | Description |
|---|---|
io.print(...) | Write to stdout with newline |
io.write(...) | Write to stdout without newline |
io.eprint(...) | Write to stderr with newline |
io.readline() | Read line from stdin |
io.flush() | Flush output buffer |
CLI Flags
Section titled “CLI Flags”| Flag | Description |
|---|---|
wippy run -x app:cli | Run CLI process (auto-detects terminal.host) |
wippy run -x app:cli --host app:term | Explicit terminal host |
wippy run -x app:cli -v | With verbose logging |
Next Steps
Section titled “Next Steps”- I/O Module - Complete I/O reference
- System Module - Runtime and system info
- Echo Service - Multi-process applications