Skip to content

Extract shared raw CLI hint scanner (#342)#364

Draft
leynos wants to merge 1 commit into
mainfrom
issue-342-shared-raw-hint-scanner
Draft

Extract shared raw CLI hint scanner (#342)#364
leynos wants to merge 1 commit into
mainfrom
issue-342-shared-raw-hint-scanner

Conversation

@leynos

@leynos leynos commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #342

locale_hint_from_args and diag_json_hint_from_args in src/cli_l10n.rs duplicated the raw argument grammar (-- terminator, --flag value, --flag=value, last-wins). This extracts a single lexical scanner so the locale and diagnostic-mode startup paths cannot diverge.

Changes

  • src/cli_l10n.rs: add scan_raw_hints(args, value_flags, bare_flags) -> RawArgHints plus small scan_value_flags/consume_flag_value helpers (flattened to satisfy the nesting threshold). Both public hint functions now delegate to it; value interpretation stays at the call sites as the issue suggests.
  • tests/cli_tests/locale.rs: five new edge-case tests — trailing valueless flag keeps the earlier value, -- after a value flag stops scanning without discarding state, a value flag greedily consumes a flag-like token (--output-format --diag-jsonNone), the last --output-format wins even when unrecognised, and a bare --diag-json survives an unrecognised format value.

All existing locale/diag-json hint tests pass unchanged, pinning behavioural equivalence.

Validation

  • make check-fmt / make lint / make test — pass (37 suites)

🤖 Generated with Claude Code

Summary by Sourcery

Extract a shared raw CLI argument scanner used by locale and diagnostic JSON hint detection to keep their startup parsing behavior consistent.

Enhancements:

  • Introduce a reusable lexical scanner for raw CLI hints that supports value flags, bare flags, and terminator handling, and use it in both locale and diagnostic-mode hint extraction.

Tests:

  • Add edge-case tests for locale and diagnostic JSON hint parsing to pin behavior around missing values, -- terminators, value precedence, and interaction between output format and diag-json flags.

`locale_hint_from_args` and `diag_json_hint_from_args` each
re-implemented the same raw argument grammar (`--` terminator,
`--flag value`, `--flag=value`, last-occurrence-wins), so a change to
one path could silently diverge from the other.

Introduce a single lexical scanner, `scan_raw_hints`, that records the
last value for requested value-taking flags and the presence of
requested bare flags. Both hint functions now delegate to it; value
interpretation (locale string, output-format mapping) stays at the
call sites. The scanner preserves the original greedy semantics: a
value flag consumes the following token even when it looks like a
flag, and a missing value or `--` terminator stops the scan.

Add focused edge-case tests: trailing valueless flags keep earlier
values, terminators stop scanning without discarding state, value
flags consume flag-like tokens, the last (even unrecognised)
`--output-format` wins, and a bare `--diag-json` survives an
unrecognised format value.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 81d9a468-86e1-4a4d-8324-1c84947e8604

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-342-shared-raw-hint-scanner

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai

sourcery-ai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Refactors CLI startup hint parsing by extracting a shared raw argument scanner for locale and diagnostic JSON detection, preserving existing behaviour while adding targeted edge-case tests to pin the new grammar.

File-Level Changes

Change Details Files
Introduce a shared raw argument scanning utility and data structure used by both locale and diagnostic JSON hint extraction.
  • Add RawArgHints struct holding last-seen values for value flags and presence of bare flags, with helper accessors
  • Implement scan_raw_hints to walk OsString args with support for -- terminator, value flags, inline --flag=value, last-wins semantics, and bare flags
  • Factor out scan_value_flags and consume_flag_value helpers plus ScanStep enum and RawArgIter alias to keep scanning logic flat and readable
src/cli_l10n.rs
Change locale and diag-json hint functions to use the shared scanner while keeping value interpretation at call sites.
  • Refactor locale_hint_from_args to delegate to scan_raw_hints with --locale as a value flag and return the captured value
  • Refactor diag_json_hint_from_args to delegate to scan_raw_hints with --output-format as a value flag and --diag-json as a bare flag, then compute the final bool via diag_json_hint_from_output_format and flag presence
src/cli_l10n.rs
Add regression and edge-case tests to pin the new raw scanning behaviour for locale and diag-json hints.
  • Add tests ensuring a trailing valueless --locale or --locale followed by -- does not discard earlier valid locale values
  • Add tests verifying a value flag greedily consumes following flag-like tokens as values, last --output-format wins even with invalid values, and a bare --diag-json flag still takes effect after an invalid output format
tests/cli_tests/locale.rs

Assessment against linked issues

Issue Objective Addressed Explanation
#342 Extract shared raw CLI hint scanning logic used by locale_hint_from_args and diag_json_hint_from_args into a small internal helper, while keeping value interpretation at the call sites.
#342 Ensure shared scanning logic consistently handles -- terminator, --flag value, and --flag=value forms, preserving existing behaviour for locale and diagnostic hints so existing tests continue to pass.
#342 Add focused tests for the shared scanner edge cases and ensure the test/lint/format suite passes.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

Extract shared raw CLI hint scanning logic

1 participant