CLI contract
On this page
GoalsCompatibility GuaranteeExit Codesstdout / stderr RulesCompile Before Input ParsingTruthiness (-e/--exit-status)Argument Semantics-n / --null-inputBroken Pipe (SIGPIPE / EPIPE)ExamplesResolved Contract ItemsTest-backed GuaranteeSummaryこのページの目次
目的互換性の保証終了コード標準出力と標準エラー入力解析より先にコンパイル真偽値の扱い(-e/--exit-status)引数の意味-n / --null-inputパイプの切断(SIGPIPE / EPIPE)使用例実装済みの保証とテストこの文書は、jq-liteの安定した後方互換性を持つCLI仕様を定義します。記載された動作は実装済みで、自動テストにより保証・検証されます。破壊的変更にはメジャーバージョンの更新が必要です。
目的
- シェルスクリプトとCIで安定して使えること(
if jq-lite …; then …; fi)。 - 実用的な範囲でjqの慣習に従うこと。
- 終了コードと標準エラーの接頭辞でエラーを区別できること。
- エラー時に標準出力へ意図しない内容を出さないこと。
互換性の保証
このCLI仕様は安定しており、記載された動作の後方互換性を維持します。破壊的変更には、メジャーバージョンの更新、または明示的な互換性フラグが必要です。互換性フラグによる変更は推奨しません。
終了コード
| コード | 意味 |
|---|---|
| 0 | 成功 |
| 1 | -e/--exit-status指定時に結果がfalse、null、または空出力 |
| 2 | コンパイルエラー:フィルターの構文解析失敗 |
| 3 | 実行時エラー:評価失敗 |
| 4 | 入力エラー:入力の読み込み・デコード失敗 |
| 5 | 使用方法のエラー:不正な引数、--argjsonなど |
-eがなければ空出力も成功(0)です。-eが変更するのは終了コードだけで、標準出力の書式は変わりません。
標準出力と標準エラー
終了コード0または1では標準出力に結果が出る場合があります。エラー(2〜5)では標準出力は空でなければなりません。
エラー(2〜5)の診断は標準エラーに出力し、最初の行は以下の安定した接頭辞で始まります。
| 分類 | 接頭辞 | 終了コード |
|---|---|---|
| コンパイル | [COMPILE] |
2 |
| 実行時 | [RUNTIME] |
3 |
| 入力 | [INPUT] |
4 |
| 使用方法 | [USAGE] |
5 |
[COMPILE] unexpected token at …
[RUNTIME] type mismatch at …
[INPUT] failed to parse JSON input: …
[USAGE] invalid JSON for --argjson x
接頭辞以降は人間向けの診断例です。
入力解析より先にコンパイル
フィルターを入力より先にコンパイルします。フィルターと入力が両方不正なら、入力エラーではなくコンパイルエラーを報告します。
printf '{broken}\n' | jq-lite '.[ '
# 標準エラー: [COMPILE] …
# 終了コード: 2
真偽値の扱い(-e/--exit-status)
| 結果 | 終了コード |
|---|---|
| 真とみなす値 | 0 |
| false、null、空出力 | 1 |
jqと同様に、false、null、結果なしは偽です。その他の値(0、""、{}、[]など)は真です。
引数の意味
--arg name value
$nameに文字列として値を渡します。値が欠けていれば使用方法のエラー([USAGE]、5)です。
--argjson name json
JSONをデコードして$nameに渡します。1、"x"、true、nullなどのスカラーも許可します。不正なJSONは使用方法のエラー(5)です。
--argfile name file
ファイルをJSONとして読み込み、$nameに渡します。ファイルがない、読み込めない、またはJSONが不正なら使用方法のエラー(5)です。
-n / --null-input
標準入力を読み込まず、nullを入力として一度だけフィルターを評価します。終了コードと出力の通常の規則は変わりません。
jq-lite -n 'null'
# 標準出力: null
# 終了コード: 0
パイプの切断(SIGPIPE / EPIPE)
後続のコマンドがパイプを早く閉じる場合があります。
jq-lite '.[]' | head
SIGPIPE・EPIPEを致命的エラーとして扱いません。0で終了するか、-eの規則に従い、標準エラーに診断を出しません。よくあるパイプライン操作でスクリプトやCIが壊れるのを防ぎます。
使用例
コンパイルエラー
jq-lite '.[ '
# 標準エラー: [COMPILE] …
# 終了コード: 2
実行時エラー
printf '{"x":"a"}\n' | jq-lite '.x + 1'
# 標準エラー: [RUNTIME] …
# 終了コード: 3
入力エラー
printf '{broken}\n' | jq-lite '.'
# 標準エラー: [INPUT] …
# 終了コード: 4
-eで偽を返す場合
printf 'false\n' | jq-lite -e '.'
# 標準出力: false
# 終了コード: 1
実装済みの保証とテスト
入力解析より先のコンパイル、jqと同じ真偽判定、-n、-eによる終了コードだけの変更、パイプ切断時の正常終了はテストで保護されています。仕様への違反はCIの失敗として検出します。
prove -lv t/cli_contract.t
スクリプト、CI、下流ツールは、この文書化された予測可能な動作を利用できます。
This document defines the stable, fully implemented, backward-compatible CLI contract
for jq-lite.
All behaviors documented here are guaranteed, test-backed, and actively enforced by automated tests. Breaking changes require a major version bump.
Goals
- Shell scripts and CI must be stable (
if jq-lite …; then …; fi) - Follow
jqconventions where practical - Distinguish error categories via exit codes and stderr prefixes
- Keep stdout clean on errors (no unexpected stderr noise)
Compatibility Guarantee
- This CLI contract is stable
- Any behavior described here is backward-compatible
- Breaking changes require:
- a major version bump, or
- an explicit compatibility flag (discouraged)
Exit Codes
jq-lite returns one of the following exit codes:
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | -e/--exit-status specified and result is false, null, or empty output |
| 2 | Compile error (filter parse error) |
| 3 | Runtime error (evaluation failed) |
| 4 | Input error (failed to read or decode input) |
| 5 | Usage error (invalid CLI arguments, invalid --argjson, etc.) |
- Without
-e, empty output is considered success (exit 0) -eaffects only the exit code, not stdout formatting
stdout / stderr Rules
stdout
- On success (exit
0or1): output may appear on stdout - On errors (exit
2–5): stdout MUST remain empty
stderr
- On errors (exit
2–5), a diagnostic message is written to stderr - The first line MUST start with a stable prefix:
| Category | Prefix | Exit Code |
|---|---|---|
| Compile | [COMPILE] |
2 |
| Runtime | [RUNTIME] |
3 |
| Input | [INPUT] |
4 |
| Usage | [USAGE] |
5 |
Example:
[COMPILE] unexpected token at …
[RUNTIME] type mismatch at …
[INPUT] failed to parse JSON input: …
[USAGE] invalid JSON for --argjson x
Compile Before Input Parsing
Filter compilation MUST occur before input parsing.
If both the filter and the input are invalid, jq-lite MUST report a
compile error, not an input error:
printf '{broken}\n' | jq-lite '.[ '
# stderr: [COMPILE] …
# exit: 2
Input parsing errors MUST NOT mask compile errors.
Truthiness (-e/--exit-status)
When -e/--exit-status is specified:
| Result | Exit |
|---|---|
| truthy | 0 |
| false / null / empty | 1 |
Truthiness rules (jq-style):
false→ falseynull→ falsey- empty (no output) → falsey
- everything else (
0,"",{},[], etc.) → truthy
Argument Semantics
--arg name value
- Always binds
$nameas a string - Missing value → usage error (
[USAGE], exit 5)
--argjson name json
- Decodes
jsonas JSON and binds to$name - Scalar JSON values allowed:
1,"x",true,null - Invalid JSON → usage error (
[USAGE], exit 5)
--argfile name file
- Reads
file, decodes as JSON, and binds to$name - Missing or unreadable file → usage error (
[USAGE], exit 5) - Invalid JSON → usage error (
[USAGE], exit 5)
-n / --null-input
When -n is specified:
- stdin is not read
- the filter is evaluated once with
nullas input - normal exit code and output rules apply
Example:
jq-lite -n 'null'
# stdout: null
# exit: 0
Broken Pipe (SIGPIPE / EPIPE)
When downstream closes the pipe early:
jq-lite '.[]' | head
SIGPIPE/EPIPEMUST NOT be treated as a fatal errorjq-liteshould exit0(or follow-erules)- No diagnostic output MUST be printed to stderr
Rationale: This frequently occurs in pipelines and must not break scripts or CI.
Examples
Compile Error
jq-lite '.[ '
# stderr: [COMPILE] …
# exit: 2
Runtime Error
printf '{"x":"a"}\n' | jq-lite '.x + 1'
# stderr: [RUNTIME] …
# exit: 3
Input Error
printf '{broken}\n' | jq-lite '.'
# stderr: [INPUT] …
# exit: 4
-e falsey Result
printf 'false\n' | jq-lite -e '.'
# stdout: false
# exit: 1
Resolved Contract Items
The following contract items are fully implemented and covered by tests:
- Compile occurs before input parsing
-etruthiness fully matches jq (0is truthy)-n / --null-inputis supported-eaffects only exit code, not stdout format- Pipeline (broken pipe) handling prints no stderr and exits normally
Test-backed Guarantee
This contract is enforced by automated tests. Any violation will fail CI:
prove -lv t/cli_contract.t
Summary
jq-liteprovides a stable, predictable CLI- Compatibility is documented, intentional, and test-backed
- Scripts, CI, and downstream tools can rely on this behavior