This commit is contained in:
Tyrrrz
2024-06-16 02:16:43 +03:00
parent 3fc7054f80
commit 034d3cec66
17 changed files with 85 additions and 440 deletions

View File

@@ -4,6 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ApplicationIcon>../favicon.ico</ApplicationIcon> <ApplicationIcon>../favicon.ico</ApplicationIcon>
<PublishAot>true</PublishAot>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -23,8 +23,6 @@ public class CliApplication(
ITypeActivator typeActivator ITypeActivator typeActivator
) )
{ {
private readonly CommandBinder _commandBinder = new(typeActivator);
/// <summary> /// <summary>
/// Application metadata. /// Application metadata.
/// </summary> /// </summary>
@@ -116,7 +114,7 @@ public class CliApplication(
try try
{ {
// Bind the command input to the command instance // Bind the command input to the command instance
_commandBinder.Bind(commandInput, commandSchema, commandInstance); commandInstance.Bind(commandSchema, commandInput);
// Handle the version option // Handle the version option
if (commandInstance is ICommandWithVersionOption { IsVersionRequested: true }) if (commandInstance is ICommandWithVersionOption { IsVersionRequested: true })

View File

@@ -1,389 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using CliFx.Exceptions;
using CliFx.Extensibility;
using CliFx.Infrastructure;
using CliFx.Input;
using CliFx.Schema;
using CliFx.Utils.Extensions;
namespace CliFx;
internal class CommandBinder(ITypeActivator typeActivator)
{
private readonly IFormatProvider _formatProvider = CultureInfo.InvariantCulture;
private object? ConvertSingle(InputSchema inputSchema, string? rawValue, Type targetType)
{
// Custom converter
if (inputSchema.Converter is not null)
{
return inputSchema.Converter.Convert(rawValue);
}
// Assignable from a string (e.g. string itself, object, etc)
if (targetType.IsAssignableFrom(typeof(string)))
{
return rawValue;
}
// Special case for bool
if (targetType == typeof(bool))
{
return string.IsNullOrWhiteSpace(rawValue) || bool.Parse(rawValue);
}
// Special case for DateTimeOffset
if (targetType == typeof(DateTimeOffset))
{
// Null reference exception will be handled upstream
return DateTimeOffset.Parse(rawValue!, _formatProvider);
}
// Special case for TimeSpan
if (targetType == typeof(TimeSpan))
{
// Null reference exception will be handled upstream
return TimeSpan.Parse(rawValue!, _formatProvider);
}
// Enum
if (targetType.IsEnum)
{
// Null reference exception will be handled upstream
return Enum.Parse(targetType, rawValue!, true);
}
// Convertible primitives (int, double, char, etc)
if (targetType.Implements(typeof(IConvertible)))
{
return Convert.ChangeType(rawValue, targetType, _formatProvider);
}
// Nullable<T>
var nullableUnderlyingType = targetType.TryGetNullableUnderlyingType();
if (nullableUnderlyingType is not null)
{
return !string.IsNullOrWhiteSpace(rawValue)
? ConvertSingle(inputSchema, rawValue, nullableUnderlyingType)
: null;
}
// String-constructable (FileInfo, etc)
var stringConstructor = targetType.GetConstructor([typeof(string)]);
if (stringConstructor is not null)
{
return stringConstructor.Invoke([rawValue]);
}
// String-parseable (with IFormatProvider)
var parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true);
if (parseMethodWithFormatProvider is not null)
{
return parseMethodWithFormatProvider.Invoke(null, [rawValue, _formatProvider]);
}
// String-parseable (without IFormatProvider)
var parseMethod = targetType.TryGetStaticParseMethod();
if (parseMethod is not null)
{
return parseMethod.Invoke(null, [rawValue]);
}
throw CliFxException.InternalError(
$"""
{inputSchema.GetKind()} {inputSchema.GetFormattedIdentifier()} has an unsupported underlying property type.
There is no known way to convert a string value into an instance of type `{targetType.FullName}`.
To fix this, either change the property to use a supported type or configure a custom converter.
"""
);
}
private object? ConvertMultiple(
InputSchema inputSchema,
IReadOnlyList<string> rawValues,
Type targetEnumerableType,
Type targetElementType
)
{
var array = rawValues
.Select(v => ConvertSingle(inputSchema, v, targetElementType))
.ToNonGenericArray(targetElementType);
var arrayType = array.GetType();
// Assignable from an array (T[], IReadOnlyList<T>, etc)
if (targetEnumerableType.IsAssignableFrom(arrayType))
{
return array;
}
// Array-constructable (List<T>, HashSet<T>, etc)
var arrayConstructor = targetEnumerableType.GetConstructor([arrayType]);
if (arrayConstructor is not null)
{
return arrayConstructor.Invoke([array]);
}
throw CliFxException.InternalError(
$"""
{inputSchema.GetKind()} {inputSchema.GetFormattedIdentifier()} has an unsupported underlying property type.
There is no known way to convert an array of `{targetElementType.FullName}` into an instance of type `{targetEnumerableType.FullName}`.
To fix this, change the property to use a type which can be assigned from an array or a type which has a constructor that accepts an array.
"""
);
}
private object? ConvertMember(InputSchema inputSchema, IReadOnlyList<string> rawValues)
{
try
{
// Non-scalar
var enumerableUnderlyingType =
inputSchema.Property.Type.TryGetEnumerableUnderlyingType();
if (enumerableUnderlyingType is not null && inputSchema.Property.Type != typeof(string))
{
return ConvertMultiple(
inputSchema,
rawValues,
inputSchema.Property.Type,
enumerableUnderlyingType
);
}
// Scalar
if (rawValues.Count <= 1)
{
return ConvertSingle(
inputSchema,
rawValues.SingleOrDefault(),
inputSchema.Property.Type
);
}
}
catch (Exception ex) when (ex is not CliFxException) // don't wrap CliFxException
{
// We use reflection-based invocation which can throw TargetInvocationException.
// Unwrap those exceptions to provide a more user-friendly error message.
var errorMessage = ex is TargetInvocationException invokeEx
? invokeEx.InnerException?.Message ?? invokeEx.Message
: ex.Message;
throw CliFxException.UserError(
$"""
{inputSchema.GetKind()} {inputSchema.GetFormattedIdentifier()} cannot be set from the provided argument(s):
{rawValues.Select(v => '<' + v + '>').JoinToString(" ")}
Error: {errorMessage}
""",
ex
);
}
// Mismatch (scalar but too many values)
throw CliFxException.UserError(
$"""
{inputSchema.GetKind()} {inputSchema.GetFormattedIdentifier()} expects a single argument, but provided with multiple:
{rawValues.Select(v => '<' + v + '>').JoinToString(" ")}
"""
);
}
private void ValidateMember(InputSchema inputSchema, object? convertedValue)
{
var errors = new List<BindingValidationError>();
foreach (var validatorType in inputSchema.Validators)
{
var validator = typeActivator.CreateInstance<IBindingValidator>(validatorType);
var error = validator.Validate(convertedValue);
if (error is not null)
errors.Add(error);
}
if (errors.Any())
{
throw CliFxException.UserError(
$"""
{inputSchema.GetKind()} {inputSchema.GetFormattedIdentifier()} has been provided with an invalid value.
Error(s):
{errors.Select(e => "- " + e.Message).JoinToString(Environment.NewLine)}
"""
);
}
}
private void BindMember(
InputSchema inputSchema,
ICommand commandInstance,
IReadOnlyList<string> rawValues
)
{
var convertedValue = ConvertMember(inputSchema, rawValues);
ValidateMember(inputSchema, convertedValue);
inputSchema.Property.SetValue(commandInstance, convertedValue);
}
private void BindParameters(
CommandInput commandInput,
CommandSchema commandSchema,
ICommand commandInstance
)
{
// Ensure there are no unexpected parameters and that all parameters are provided
var remainingParameterInputs = commandInput.Parameters.ToList();
var remainingRequiredParameterSchemas = commandSchema
.Parameters.Where(p => p.IsRequired)
.ToList();
var position = 0;
foreach (var parameterSchema in commandSchema.Parameters.OrderBy(p => p.Order))
{
// Break when there are no remaining inputs
if (position >= commandInput.Parameters.Count)
break;
// Scalar: take one input at the current position
if (parameterSchema.Property.IsScalar())
{
var parameterInput = commandInput.Parameters[position];
BindMember(parameterSchema, commandInstance, [parameterInput.Value]);
position++;
remainingParameterInputs.Remove(parameterInput);
}
// Non-scalar: take all remaining inputs starting from the current position
else
{
var parameterInputs = commandInput.Parameters.Skip(position).ToArray();
BindMember(
parameterSchema,
commandInstance,
parameterInputs.Select(p => p.Value).ToArray()
);
position += parameterInputs.Length;
remainingParameterInputs.RemoveRange(parameterInputs);
}
remainingRequiredParameterSchemas.Remove(parameterSchema);
}
if (remainingParameterInputs.Any())
{
throw CliFxException.UserError(
$"""
Unexpected parameter(s):
{remainingParameterInputs.Select(p => p.GetFormattedIdentifier()).JoinToString(" ")}
"""
);
}
if (remainingRequiredParameterSchemas.Any())
{
throw CliFxException.UserError(
$"""
Missing required parameter(s):
{remainingRequiredParameterSchemas
.Select(p => p.GetFormattedIdentifier())
.JoinToString(" ")}
"""
);
}
}
private void BindOptions(
CommandInput commandInput,
CommandSchema commandSchema,
ICommand commandInstance
)
{
// Ensure there are no unrecognized options and that all required options are set
var remainingOptionInputs = commandInput.Options.ToList();
var remainingRequiredOptionSchemas = commandSchema
.Options.Where(o => o.IsRequired)
.ToList();
foreach (var optionSchema in commandSchema.Options)
{
var optionInputs = commandInput
.Options.Where(o => optionSchema.MatchesIdentifier(o.Identifier))
.ToArray();
var environmentVariableInput = commandInput.EnvironmentVariables.FirstOrDefault(e =>
optionSchema.MatchesEnvironmentVariable(e.Name)
);
// Direct input
if (optionInputs.Any())
{
var rawValues = optionInputs.SelectMany(o => o.Values).ToArray();
BindMember(optionSchema, commandInstance, rawValues);
// Required options need at least one value to be set
if (rawValues.Any())
remainingRequiredOptionSchemas.Remove(optionSchema);
}
// Environment variable
else if (environmentVariableInput is not null)
{
var rawValues = optionSchema.IsSequence
? [environmentVariableInput.Value]
: environmentVariableInput.SplitValues();
BindMember(optionSchema, commandInstance, rawValues);
// Required options need at least one value to be set
if (rawValues.Any())
remainingRequiredOptionSchemas.Remove(optionSchema);
}
// No input, skip
else
{
continue;
}
remainingOptionInputs.RemoveRange(optionInputs);
}
if (remainingOptionInputs.Any())
{
throw CliFxException.UserError(
$"""
Unrecognized option(s):
{remainingOptionInputs.Select(o => o.GetFormattedIdentifier()).JoinToString(", ")}
"""
);
}
if (remainingRequiredOptionSchemas.Any())
{
throw CliFxException.UserError(
$"""
Missing required option(s):
{remainingRequiredOptionSchemas
.Select(o => o.GetFormattedIdentifier())
.JoinToString(", ")}
"""
);
}
}
public void Bind(
CommandInput commandInput,
CommandSchema commandSchema,
ICommand commandInstance
)
{
BindParameters(commandInput, commandSchema, commandInstance);
BindOptions(commandInput, commandSchema, commandInstance);
}
}

View File

@@ -308,50 +308,49 @@ internal class HelpConsoleFormatter(ConsoleWriter consoleWriter, HelpContext con
private void WriteDefaultValue(InputSchema schema) private void WriteDefaultValue(InputSchema schema)
{ {
var defaultValue = context.CommandDefaultValues.GetValueOrDefault(schema); var defaultValue = context.CommandDefaultValues.GetValueOrDefault(schema);
if (defaultValue is not null) if (defaultValue is null) return;
// Non-Scalar
if (defaultValue is not string && defaultValue is IEnumerable defaultValues)
{ {
// Non-Scalar var elementType =
if (defaultValue is not string && defaultValue is IEnumerable defaultValues) schema.Property.Type.TryGetEnumerableUnderlyingType() ?? typeof(object);
if (elementType.IsToStringOverriden())
{ {
var elementType = Write(ConsoleColor.White, "Default: ");
defaultValues.GetType().TryGetEnumerableUnderlyingType() ?? typeof(object);
if (elementType.IsToStringOverriden()) var isFirst = true;
foreach (var element in defaultValues)
{ {
Write(ConsoleColor.White, "Default: "); if (isFirst)
var isFirst = true;
foreach (var element in defaultValues)
{ {
if (isFirst) isFirst = false;
{ }
isFirst = false; else
} {
else Write(", ");
{
Write(", ");
}
Write('"');
Write(element.ToString(CultureInfo.InvariantCulture));
Write('"');
} }
Write('.'); Write('"');
Write(element.ToString(CultureInfo.InvariantCulture));
Write('"');
} }
}
else
{
if (defaultValue.GetType().IsToStringOverriden())
{
Write(ConsoleColor.White, "Default: ");
Write('"'); Write('.');
Write(defaultValue.ToString(CultureInfo.InvariantCulture)); }
Write('"'); }
Write('.'); else
} {
if (schema.Property.Type.IsToStringOverriden())
{
Write(ConsoleColor.White, "Default: ");
Write('"');
Write(defaultValue.ToString(CultureInfo.InvariantCulture));
Write('"');
Write('.');
} }
} }
} }

View File

@@ -1,5 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using CliFx.Input;
using CliFx.Schema;
namespace CliFx; namespace CliFx;
@@ -8,6 +10,15 @@ namespace CliFx;
/// </summary> /// </summary>
public interface ICommand public interface ICommand
{ {
/// <summary>
/// Binds the command input to the current instance, using the provided schema.
/// </summary>
/// <remarks>
/// This method is implemented automatically by the framework and should not be
/// called directly.
/// </remarks>
void Bind(CommandSchema schema, CommandInput input);
/// <summary> /// <summary>
/// Executes the command using the specified implementation of <see cref="IConsole" />. /// Executes the command using the specified implementation of <see cref="IConsole" />.
/// </summary> /// </summary>

View File

@@ -6,7 +6,7 @@
public interface ICommandWithVersionOption : ICommand public interface ICommandWithVersionOption : ICommand
{ {
/// <summary> /// <summary>
/// Whether the user requested the version information (via the `--version` option). /// Whether the user requested the application version information (via the `--version` option).
/// </summary> /// </summary>
bool IsVersionRequested { get; } bool IsVersionRequested { get; }
} }

View File

@@ -56,14 +56,18 @@ public class SystemConsole : IConsole, IDisposable
public int WindowWidth public int WindowWidth
{ {
get => Console.WindowWidth; get => Console.WindowWidth;
#pragma warning disable CA1416
set => Console.WindowWidth = value; set => Console.WindowWidth = value;
#pragma warning restore CA1416
} }
/// <inheritdoc /> /// <inheritdoc />
public int WindowHeight public int WindowHeight
{ {
get => Console.WindowHeight; get => Console.WindowHeight;
#pragma warning disable CA1416
set => Console.WindowHeight = value; set => Console.WindowHeight = value;
#pragma warning restore CA1416
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -5,7 +5,10 @@ using CliFx.Utils.Extensions;
namespace CliFx.Input; namespace CliFx.Input;
internal partial class CommandInput( /// <summary>
/// Describes input for a command.
/// </summary>
public partial class CommandInput(
string? commandName, string? commandName,
IReadOnlyList<DirectiveInput> directives, IReadOnlyList<DirectiveInput> directives,
IReadOnlyList<ParameterInput> parameters, IReadOnlyList<ParameterInput> parameters,

View File

@@ -2,7 +2,7 @@
namespace CliFx.Input; namespace CliFx.Input;
internal class DirectiveInput(string name) public class DirectiveInput(string name)
{ {
public string Name { get; } = name; public string Name { get; } = name;

View File

@@ -3,7 +3,7 @@ using System.IO;
namespace CliFx.Input; namespace CliFx.Input;
internal class EnvironmentVariableInput(string name, string value) public class EnvironmentVariableInput(string name, string value)
{ {
public string Name { get; } = name; public string Name { get; } = name;

View File

@@ -2,13 +2,22 @@
namespace CliFx.Input; namespace CliFx.Input;
internal class OptionInput(string identifier, IReadOnlyList<string> values) /// <summary>
/// Describes the materialized input for an option of a command.
/// </summary>
public class OptionInput(string identifier, IReadOnlyList<string> values)
{ {
/// <summary>
/// Option identifier (either the name or the short name).
/// </summary>
public string Identifier { get; } = identifier; public string Identifier { get; } = identifier;
/// <summary>
/// Provided option values.
/// </summary>
public IReadOnlyList<string> Values { get; } = values; public IReadOnlyList<string> Values { get; } = values;
public string GetFormattedIdentifier() => internal string GetFormattedIdentifier() =>
Identifier switch Identifier switch
{ {
{ Length: >= 2 } => "--" + Identifier, { Length: >= 2 } => "--" + Identifier,

View File

@@ -1,8 +1,14 @@
namespace CliFx.Input; namespace CliFx.Input;
internal class ParameterInput(string value) /// <summary>
/// Describes the materialized input for a parameter of a command.
/// </summary>
public class ParameterInput(string value)
{ {
/// <summary>
/// Parameter value.
/// </summary>
public string Value { get; } = value; public string Value { get; } = value;
public string GetFormattedIdentifier() => $"<{Value}>"; internal string GetFormattedIdentifier() => $"<{Value}>";
} }

View File

@@ -5,7 +5,7 @@ using System.Diagnostics.CodeAnalysis;
namespace CliFx.Schema; namespace CliFx.Schema;
/// <summary> /// <summary>
/// Describes an individual command. /// Describes an individual command, with its parameter and option bindings.
/// </summary> /// </summary>
public class CommandSchema( public class CommandSchema(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,

View File

@@ -4,7 +4,7 @@ using CliFx.Extensibility;
namespace CliFx.Schema; namespace CliFx.Schema;
/// <summary> /// <summary>
/// Describes an input of a command, which can be either a parameter or an option. /// Describes an input binding of a command.
/// </summary> /// </summary>
public abstract class InputSchema( public abstract class InputSchema(
PropertyBinding property, PropertyBinding property,

View File

@@ -6,7 +6,7 @@ using CliFx.Extensibility;
namespace CliFx.Schema; namespace CliFx.Schema;
/// <summary> /// <summary>
/// Describes an option input of a command. /// Describes an option binding of a command.
/// </summary> /// </summary>
public class OptionSchema( public class OptionSchema(
PropertyBinding property, PropertyBinding property,

View File

@@ -4,7 +4,7 @@ using CliFx.Extensibility;
namespace CliFx.Schema; namespace CliFx.Schema;
/// <summary> /// <summary>
/// Describes a parameter input of a command. /// Describes a parameter binding of a command.
/// </summary> /// </summary>
public class ParameterSchema( public class ParameterSchema(
PropertyBinding property, PropertyBinding property,

View File

@@ -1,13 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
namespace CliFx.Schema; namespace CliFx.Schema;
/// <summary> /// <summary>
/// Represents a binding to a CLR property. /// Represents a CLR property binding.
/// </summary> /// </summary>
public class PropertyBinding( public class PropertyBinding(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicMethods)]
Type type, Type type,
Func<object, object?> getValue, Func<object, object?> getValue,
Action<object, object?> setValue Action<object, object?> setValue
@@ -16,6 +18,7 @@ public class PropertyBinding(
/// <summary> /// <summary>
/// Underlying CLR type of the property. /// Underlying CLR type of the property.
/// </summary> /// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicMethods)]
public Type Type { get; } = type; public Type Type { get; } = type;
/// <summary> /// <summary>