mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-19 10:12:50 +08:00
41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
namespace Spectre.Console.Cli;
|
|
|
|
internal static class CommandPropertyBinder
|
|
{
|
|
public static CommandSettings CreateSettings(CommandValueLookup lookup, Type settingsType, ITypeResolver resolver)
|
|
{
|
|
var settings = CreateSettings(resolver, settingsType);
|
|
|
|
foreach (var (parameter, value) in lookup)
|
|
{
|
|
if (value != default)
|
|
{
|
|
parameter.Property.SetValue(settings, value);
|
|
}
|
|
}
|
|
|
|
// Validate the settings.
|
|
var validationResult = settings.Validate();
|
|
if (!validationResult.Successful)
|
|
{
|
|
throw CommandRuntimeException.ValidationFailed(validationResult);
|
|
}
|
|
|
|
return settings;
|
|
}
|
|
|
|
private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType)
|
|
{
|
|
if (resolver.Resolve(settingsType) is CommandSettings settings)
|
|
{
|
|
return settings;
|
|
}
|
|
|
|
if (Activator.CreateInstance(settingsType) is CommandSettings instance)
|
|
{
|
|
return instance;
|
|
}
|
|
|
|
throw CommandParseException.CouldNotCreateSettings(settingsType);
|
|
}
|
|
} |