fix: 存在しないサブコマンドへの --help をエラー扱いにして exit 1 を返す#147
Conversation
Commander は --help/-h を検出すると、残りの operand が既知のサブコマンドか 検証せずにヘルプを表示して exit 0 していた (例: geonic hello --help)。 サブコマンドを持つコマンドの outputHelp をラップし、未解決の operand が 残っている場合は 'geonic help <unknown>' と同じエラー形式で exit 1 にする。
- operand 判定を cmd.args 先頭トークンに限定し、未知オプションの値を サブコマンド名と誤認しないようにする (--bogus json --help 等)。 副作用として --help が先行する場合 (geonic --help hello) は 従来どおりヘルプ表示に戻る - エラーメッセージはユーザーが入力したエイリアス (models, batch) を そのまま echo し、正規名に置換しない (rawArgs から復元) - unknown-command エラー出力を unknownCommandError() に統合し、 showHelp との重複を解消 - テストの spy ボイラープレートを runExpectingError/runExpectingHelp に 集約し、既存テストも移行
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough未知サブコマンドに対する Changes未知サブコマンドのヘルプ処理
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant createProgram
participant enforceKnownCommandHelp
participant Commander
participant unknownCommandError
createProgram->>enforceKnownCommandHelp: プログラムへ検証処理を登録
createProgram->>Commander: argv を解析して --help を処理
Commander->>enforceKnownCommandHelp: 未解決オペランドを確認
enforceKnownCommandHelp->>unknownCommandError: コマンドパスを渡してエラー終了
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed: private package registry requires authentication. Disable ESLint in CodeRabbit settings or use public packages. Comment |
rawArgs の文字列スキャンは、グローバルオプション (--service 等) の値が コマンド名/エイリアスと衝突すると誤ったトークンを echo していた (例: geonic --service custom-data-models models badsub --help が 'custom-data-models badsub' と表示)。 preSubcommand フックで dispatch 時に commander 自身が解決した operand (parent.args[0]) を WeakMap に記録する方式へ変更。オプション値は parseOptions で消費済みのため衝突しない。あわせて: - 空文字 operand を unknown 扱いしない (truthiness ガード) - 回帰テスト 3 件追加 (オプション値衝突 / サブグループで --help 先行 / 空文字 operand)
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/help.test.ts (1)
21-37: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winspy の復元を
finallyで保証してください。parse や assertion が失敗すると
mockRestore()に到達せず、後続テストの stdout、stderr、process.exitが汚染されます。両ヘルパーの復元処理をtry/finallyに移してください。Also applies to: 697-711
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/help.test.ts` around lines 21 - 37, Update runExpectingError and the other helper at the referenced location so console.error and process.exit spies are always restored in a finally block. Keep parsing, output collection, and assertions within the protected try section, ensuring restoration occurs even when parsing or an assertion fails.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/help.test.ts`:
- Around line 21-37: Update runExpectingError and the other helper at the
referenced location so console.error and process.exit spies are always restored
in a finally block. Keep parsing, output collection, and assertions within the
protected try section, ensuring restoration occurs even when parsing or an
assertion fails.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 86b5e5a6-9563-4176-a201-3dbef35b1e24
📒 Files selected for processing (4)
CHANGELOG.mdsrc/cli.tssrc/commands/help.tstests/help.test.ts
|
@coderabbitai 対応しました (4fbde3e)。 |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
git log --oneline -5
git show 4fbde3e --stat
git show 4fbde3e -- tests/help.test.ts | head -150Length of output: 2969 Confirmed. The commit 4fbde3e correctly moves the spy restoration into Thanks for the quick fix! |
* test: models.feature の @wip を解除し一意制約の E2E シナリオを追加 (geonicdb#1268 追従) - @wip の理由「モデル作成に tenantId が必要」は e2e hooks の e2e_test テナント + tenant_admin 整備で解消済みのため全シナリオを有効化 - get/delete シナリオの ID 指定を Mongo id → type に修正 (API はモデルを type でキーする) - 新規シナリオ: 制約宣言 → models get table の可読表示 → 重複エンティティ exit 1 + 違反制約名 + ヒント → [] で全削除後に重複可 - devDependency geonicdb を一意制約実装済み main (913ceecf) に更新 * docs: CHANGELOG の PR 番号を修正 (#147 → #148)
概要
geonic hello --helpのような存在しないサブコマンドに対するヘルプ実行が、トップレベルのヘルプを表示して exit 0 で成功扱いになっていた。エラーメッセージを stderr に出力し、exit 1 を返すようにする。原因
Commander は
--help/-hを検出すると、残りの operand が既知のサブコマンドか検証する前に_outputHelpIfRequestedでヘルプを表示して exit 0 する。ルート・グループ配下(geonic entities hello --help)の両方で発生していた。実装
enforceKnownCommandHelp()を追加し、サブコマンドを持つ全コマンドのoutputHelpをラップ。未知のサブコマンド名が operand に残っている場合はgeonic help <unknown>と同じエラー形式で exit 1cmd.argsの先頭トークンのみ(commander は operands を unknown オプションより前に並べるため、未知オプションの値を誤ってサブコマンド名と判定しない)models badsub→'models badsub'。正規名custom-data-modelsに置換しない)unknownCommandError()に統合し showHelp と共有挙動
geonic hello --helpError: 'hello' is not a geonic command./ exit 1geonic entities hello --helpError: 'entities hello' .../ exit 1geonic models badsub --helpError: 'models badsub' .../ exit 1geonic entities --bogus json --helpgeonic --help/entities --help等の正常系テスト
Summary by CodeRabbit
--helpまたは-hを指定した場合、ヘルプ表示のみで成功終了していた挙動を修正し、エラーメッセージ出力のうえ終了コード 1 に変更。