Skip to content

feat(generator): [EnumValue] attribute for custom enum CLI strings + AOT-safe switch parsing#48

Merged
Mpdreamz merged 1 commit into
mainfrom
feat/enum-value-attribute
May 7, 2026
Merged

feat(generator): [EnumValue] attribute for custom enum CLI strings + AOT-safe switch parsing#48
Mpdreamz merged 1 commit into
mainfrom
feat/enum-value-attribute

Conversation

@Mpdreamz
Copy link
Copy Markdown
Contributor

@Mpdreamz Mpdreamz commented May 7, 2026

Summary

  • Adds [EnumValue("cli-string")] attribute (AttributeTargets.Field) so consumers can override the CLI string for individual enum members (e.g. FireRed--palette fire-red, --palette FIRE-RED both work)
  • The generator now always emits a pre-generated switch for enum parsing instead of Enum.TryParse + Enum.IsDefined, removing all reflection from enum handling — fully AOT-safe
  • EnumMemberCliNames (parallel to EnumMemberNames, default when no [EnumValue] is used) flows through ParameterModel and is reflected in help text, completions, schema output, and default-value display
  • Members without the attribute continue to use memberName.ToLowerInvariant() — no behaviour change for existing code

Details

The switch is keyed on (rawValue ?? "").ToLowerInvariant() for case-insensitive matching. A bool sentinel (__evp_) tracks parse success to avoid any nullable struct dereference warnings.

EnumMemberCliNames is only populated in ParameterModel when at least one member carries [EnumValue]; otherwise all display paths fall back to the lowercased C# name via ResolveEnumMemberCliName.

Test plan

  • EnumValueParseTestsfire-red, FIRE-RED, ocean-blue, green all parse to the correct member; old identifier form firered is rejected with exit 2
  • EnumValueHelpTests — help shows <fire-red|ocean-blue|green> in the allowed-values line and --palette <enum> in the usage line
  • All 221 existing tests continue to pass (no regression to unannotated enum parsing)

🤖 Generated with Claude Code

…always-switch enum parsing

Adds `[EnumValue("cli-string")]` on enum fields to override the string users
type on the command line (e.g. `FireRed` → `--palette fire-red`). Members
without the attribute continue to use their lowercased C# name.

The generator now always emits a pre-generated switch for enum parsing instead
of `Enum.TryParse` + `Enum.IsDefined`, eliminating all reflection from enum
handling and making it fully AOT-safe. The switch is keyed on
`(rawValue ?? "").ToLowerInvariant()` so matching remains case-insensitive.

`EnumMemberCliNames` (parallel to `EnumMemberNames`, only populated when any
member carries the attribute) flows through `ParameterModel` and is used in
help text, completions, schema, and default-value display. When absent the
generator falls back to `memberName.ToLowerInvariant()` for display, preserving
existing behaviour for unannotated enums.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@Mpdreamz Mpdreamz force-pushed the feat/enum-value-attribute branch from 9eb2086 to f346c2b Compare May 7, 2026 08:15
@Mpdreamz Mpdreamz merged commit d6c93ba into main May 7, 2026
4 checks passed
@Mpdreamz Mpdreamz deleted the feat/enum-value-attribute branch May 7, 2026 08:17
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.

1 participant