Skip to content

fix(cli): require a subcommand in the CLI parser (#143) - #146

Merged
sergey-miryanov merged 2 commits into
sergey-miryanov:mainfrom
mhaye9545:fix/issue-143-require-subcommand
Sep 3, 2026
Merged

sergey-miryanov merged 2 commits into
sergey-miryanov:mainfrom
mhaye9545:fix/issue-143-require-subcommand

Conversation

@mhaye9545

Copy link
Copy Markdown
Contributor

Closes #143. Part of #142.

Summary

gcmon with no subcommand used to print an AttributeError traceback ('Namespace' object has no attribute 'verbose'), while gcmon 12345 exited 2 with an invalid-choice message. This PR makes the subcommand mandatory in the argparse parser, which produces a consistent usage message for both cases, and deletes the dead fallback dispatch code in main().

Changes

  1. Mandatory subcommand:
    • In src/gcmon/cli/main.py:_create_parser(), set required=True on parser.add_subparsers(dest="command", required=True, help="Available commands").
  2. Delete dead fallback code:
    • In src/gcmon/cli/main.py:main(), removed if hasattr(args, "func"), the dead fallback if args.command is None, and the logger.error("Unknown command: ...") block.
    • Replaced with direct return int(args.func(args)) since all subparsers (monitor, combine, run) define func defaults.
    • Removed unused module-level logger = logging.getLogger("gcmon").
  3. Tests (tests/test_cli.py):
    • test_main_no_subcommand_exits_2: asserts main([]) raises SystemExit with code == 2 and usage error naming the commands.
    • test_main_invalid_subcommand_exits_2: asserts main(["12345"]) raises SystemExit with code == 2 and invalid-choice error.
    • test_main_version_exits_0: asserts main(["--version"]) exits with code == 0.
    • test_main_subcommands_dispatch: pins that monitor, run, and combine subcommands dispatch to their handlers.
  4. CHANGELOG:
    • Added entry under ## WIP -> ### Bugfixes.

Verification

  • main([]) exits 2 with gcmon: error: the following arguments are required: command
  • main(["12345"]) exits 2 with gcmon: error: argument command: invalid choice: '12345'
  • main(["--version"]) exits 0
  • ruff check src tests passes cleanly.

@codspeed

codspeed Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 19 untouched benchmarks


Comparing mhaye9545:fix/issue-143-require-subcommand (f9ba5c6) with main (7685fce)

Open in CodSpeed

@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.25%. Comparing base (12fbd51) to head (f9ba5c6).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #146      +/-   ##
==========================================
+ Coverage   98.01%   98.25%   +0.23%     
==========================================
  Files          60       60              
  Lines        3323     3317       -6     
  Branches      452      450       -2     
==========================================
+ Hits         3257     3259       +2     
+ Misses         39       33       -6     
+ Partials       27       25       -2     
Files with missing lines Coverage Δ
src/gcmon/cli/main.py 93.10% <100.00%> (+11.85%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sergey-miryanov

Copy link
Copy Markdown
Owner

@mhaye9545 Could you please fix types in tests?

@mhaye9545

Copy link
Copy Markdown
Contributor Author

Fixed! Replaced the inline lambda callbacks in est_main_subcommands_dispatch with explicit typed mock functions, so mypy (strict) passes cleanly with zero issues.

@sergey-miryanov
sergey-miryanov merged commit 2c3d90e into sergey-miryanov:main Sep 3, 2026
17 checks passed
@sergey-miryanov

Copy link
Copy Markdown
Owner

Great! Thanks for your contribution!

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.

Require a subcommand in the CLI parser

2 participants