Skip to content

[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正#284

Open
yuji38kwmt wants to merge 1 commit intomainfrom
202601154-2
Open

[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正#284
yuji38kwmt wants to merge 1 commit intomainfrom
202601154-2

Conversation

@yuji38kwmt
Copy link
Copy Markdown
Collaborator

問題:

  • ネストされたサブコマンド(例: 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を明示的に指定

問題:
- ネストされたサブコマンド(例: 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を明示的に指定
Copilot AI review requested due to automatic review settings January 15, 2026 00:18
@kci-pr-agent
Copy link
Copy Markdown
Contributor

kci-pr-agent Bot commented Jan 15, 2026

Title

[AI生成] ネストされたサブコマンドで不正な引数を指定した際に、正しいサブコマンドのヘルプを表示するように修正


Description

  • パーサーをparse_known_args()へ置換

  • サブコマンド不正引数時にヘルプ表示&エラー処理

  • allow_abbrev=Falseを全パーサーに適用


Changes walkthrough 📝

Relevant files
Error handling
__main__.py
不正引数時のヘルプ表示改善                                                                                       

annoworkcli/main.py

  • ArgumentParser に prog="annoworkcli" 追加
  • parse_args()parse_known_args() に置換
  • 不正引数検出時、サブコマンドヘルプ表示&エラー出力
  • エラーメッセージの prog 構築&exit制御
  • +21/-3   
    Configuration changes
    cli.py
    サブパーサーでallow_abbrev無効化                                                                     

    annoworkcli/common/cli.py

    • add_parser()allow_abbrev=False を追加
    +1/-0     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @kci-pr-agent
    Copy link
    Copy Markdown
    Contributor

    kci-pr-agent Bot commented Jan 15, 2026

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    sysモジュール未インポート

    sys.stderrを使用していますが、sysモジュールがインポートされていないとNameErrorになります。ファイル先頭にimport sysがあるか確認してください。

    # stderrに出力するため、print_helpの出力先をstderrに変更
    args.command_help(sys.stderr)
    エラー処理の一貫性

    サブコマンド用エラー表示でparser.exit、トップレベルでparser.errorを使い分けています。メッセージフォーマットや終了コードが混在しないよう、統一した方法を検討してください。

        parser.exit(2, f"{prog}: error: unrecognized arguments: {' '.join(unknown_args)}\n")
    else:
        parser.error(f"unrecognized arguments: {' '.join(unknown_args)}")

    Comment thread annoworkcli/__main__.py
    # stderrに出力するため、print_helpの出力先をstderrに変更
    args.command_help(sys.stderr)
    # エラーメッセージのprog部分を構築
    prog_parts = ["annoworkcli", args.command_name]
    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggestion: プログラム名をハードコードするのではなく、parser.progを利用して動的に取得することで、一貫性を保ちやすくなります。 [general, importance: 6]

    Suggested change
    prog_parts = ["annoworkcli", args.command_name]
    prog_parts = [parser.prog, args.command_name]

    Comment thread annoworkcli/__main__.py
    if hasattr(args, "subcommand_func"):
    # サブコマンドのヘルプを表示してからエラーメッセージを出力
    # stderrに出力するため、print_helpの出力先をstderrに変更
    args.command_help(sys.stderr)
    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggestion: print_helpへの出力先を明示的に指定するため、キーワード引数を使ってfile=sys.stderrとしたほうが可読性と互換性が向上します。 [general, importance: 4]

    Suggested change
    args.command_help(sys.stderr)
    args.command_help(file=sys.stderr)

    Copy link
    Copy Markdown
    Contributor

    Copilot AI left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    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() to parse_known_args() in __main__.py to detect unknown arguments
    • Added custom error handling logic to display appropriate subcommand help when unknown arguments are detected
    • Explicitly set allow_abbrev=False in add_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

    Comment thread annoworkcli/__main__.py
    args.command_help(sys.stderr)
    # エラーメッセージのprog部分を構築
    prog_parts = ["annoworkcli", args.command_name]
    if hasattr(args, "subcommand_name") and args.subcommand_name:
    Copy link

    Copilot AI Jan 15, 2026

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    subcommand_nameが空文字列の場合の扱いが不明確です。and args.subcommand_nameの条件により空文字列の場合は除外されますが、dest="subcommand_name"で設定される値は、サブコマンドが指定されない場合はNoneになるはずです。空文字列チェックが本当に必要かどうか検討してください。必要ない場合はif hasattr(args, "subcommand_name") and args.subcommand_name is not None:のように明示的にNoneチェックを行うことを推奨します。

    Suggested change
    if hasattr(args, "subcommand_name") and args.subcommand_name:
    if hasattr(args, "subcommand_name") and args.subcommand_name is not None:

    Copilot uses AI. Check for mistakes.
    Comment thread annoworkcli/__main__.py
    Comment on lines +94 to +108
    # 不正な引数が指定された場合は、適切なパーサーのエラーメッセージを表示
    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)}")
    Copy link

    Copilot AI Jan 15, 2026

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    不正な引数が指定された場合のエラーハンドリングロジックに対するテストカバレッジがありません。ネストされたサブコマンドでの不正な引数の処理、トップレベルコマンドでの不正な引数の処理をテストするケースを追加してください。例: annoworkcli annofab visualize_statistics --invalid-argannoworkcli --invalid-argなど。

    Copilot uses AI. Check for mistakes.
    @yuji38kwmt
    Copy link
    Copy Markdown
    Collaborator Author

    検討中

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants