CLIアプリケーション
CLIアプリケーション
Section titled “CLIアプリケーション”入力を読み取り、出力を書き込み、ユーザーと対話するコマンドラインツールを構築します。
構築するもの
Section titled “構築するもの”ユーザーに挨拶するシンプルなCLI:
$ wippy run -x app:cliHello from CLI!プロジェクト構造
Section titled “プロジェクト構造”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: # ターミナルホストはプロセスを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 }システム情報
Section titled “システム情報”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 }名前付きコマンド
Section titled “名前付きコマンド”-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 usermain()から戻り値で終了コードを設定:
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から1行読み取り |
io.flush() | 出力バッファをフラッシュ |
CLIフラグ
Section titled “CLIフラグ”| フラグ | 説明 |
|---|---|
wippy run -x app:cli | CLIプロセスを実行(terminal.hostを自動検出) |
wippy run -x app:cli --host app:term | 明示的なターミナルホスト |
wippy run -x app:cli -v | 詳細ログ付き |
次のステップ
Section titled “次のステップ”- I/Oモジュール - 完全なI/Oリファレンス
- Systemモジュール - ランタイムとシステム情報
- Echoサービス - マルチプロセスアプリケーション