mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-17 17:32:50 +08:00
Check for default value before writing property values
Previous version was overwriting values that might have been set via a property initializer. Closes #422
This commit is contained in:
parent
21f731ebd5
commit
ebb1076dd0
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using Spectre.Console.Cli;
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
@ -10,6 +11,12 @@ namespace Spectre.Console.Tests.Data
|
|||||||
public string Greeting { get; set; }
|
public string Greeting { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class OptionalArgumentWithPropertyInitializerSettings : CommandSettings
|
||||||
|
{
|
||||||
|
[CommandArgument(0, "[NAMES]")]
|
||||||
|
public string[] Names { get; set; } = Array.Empty<string>();
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class OptionalArgumentWithDefaultValueAndTypeConverterSettings : CommandSettings
|
public sealed class OptionalArgumentWithDefaultValueAndTypeConverterSettings : CommandSettings
|
||||||
{
|
{
|
||||||
[CommandArgument(0, "[GREETING]")]
|
[CommandArgument(0, "[GREETING]")]
|
||||||
|
@ -244,6 +244,50 @@ namespace Spectre.Console.Tests.Unit.Cli
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Assign_Property_Initializer_To_Optional_Argument()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandAppTester();
|
||||||
|
app.SetDefaultCommand<GenericCommand<OptionalArgumentWithPropertyInitializerSettings>>();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = app.Run(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ExitCode.ShouldBe(0);
|
||||||
|
result.Settings
|
||||||
|
.ShouldBeOfType<OptionalArgumentWithPropertyInitializerSettings>()
|
||||||
|
.And(settings => settings.Names.ShouldNotBeNull())
|
||||||
|
.And(settings => settings.Names.ShouldBeEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Overwrite_Property_Initializer_With_Argument_Value()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandAppTester();
|
||||||
|
app.SetDefaultCommand<GenericCommand<OptionalArgumentWithPropertyInitializerSettings>>();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = app.Run("ABBA", "Herreys");
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ExitCode.ShouldBe(0);
|
||||||
|
result.Settings
|
||||||
|
.ShouldBeOfType<OptionalArgumentWithPropertyInitializerSettings>()
|
||||||
|
.And(settings => settings.Names.ShouldContain("ABBA"))
|
||||||
|
.And(settings => settings.Names.ShouldContain("Herreys"));
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary()
|
public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary()
|
||||||
{
|
{
|
||||||
|
@ -9,9 +9,12 @@ namespace Spectre.Console.Cli
|
|||||||
var settings = CreateSettings(resolver, settingsType);
|
var settings = CreateSettings(resolver, settingsType);
|
||||||
|
|
||||||
foreach (var (parameter, value) in lookup)
|
foreach (var (parameter, value) in lookup)
|
||||||
|
{
|
||||||
|
if (value != default)
|
||||||
{
|
{
|
||||||
parameter.Property.SetValue(settings, value);
|
parameter.Property.SetValue(settings, value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Validate the settings.
|
// Validate the settings.
|
||||||
var validationResult = settings.Validate();
|
var validationResult = settings.Validate();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user