Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Spectre.Console.Cli.Tests/CommandAppTests.Pairs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public sealed class ReadOnlyDictionarySettings : CommandSettings
public IReadOnlyDictionary<string, string>? Values { get; set; }
}

public sealed class DefaultValuesSettings : CommandSettings
{
[CommandOption("--var <VALUE>")]
[DefaultValue(new[] { "Monday=1", "Tuesday=2" })]
public IReadOnlyDictionary<DayOfWeek, int>? Values { get; set; }
}

public sealed class StringIntDeconstructor : PairDeconstructor<string, string>
{
protected override (string Key, string Value) Deconstruct(string? value)
Expand Down Expand Up @@ -312,5 +319,30 @@ public void Should_Map_ReadOnly_Dictionary_Values()
pair.Values["baz"].ShouldBe("qux");
});
}

[Fact]
public void Should_Map_Default_Values()
{
// Given
var app = new CommandAppTester();
app.SetDefaultCommand<GenericCommand<DefaultValuesSettings>>();
app.Configure(config =>
{
config.PropagateExceptions();
});

// When
var result = app.Run();

// Then
result.ExitCode.ShouldBe(0);
result.Settings.ShouldBeOfType<DefaultValuesSettings>().And(pair =>
{
pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(2);
pair.Values[DayOfWeek.Monday].ShouldBe(1);
pair.Values[DayOfWeek.Tuesday].ShouldBe(2);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ private object GetLookup(CommandParameter parameter, ITypeResolver resolver, obj
}

// Deconstruct and add to multimap.
var pair = deconstructor.Deconstruct(resolver, genericTypes[0], genericTypes[1], value as string);
if (pair.Key != null)
foreach (var text in value as IEnumerable<string?> ?? [ value as string ])
{
multimap.Add(pair);
var pair = deconstructor.Deconstruct(resolver, genericTypes[0], genericTypes[1], text);
if (pair.Key != null)
{
multimap.Add(pair);
}
}

return multimap;
Expand Down