feat(generator): [EnumValue] attribute for custom enum CLI strings + AOT-safe switch parsing#48
Merged
Merged
Conversation
…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>
9eb2086 to
f346c2b
Compare
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
[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-REDboth work)switchfor enum parsing instead ofEnum.TryParse+Enum.IsDefined, removing all reflection from enum handling — fully AOT-safeEnumMemberCliNames(parallel toEnumMemberNames,defaultwhen no[EnumValue]is used) flows throughParameterModeland is reflected in help text, completions, schema output, and default-value displaymemberName.ToLowerInvariant()— no behaviour change for existing codeDetails
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.EnumMemberCliNamesis only populated inParameterModelwhen at least one member carries[EnumValue]; otherwise all display paths fall back to the lowercased C# name viaResolveEnumMemberCliName.Test plan
EnumValueParseTests—fire-red,FIRE-RED,ocean-blue,greenall parse to the correct member; old identifier formfireredis rejected with exit 2EnumValueHelpTests— help shows<fire-red|ocean-blue|green>in the allowed-values line and--palette <enum>in the usage line🤖 Generated with Claude Code