🧹 [Refactor] Use set_defaults for argparse handlers in main.py#170
🧹 [Refactor] Use set_defaults for argparse handlers in main.py#170badMade wants to merge 2 commits into
Conversation
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
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.
| def handle_load_session(args: argparse.Namespace) -> int: | ||
| session = load_session(args.session_id) |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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 viaparser.set_defaults(func=...). - Simplified
main()to invoke the selected handler (args.func(args)) instead of branching onargs.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.
| manifest = build_port_manifest() | ||
| print(QueryEnginePort(manifest).render_summary()) | ||
| return 0 | ||
|
|
| 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>
| if hasattr(args, 'func'): | ||
| return args.func(args) | ||
| parser.error(f'unknown command: {args.command}') | ||
| return 2 |
| // 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)); | ||
|
|
| console.log('Some checks failed.'); | ||
| break; |
🎯 What: Extracted the long
if-elifchain insrc/main.py:main()into isolated subcommand handler functions utilizingparser.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 (likebuild_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 viaPYTHONPATH=. 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.pywhere adding new CLI tools follows a predictable and scalable pattern without extending a monolithicif/elif/elseblock.PR created automatically by Jules for task 14372630581445231016 started by @badMade