94 lines
3.2 KiB
C#
94 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
|
|
namespace CliFx.Binding;
|
|
|
|
/// <summary>
|
|
/// Describes a binding between a CLR type and a command.
|
|
/// </summary>
|
|
public class CommandDescriptor(
|
|
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
|
|
Type type,
|
|
string? name,
|
|
string? description,
|
|
IReadOnlyList<string> examples,
|
|
IReadOnlyList<CommandInputDescriptor> inputs
|
|
)
|
|
{
|
|
/// <summary>
|
|
/// CLR type of the command.
|
|
/// </summary>
|
|
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
|
|
public Type Type { get; } = type;
|
|
|
|
/// <inheritdoc cref="CommandAttribute.Name" />
|
|
public string? Name { get; } = name;
|
|
|
|
internal bool IsDefault => string.IsNullOrWhiteSpace(Name);
|
|
|
|
/// <inheritdoc cref="CommandAttribute.Description" />
|
|
public string? Description { get; } = description;
|
|
|
|
/// <inheritdoc cref="CommandAttribute.Examples" />
|
|
public IReadOnlyList<string> Examples { get; } = examples;
|
|
|
|
/// <summary>
|
|
/// Inputs (parameters and options) of the command;
|
|
/// </summary>
|
|
public IReadOnlyList<CommandInputDescriptor> Inputs { get; } = inputs;
|
|
|
|
internal IReadOnlyList<CommandParameterDescriptor> Parameters { get; } =
|
|
inputs.OfType<CommandParameterDescriptor>().ToArray();
|
|
|
|
internal IReadOnlyList<CommandOptionDescriptor> Options { get; } =
|
|
inputs.OfType<CommandOptionDescriptor>().ToArray();
|
|
|
|
internal bool MatchesName(string? name) =>
|
|
!string.IsNullOrWhiteSpace(Name)
|
|
? string.Equals(name, Name, StringComparison.OrdinalIgnoreCase)
|
|
: string.IsNullOrWhiteSpace(name);
|
|
|
|
/// <summary>
|
|
/// Initializes an instance of <see cref="CommandDescriptor" /> without examples.
|
|
/// </summary>
|
|
public CommandDescriptor(
|
|
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
|
|
Type type,
|
|
string? name,
|
|
string? description,
|
|
IReadOnlyList<CommandInputDescriptor> inputs
|
|
)
|
|
: this(type, name, description, Array.Empty<string>(), inputs) { }
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString() => !string.IsNullOrWhiteSpace(Name) ? Name : "<default>";
|
|
}
|
|
|
|
/// <inheritdoc cref="CommandDescriptor" />
|
|
/// <remarks>
|
|
/// Generic version used by source-generated code for static type references and AOT compatibility.
|
|
/// </remarks>
|
|
public class CommandDescriptor<
|
|
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
|
|
TCommand
|
|
>(
|
|
string? name,
|
|
string? description,
|
|
IReadOnlyList<string> examples,
|
|
IReadOnlyList<CommandInputDescriptor> inputs
|
|
) : CommandDescriptor(typeof(TCommand), name, description, examples, inputs)
|
|
where TCommand : ICommand
|
|
{
|
|
/// <summary>
|
|
/// Initializes an instance of <see cref="CommandDescriptor{TCommand}" /> without examples.
|
|
/// </summary>
|
|
public CommandDescriptor(
|
|
string? name,
|
|
string? description,
|
|
IReadOnlyList<CommandInputDescriptor> inputs
|
|
)
|
|
: this(name, description, Array.Empty<string>(), inputs) { }
|
|
}
|