CLI-Anything OpenRefine 是做什麼的?
當工作需要 OpenRefine 進行資料清理、轉換、可重複使用的操作歷史,或從自動化代理工作流程匯出 CSV/TSV 時,請使用此技能。
前置需求
安裝 Harness:
cd openrefine/agent-harness
python -m pip install -e .
在執行後端指令前,先啟動 OpenRefine:
openrefine -i 127.0.0.1 -p 3333
設定自訂伺服器位址,可透過 OPENREFINE_URL=http://127.0.0.1:3333 環境變數或 --base-url 參數傳遞。
代理指令規則
- 在每個一次性指令上優先使用
--json。 - 使用
--session <path>來隔離任務狀態。 - 在執行列、套用、匯出、還原或重做指令前,必須先匯入或開啟專案。
- 現有的 OpenRefine 操作歷史 JSON 可以直接傳遞給
data apply指令。 - 產生的檔案為一般的 OpenRefine 操作 JSON 以及匯出的 CSV/TSV 資料。
常用指令
cli-anything-openrefine --json server ping
cli-anything-openrefine --json project list
cli-anything-openrefine --json --session run/session.json project import messy.csv --name cleanup
cli-anything-openrefine --json --session run/session.json data rows --limit 10
cli-anything-openrefine --json ops text-transform run/trim.json --column Name --expression 'value.trim()'
cli-anything-openrefine --json --session run/session.json data apply run/trim.json
cli-anything-openrefine --json --session run/session.json data export run/clean.csv --format csv
cli-anything-openrefine --json --session run/session.json session undo
cli-anything-openrefine --json --session run/session.json session redo
REPL
執行 cli-anything-openrefine(不帶任何子指令)即可進入 REPL 模式。
對於自動化的使用者流程,可以透過 stdin 傳遞以換行分隔的 REPL 指令。
重新導向的串流會自動使用僅包含 ASCII 的輸入/輸出路徑,而互動式終端機則保留 Unicode 橫幅、指令歷史以及樣式:
printf 'help\nexit\n' | cli-anything-openrefine
當 REPL 被重新導向時,Unicode 內容會以可逆的 ASCII 反斜線跳脫序列呈現,避免傳統 Windows 輸出編碼將成功的指令轉變為失敗的旅程。
在 exit 或檔案結尾處,如果有任何指令失敗,管線工作流程會回傳非零的結束狀態,讓 CI 系統能夠偵測到不成功的使用者旅程。
錯誤處理
當設定了 --json 時,指令失敗會將一個包含 ok: false 的 JSON 物件寫入 stderr。