Skip to content

Commit 4dbc15e

Browse files
committed
修复可空结构类型的默认值赋值错误
1 parent 1fa84eb commit 4dbc15e

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/DotNetCampus.CommandLine.Analyzer/Generators/BuilderGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,12 @@ private string GenerateOptionPropertyAssignment(OptionPropertyGeneratingModel pr
122122
};
123123
var exception = property.IsRequired
124124
? $"throw new global::DotNetCampus.Cli.Exceptions.RequiredPropertyNotAssignedException($\"The command line arguments doesn't contain a required option '{property.GetDisplayCommandOption()}'. Command line: {{commandLine}}\", \"{property.PropertyName}\")"
125-
: property.IsValueType
126-
? "default"
127-
: "null!";
125+
: (property.IsNullable, property.IsValueType) switch
126+
{
127+
(true, _) => "null",
128+
(false, true) => "default",
129+
(false, false) => "null!",
130+
};
128131

129132
var getters = property.GenerateAllNames(
130133
shortOption => $"""commandLine.GetShortOption("{shortOption}"{caseSensitive})""",
@@ -178,9 +181,12 @@ private string GenerateValuePropertyAssignment(CommandObjectGeneratingModel mode
178181
var verbText = model.VerbName is { } verbName ? $"\"{verbName}\"" : "null";
179182
var exception = property.IsRequired
180183
? $"throw new global::DotNetCampus.Cli.Exceptions.RequiredPropertyNotAssignedException($\"The command line arguments doesn't contain a required positional argument at {property.Index ?? 0}. Command line: {{commandLine}}\", \"{property.PropertyName}\")"
181-
: property.IsValueType
182-
? "default"
183-
: "null!";
184+
: (property.IsNullable, property.IsValueType) switch
185+
{
186+
(true, _) => "null",
187+
(false, true) => "default",
188+
(false, false) => "null!",
189+
};
184190
if (property.IsRequired || property.IsInitOnly)
185191
{
186192
return $"""

0 commit comments

Comments
 (0)