Extract command-line JSON detection to shared argscan module#94
Merged
Conversation
Two cleanups from a codebase-wide simplification review: - Extract the raw-argv --json/-o json detection duplicated between main._command_line_requests_json and telemetry._notice_suppressed into a new rich-free, cycle-free aai_cli/argscan.py shared by both (telemetry could import neither main nor output), and register the module in the import-linter contracts. - Add AppState.resolve_api_key() alongside the existing resolve_profile/ resolve_environment/resolve_session methods, and route the 12 config.resolve_api_key(profile=state.profile) call sites across the command layer through it, so key resolution joins the other precedence rules in the one documented place. https://claude.ai/code/session_011nWq87bwQmisipjUNhjX99
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactor the command-line argument scanning logic into a dedicated
argscanmodule to eliminate code duplication and enable reuse across the codebase. This change extracts the--jsonflag detection logic that was previously private tomain.pyand makes it available to other modules liketelemetry.pythat need to sniff the raw token list before subcommand parsing.Key Changes
New module
aai_cli/argscan.py: Extractedrequests_json()function that detects JSON output flags (--json,-j,-o json,--output json, and their glued forms) from raw command-line arguments. This function is free of Rich and import cycles, making it safe for use across the codebase.Refactored
aai_cli/main.py:_command_line_requests_json()functionargscan.requests_json()_RAW_ARGS_META_KEYto explain the JSON detection patternconfigimportUpdated
aai_cli/telemetry.py:argscan.requests_json()_notice_suppressed()to combine quiet flag checks with the shared JSON detectionCentralized API key resolution in
aai_cli/context.py:AppState.resolve_api_key()method to encapsulate profile-aware API key resolutionUpdated command modules (
llm.py,transcripts.py,transcribe.py,agent.py,evaluate.py,speak.py,stream.py,login.py):config.resolve_api_key(profile=state.profile)calls withstate.resolve_api_key()configimports where applicableUpdated tests and documentation:
test_command_line_requests_json_*tests to useargscan.requests_jsonAGENTS.mdto referenceargscan.requests_jsoninstead of the private function.importlinterto includeaai_cli.argscanin the appropriate constraint layersImplementation Details
The
argscanmodule is intentionally minimal and dependency-free to avoid import cycles and Rich dependencies. This allows both the root callback (which runs before subcommand parsing) and telemetry's first-run notice to reliably detect JSON output requests from the raw token list without duplicating logic.The
AppState.resolve_api_key()method provides a cleaner API for commands to get the authenticated API key, centralizing the profile resolution logic and reducing boilerplate across command implementations.https://claude.ai/code/session_011nWq87bwQmisipjUNhjX99