CLI 애플리케이션
CLI 애플리케이션
섹션 제목: “CLI 애플리케이션”입력을 읽고, 출력을 쓰고, 사용자와 상호작용하는 커맨드라인 도구를 빌드합니다.
만들 것
섹션 제목: “만들 것”사용자에게 인사하는 간단한 CLI:
$ wippy run -x app:cliHello from CLI!프로젝트 구조
섹션 제목: “프로젝트 구조”cli-app/├── wippy.lock└── src/ ├── _index.yaml └── cli.lua1단계: 프로젝트 생성
섹션 제목: “1단계: 프로젝트 생성”mkdir cli-app && cd cli-appmkdir src2단계: 엔트리 정의
섹션 제목: “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 코드
섹션 제목: “3단계: CLI 코드”src/cli.lua 생성:
local io = require("io")
local function main() io.print("Hello from CLI!") return 0end
return { main = main }4단계: 실행
섹션 제목: “4단계: 실행”wippy initwippy run -x app:cli출력:
Hello from CLI!-x 플래그는 terminal.host를 자동 감지하고 깨끗한 출력을 위해 사일런트 모드로 실행합니다.
사용자 입력 읽기
섹션 제목: “사용자 입력 읽기”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 참조
섹션 제목: “I/O 참조”| 함수 | 설명 |
|---|---|
io.print(...) | 개행과 함께 stdout에 쓰기 |
io.write(...) | 개행 없이 stdout에 쓰기 |
io.eprint(...) | 개행과 함께 stderr에 쓰기 |
io.readline() | stdin에서 줄 읽기 |
io.flush() | 출력 버퍼 플러시 |
CLI 플래그
섹션 제목: “CLI 플래그”| 플래그 | 설명 |
|---|---|
wippy run -x app:cli | CLI 프로세스 실행 (terminal.host 자동 감지) |
wippy run -x app:cli --host app:term | 명시적 터미널 호스트 |
wippy run -x app:cli -v | 상세 로깅 포함 |