Skip to content

Extract command-line JSON detection to shared argscan module#94

Merged
alexkroman merged 2 commits into
mainfrom
claude/laughing-curie-mz15ac
Jun 12, 2026
Merged

Extract command-line JSON detection to shared argscan module#94
alexkroman merged 2 commits into
mainfrom
claude/laughing-curie-mz15ac

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

Summary

Refactor the command-line argument scanning logic into a dedicated argscan module to eliminate code duplication and enable reuse across the codebase. This change extracts the --json flag detection logic that was previously private to main.py and makes it available to other modules like telemetry.py that need to sniff the raw token list before subcommand parsing.

Key Changes

  • New module aai_cli/argscan.py: Extracted requests_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:

    • Removed private _command_line_requests_json() function
    • Updated all call sites to use argscan.requests_json()
    • Improved docstring for _RAW_ARGS_META_KEY to explain the JSON detection pattern
    • Removed unused config import
  • Updated aai_cli/telemetry.py:

    • Replaced duplicated JSON detection logic with call to argscan.requests_json()
    • Simplified _notice_suppressed() to combine quiet flag checks with the shared JSON detection
  • Centralized API key resolution in aai_cli/context.py:

    • Added AppState.resolve_api_key() method to encapsulate profile-aware API key resolution
    • Updated docstring to reflect the expanded responsibility
  • Updated command modules (llm.py, transcripts.py, transcribe.py, agent.py, evaluate.py, speak.py, stream.py, login.py):

    • Replaced config.resolve_api_key(profile=state.profile) calls with state.resolve_api_key()
    • Removed unused config imports where applicable
  • Updated tests and documentation:

    • Migrated test_command_line_requests_json_* tests to use argscan.requests_json
    • Updated AGENTS.md to reference argscan.requests_json instead of the private function
    • Updated .importlinter to include aai_cli.argscan in the appropriate constraint layers

Implementation Details

The argscan module 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

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
@alexkroman alexkroman enabled auto-merge (squash) June 12, 2026 02:46
@alexkroman alexkroman merged commit c2e365c into main Jun 12, 2026
8 checks passed
@alexkroman alexkroman deleted the claude/laughing-curie-mz15ac branch June 12, 2026 02:52
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