Skip to content

fix(generator): non-nullable [AsParameters] properties with defaults not required#40

Merged
Mpdreamz merged 1 commit into
mainfrom
fix/non-nullable-with-defaults
May 4, 2026
Merged

fix(generator): non-nullable [AsParameters] properties with defaults not required#40
Mpdreamz merged 1 commit into
mainfrom
fix/non-nullable-with-defaults

Conversation

@Mpdreamz
Copy link
Copy Markdown
Contributor

@Mpdreamz Mpdreamz commented May 4, 2026

Summary

  • FromAsParametersInitProperty was computing required from the property type's nullability alone — it never checked for a C# property initializer (= value). A non-nullable property like public FixtureSeverity Severity { get; set; } = FixtureSeverity.Information; was treated as a required CLI flag. Fixed by calling TryGetOptionsPropertyDefaultLiteral and applying && defaultValueLiteral is null, mirroring the existing FromOptionsProperty path.
  • EmitParseAndAssign called EmitParseFromString unconditionally when DefaultValueLiteral is null, passing a null raw expression into type-specific parsers (e.g. Enum.TryParse) when an optional flag was absent. This caused a spurious Error: invalid value for --foo: ''. and caused TryParseArgh to return false early — meaning subsequent flags (like --skip-private-repositories) were never applied. Fixed by wrapping the parse in if (rawExpr is not null) for the optional-no-default case.

Test plan

  • 3 new facts in ParseErrors/ParseErrorTests.cs verify exit 0 when omitting non-nullable-with-default flags across all three shapes: global options property, [AsParameters] DTO property, method parameter
  • 2 new facts in Binding/AsParametersAndBindingTests.cs verify the correct default values are bound (Information enum, 20 int)
  • Full integration suite: 134/135 pass (1 unrelated parallelism timeout that passes in isolation)
  • Unit test suite: 83/83 pass

🤖 Generated with Claude Code

…not required

Two bugs caused non-nullable options-type properties with C# initializers
(e.g. `public LogLevel Severity { get; set; } = LogLevel.Information;`) to
be treated as required CLI flags:

1. `FromAsParametersInitProperty` computed `required` from the property type's
   nullability alone, never checking for a property initializer. Fixed by
   calling `TryGetOptionsPropertyDefaultLiteral` and applying `&& defaultValueLiteral is null`,
   mirroring the existing global-options path in `FromOptionsProperty`.

2. `EmitParseAndAssign` called `EmitParseFromString` unconditionally when
   `DefaultValueLiteral is null`, passing a null raw expression into parsers
   like `Enum.TryParse` when the flag was absent. Fixed by wrapping the parse
   in `if (rawExpr is not null)` for the optional-no-default case.

Tests added for all three parameter shapes: global options properties,
[AsParameters] DTO properties, and method parameters.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@Mpdreamz Mpdreamz merged commit f51cd28 into main May 4, 2026
4 checks passed
@Mpdreamz Mpdreamz deleted the fix/non-nullable-with-defaults branch May 4, 2026 20:49
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