[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正#284
[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正#284yuji38kwmt wants to merge 1 commit intomainfrom
Conversation
問題: - ネストされたサブコマンド(例: annoworkcli annofab visualize_statistics)で不正な引数を指定すると、 トップレベルのヘルプ(annoworkcli)が表示されていた - これはargparseのネストされたサブパーサーでのエラーハンドリングの問題によるもの 解決策: - parse_args()の代わりにparse_known_args()を使用 - 不正な引数が検出された場合、適切なサブコマンドのヘルプを表示するように修正 - サブパーサーにallow_abbrev=Falseを明示的に指定 修正ファイル: - annoworkcli/__main__.py: parse_known_args()を使用し、不正な引数のエラーハンドリングを実装 - annoworkcli/common/cli.py: add_parser()でallow_abbrev=Falseを明示的に指定
Title[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正 Description
Changes walkthrough 📝
|
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
| # stderrに出力するため、print_helpの出力先をstderrに変更 | ||
| args.command_help(sys.stderr) | ||
| # エラーメッセージのprog部分を構築 | ||
| prog_parts = ["annoworkcli", args.command_name] |
There was a problem hiding this comment.
Suggestion: プログラム名をハードコードするのではなく、parser.progを利用して動的に取得することで、一貫性を保ちやすくなります。 [general, importance: 6]
| prog_parts = ["annoworkcli", args.command_name] | |
| prog_parts = [parser.prog, args.command_name] |
| if hasattr(args, "subcommand_func"): | ||
| # サブコマンドのヘルプを表示してからエラーメッセージを出力 | ||
| # stderrに出力するため、print_helpの出力先をstderrに変更 | ||
| args.command_help(sys.stderr) |
There was a problem hiding this comment.
Suggestion: print_helpへの出力先を明示的に指定するため、キーワード引数を使ってfile=sys.stderrとしたほうが可読性と互換性が向上します。 [general, importance: 4]
| args.command_help(sys.stderr) | |
| args.command_help(file=sys.stderr) |
There was a problem hiding this comment.
Pull request overview
This PR fixes error handling for nested subcommands in the CLI. Previously, when invalid arguments were provided to nested subcommands (e.g., annoworkcli annofab visualize_statistics --invalid-arg), the top-level help was displayed instead of the subcommand-specific help. The changes switch from parse_args() to parse_known_args() and implement custom error handling to display the correct subcommand help.
Changes:
- Switched from
parse_args()toparse_known_args()in__main__.pyto detect unknown arguments - Added custom error handling logic to display appropriate subcommand help when unknown arguments are detected
- Explicitly set
allow_abbrev=Falseinadd_parser()for consistency
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| annoworkcli/main.py | Implemented parse_known_args() with custom error handling for unknown arguments in nested subcommands |
| annoworkcli/common/cli.py | Added explicit allow_abbrev=False to subparser creation |
| args.command_help(sys.stderr) | ||
| # エラーメッセージのprog部分を構築 | ||
| prog_parts = ["annoworkcli", args.command_name] | ||
| if hasattr(args, "subcommand_name") and args.subcommand_name: |
There was a problem hiding this comment.
subcommand_nameが空文字列の場合の扱いが不明確です。and args.subcommand_nameの条件により空文字列の場合は除外されますが、dest="subcommand_name"で設定される値は、サブコマンドが指定されない場合はNoneになるはずです。空文字列チェックが本当に必要かどうか検討してください。必要ない場合はif hasattr(args, "subcommand_name") and args.subcommand_name is not None:のように明示的にNoneチェックを行うことを推奨します。
| if hasattr(args, "subcommand_name") and args.subcommand_name: | |
| if hasattr(args, "subcommand_name") and args.subcommand_name is not None: |
| # 不正な引数が指定された場合は、適切なパーサーのエラーメッセージを表示 | ||
| if unknown_args: | ||
| # subcommand_funcが設定されている場合は、サブコマンドのパーサーでエラーを表示 | ||
| if hasattr(args, "subcommand_func"): | ||
| # サブコマンドのヘルプを表示してからエラーメッセージを出力 | ||
| # stderrに出力するため、print_helpの出力先をstderrに変更 | ||
| args.command_help(sys.stderr) | ||
| # エラーメッセージのprog部分を構築 | ||
| prog_parts = ["annoworkcli", args.command_name] | ||
| if hasattr(args, "subcommand_name") and args.subcommand_name: | ||
| prog_parts.append(args.subcommand_name) | ||
| prog = " ".join(prog_parts) | ||
| parser.exit(2, f"{prog}: error: unrecognized arguments: {' '.join(unknown_args)}\n") | ||
| else: | ||
| parser.error(f"unrecognized arguments: {' '.join(unknown_args)}") |
There was a problem hiding this comment.
不正な引数が指定された場合のエラーハンドリングロジックに対するテストカバレッジがありません。ネストされたサブコマンドでの不正な引数の処理、トップレベルコマンドでの不正な引数の処理をテストするケースを追加してください。例: annoworkcli annofab visualize_statistics --invalid-arg、annoworkcli --invalid-argなど。
|
検討中 |
問題:
解決策:
修正ファイル: