CLI 应用程序
CLI 应用程序
Section titled “CLI 应用程序”构建读取输入、写入输出并与用户交互的命令行工具。
我们要构建什么
Section titled “我们要构建什么”一个问候用户的简单 CLI:
$ wippy run -x app:cliHello from CLI!cli-app/├── wippy.lock└── src/ ├── _index.yaml └── cli.lua步骤 1:创建项目
Section titled “步骤 1:创建项目”mkdir cli-app && cd cli-appmkdir src步骤 2:入口定义
Section titled “步骤 2:入口定义”创建 src/_index.yaml:
version: "1.0"namespace: app
entries: # Terminal host 将进程连接到 stdin/stdout - name: terminal kind: terminal.host lifecycle: auto_start: true
# CLI 进程 - name: cli kind: process.lua source: file://cli.lua method: main modules: - ioterminal.host 将你的 Lua 进程桥接到终端。没有它,io.print() 就没有地方写入。
步骤 3:CLI 代码
Section titled “步骤 3:CLI 代码”创建 src/cli.lua:
local io = require("io")
local function main() io.print("Hello from CLI!") return 0end
return { main = main }步骤 4:运行
Section titled “步骤 4:运行”wippy initwippy run -x app:cli输出:
Hello from CLI!-x 标志自动检测你的 terminal.host 并以静默模式运行以获得干净的输出。
读取用户输入
Section titled “读取用户输入”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 }使用 ANSI 转义码实现颜色:
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 模块访问运行时统计信息:
# 添加到入口定义modules: - 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 }可以将进程注册为命名命令,而不是使用 -x app:cli:
- name: cli kind: process.lua meta: command: name: greet short: Greet the user source: file://cli.lua method: main modules: - io现在按名称运行:
wippy run greet列出所有可用命令:
wippy run listAvailable commands: greet Greet the user从 main() 返回以设置退出码:
local function main() if error_occurred then return 1 -- 错误 end return 0 -- 成功endI/O 参考
Section titled “I/O 参考”| 函数 | 描述 |
|---|---|
io.print(...) | 写入 stdout 并换行 |
io.write(...) | 写入 stdout 不换行 |
io.eprint(...) | 写入 stderr 并换行 |
io.readline() | 从 stdin 读取一行 |
io.flush() | 刷新输出缓冲区 |
CLI 标志
Section titled “CLI 标志”| 标志 | 描述 |
|---|---|
wippy run -x app:cli | 运行 CLI 进程(自动检测 terminal.host) |
wippy run -x app:cli --host app:term | 显式指定 terminal host |
wippy run -x app:cli -v | 显示详细日志 |
- I/O Module - 完整的 I/O 参考
- System Module - 运行时和系统信息
- Echo Service - 多进程应用程序