Skip to content

🧹 [Refactor] Use set_defaults for argparse handlers in main.py#170

Open
badMade wants to merge 2 commits into
mainfrom
refactor/argparse-handlers-14372630581445231016
Open

🧹 [Refactor] Use set_defaults for argparse handlers in main.py#170
badMade wants to merge 2 commits into
mainfrom
refactor/argparse-handlers-14372630581445231016

Conversation

@badMade
Copy link
Copy Markdown
Owner

@badMade badMade commented May 11, 2026

🎯 What: Extracted the long if-elif chain in src/main.py:main() into isolated subcommand handler functions utilizing parser.set_defaults(func=...).

💡 Why: This drastically simplifies the dispatch logic within main(), improving maintainability and testability of each subcommand separately. Additionally, it delays the execution of common setup tasks (like build_port_manifest()) to only the handlers that explicitly require it, preventing unnecessary overhead for commands that don't need it.

Verification: Validated that syntax is correct (python3 -m py_compile), and confirmed that all 31 existing unittests pass reliably via PYTHONPATH=. python3 -m unittest discover tests. Furthermore, the code review tool evaluated the change and rated it as mostly correct (noting minor issues like temporary files, which were subsequently deleted).

Result: A cleaner, modularized main.py where adding new CLI tools follows a predictable and scalable pattern without extending a monolithic if/elif/else block.


PR created automatically by Jules for task 14372630581445231016 started by @badMade

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 18:59
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the CLI command handling in src/main.py by moving logic from a large conditional block in the main function into dedicated handler functions and utilizing argparse subparser defaults. Feedback was provided to improve input validation in handle_load_session by ensuring the session_id is not empty before use.

Comment thread src/main.py
Comment on lines +108 to +109
def handle_load_session(args: argparse.Namespace) -> int:
session = load_session(args.session_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-medium medium

The session_id identifier is used to construct a file path for loading session data. According to the general rules, identifiers used in file paths should be explicitly validated to reject empty strings. This prevents issues where an empty identifier might lead to accessing unintended files (e.g., .json). While validate_session_id (in session_store.py) handles path traversal, it does not currently reject empty strings, so adding a check here ensures robustness on this access path.

Suggested change
def handle_load_session(args: argparse.Namespace) -> int:
session = load_session(args.session_id)
def handle_load_session(args: argparse.Namespace) -> int:
if not args.session_id:
print('Error: session_id cannot be empty')
return 1
session = load_session(args.session_id)

Copy link
Copy Markdown

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

Refactors src/main.py’s CLI dispatch by replacing the long if/elif chain in main() with per-subcommand handler functions wired via argparse’s set_defaults(func=...), and defers expensive setup (like build_port_manifest()) to only the commands that need it.

Changes:

  • Added discrete handle_* functions for each CLI subcommand and registered them via parser.set_defaults(func=...).
  • Simplified main() to invoke the selected handler (args.func(args)) instead of branching on args.command.
  • Moved build_port_manifest() calls into only the relevant handlers (e.g., summary, manifest, subsystems).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main.py
manifest = build_port_manifest()
print(QueryEnginePort(manifest).render_summary())
return 0

Comment thread src/main.py
return 0 if result.handled else 1

def build_parser() -> argparse.ArgumentParser:

…CI issue

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@badMade badMade marked this pull request as ready for review May 12, 2026 17:46
Copilot AI review requested due to automatic review settings May 12, 2026 17:46
Copy link
Copy Markdown

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread src/main.py
if hasattr(args, 'func'):
return args.func(args)
parser.error(f'unknown command: {args.command}')
return 2
Comment on lines 33 to 37
// Wait up to 5 minutes for checks to complete
let allPassed = false;
for (let i = 0; i < 10; i++) {
await new Promise(r => setTimeout(r, 30000));

Comment on lines +55 to +56
console.log('Some checks failed.');
break;
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