This commit is contained in:
Tyrrrz
2024-08-11 22:12:36 +03:00
parent a4376c955b
commit 82b0c6fd98
10 changed files with 80 additions and 106 deletions

View File

@@ -4,33 +4,25 @@ namespace CliFx.Attributes;
/// <summary>
/// Annotates a type that defines a command.
/// If a command is named, then the user must provide its name through the command-line arguments in order to execute it.
/// If a command is not named, then it is treated as the application's default command and is executed when no other
/// command is specified.
/// </summary>
/// <remarks>
/// Only one default command is allowed per application.
/// All commands registered in an application must have unique names (comparison IS NOT case-sensitive).
/// </remarks>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class CommandAttribute : Attribute
public class CommandAttribute(string? name = null) : Attribute
{
/// <summary>
/// Initializes an instance of <see cref="CommandAttribute" />.
/// </summary>
public CommandAttribute(string name) => Name = name;
/// <summary>
/// Initializes an instance of <see cref="CommandAttribute" />.
/// </summary>
public CommandAttribute() { }
/// <summary>
/// Command name.
/// </summary>
/// <remarks>
/// Command can have no name, in which case it's treated as the application's default command.
/// Only one default command is allowed in an application.
/// All commands registered in an application must have unique names (comparison IS NOT case-sensitive).
/// </remarks>
public string? Name { get; }
public string? Name { get; } = name;
/// <summary>
/// Command description.
/// This is shown to the user in the help text.
/// </summary>
public string? Description { get; set; }
}
}

View File

@@ -1,8 +1,11 @@
namespace CliFx.Attributes;
/// <summary>
/// Annotates a property that defines the help option for a command.
/// Binds a property to the help option of a command.
/// </summary>
/// <remarks>
/// This attribute is applied automatically by the framework and should not be used explicitly.
/// </remarks>
public class CommandHelpOptionAttribute : CommandOptionAttribute
{
/// <summary>

View File

@@ -0,0 +1,34 @@
using System;
using CliFx.Extensibility;
namespace CliFx.Attributes;
/// <summary>
/// Binds a property to a command-line input.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public abstract class CommandInputAttribute : Attribute
{
/// <summary>
/// Input description, as shown in the help text.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// Custom converter used for mapping the raw command-line argument into
/// the type and shape expected by the underlying property.
/// </summary>
/// <remarks>
/// Converter must derive from <see cref="BindingConverter{T}" />.
/// </remarks>
public Type? Converter { get; set; }
/// <summary>
/// Custom validators used for verifying the value of the underlying
/// property, after it has been set.
/// </summary>
/// <remarks>
/// Validators must derive from <see cref="BindingValidator{T}" />.
/// </remarks>
public Type[] Validators { get; set; } = [];
}

View File

@@ -4,10 +4,13 @@ using CliFx.Extensibility;
namespace CliFx.Attributes;
/// <summary>
/// Annotates a property that defines a command option.
/// Binds a property to a command option — a command-line input that is identified by a name and/or a short name.
/// </summary>
/// <remarks>
/// All options in a command must have unique names (comparison IS NOT case-sensitive) and short names (comparison IS case-sensitive).
/// </remarks>
[AttributeUsage(AttributeTargets.Property)]
public class CommandOptionAttribute : Attribute
public class CommandOptionAttribute : CommandInputAttribute
{
/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute" />.
@@ -39,20 +42,11 @@ public class CommandOptionAttribute : Attribute
/// <summary>
/// Option name.
/// </summary>
/// <remarks>
/// Must contain at least two characters and start with a letter.
/// Either <see cref="Name" /> or <see cref="ShortName" /> must be set.
/// All options in a command must have unique names (comparison IS NOT case-sensitive).
/// </remarks>
public string? Name { get; }
/// <summary>
/// Option short name.
/// </summary>
/// <remarks>
/// Either <see cref="Name" /> or <see cref="ShortName" /> must be set.
/// All options in a command must have unique short names (comparison IS case-sensitive).
/// </remarks>
public char? ShortName { get; }
/// <summary>
@@ -70,28 +64,4 @@ public class CommandOptionAttribute : Attribute
/// has not been explicitly set through command-line arguments.
/// </summary>
public string? EnvironmentVariable { get; set; }
/// <summary>
/// Option description.
/// This is shown to the user in the help text.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// Custom converter used for mapping the raw command-line argument into
/// a value expected by the underlying property.
/// </summary>
/// <remarks>
/// Converter must derive from <see cref="BindingConverter{T}" />.
/// </remarks>
public Type? Converter { get; set; }
/// <summary>
/// Custom validators used for verifying the value of the underlying
/// property, after it has been bound.
/// </summary>
/// <remarks>
/// Validators must derive from <see cref="BindingValidator{T}" />.
/// </remarks>
public Type[] Validators { get; set; } = Array.Empty<Type>();
}

View File

@@ -5,21 +5,21 @@ using CliFx.Extensibility;
namespace CliFx.Attributes;
/// <summary>
/// Annotates a property that defines a command parameter.
/// Binds a property to a command parameter — a command-line input that is identified by its relative position (order).
/// Higher order means that the parameter appears later, lower order means that it appears earlier.
/// </summary>
/// <remarks>
/// All parameters in a command must have unique order values.
/// If a parameter is bound to a property whose type is a sequence (e.g. Array, <see cref="List{T}" />; except <see cref="string" />),
/// then it must have the highest order in the command.
/// Only one sequential parameter is allowed per command.
/// </remarks>
[AttributeUsage(AttributeTargets.Property)]
public class CommandParameterAttribute(int order) : Attribute
public class CommandParameterAttribute(int order) : CommandInputAttribute
{
/// <summary>
/// Parameter order.
/// Higher order means the parameter appears later, lower order means it appears earlier.
/// </summary>
/// <remarks>
/// All parameters in a command must have unique order.
/// Parameter whose type is a sequence (e.g. Array, <see cref="List{T}" />; except <see cref="string" />),
/// must always be the last parameter based on order.
/// Only one sequential parameter is allowed in a command.
/// </remarks>
public int Order { get; } = order;
/// <summary>
@@ -27,41 +27,16 @@ public class CommandParameterAttribute(int order) : Attribute
/// If a parameter is required, the user will get an error if they don't set it.
/// </summary>
/// <remarks>
/// Parameter marked as non-required must always be the last in order.
/// Only one non-required parameter is allowed in a command.
/// Parameter marked as non-required must have the highest order in the command.
/// Only one non-required parameter is allowed per command.
/// </remarks>
public bool IsRequired { get; set; } = true;
/// <summary>
/// Parameter name.
/// This is shown to the user in the help text.
/// Parameter name, as shown in the help text.
/// </summary>
/// <remarks>
/// If this isn't specified, parameter name is inferred from the property name.
/// </remarks>
public string? Name { get; set; }
/// <summary>
/// Parameter description.
/// This is shown to the user in the help text.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// Custom converter used for mapping the raw command-line argument into
/// a value expected by the underlying property.
/// </summary>
/// <remarks>
/// Converter must derive from <see cref="BindingConverter{T}" />.
/// </remarks>
public Type? Converter { get; set; }
/// <summary>
/// Custom validators used for verifying the value of the underlying
/// property, after it has been bound.
/// </summary>
/// <remarks>
/// Validators must derive from <see cref="BindingValidator{T}" />.
/// </remarks>
public Type[] Validators { get; set; } = Array.Empty<Type>();
}

View File

@@ -1,8 +1,11 @@
namespace CliFx.Attributes;
/// <summary>
/// Annotates a property that defines the version option for a command.
/// Binds a property to the version option of a command.
/// </summary>
/// <remarks>
/// This attribute is applied automatically by the framework and should not be used explicitly.
/// </remarks>
public class CommandVersionOptionAttribute : CommandOptionAttribute
{
/// <summary>

View File

@@ -14,6 +14,7 @@ namespace CliFx.Schema;
/// </summary>
public abstract class InputSchema(
PropertyBinding property,
string? description,
IBindingConverter converter,
IReadOnlyList<IBindingValidator> validators
)
@@ -22,6 +23,11 @@ public abstract class InputSchema(
property.Type != typeof(string)
&& property.Type.TryGetEnumerableUnderlyingType() is not null;
/// <summary>
/// Input description, used in the help text.
/// </summary>
public string? Description { get; } = description;
/// <summary>
/// CLR property to which this input is bound.
/// </summary>
@@ -119,7 +125,8 @@ public abstract class InputSchema<
TProperty
>(
PropertyBinding<TCommand, TProperty> property,
string? description,
BindingConverter<TProperty> converter,
IReadOnlyList<BindingValidator<TProperty>> validators
) : InputSchema(property, converter, validators)
) : InputSchema(property, description, converter, validators)
where TCommand : ICommand;

View File

@@ -18,7 +18,7 @@ public class OptionSchema(
string? description,
IBindingConverter converter,
IReadOnlyList<IBindingValidator> validators
) : InputSchema(property, converter, validators)
) : InputSchema(property,description, converter, validators)
{
/// <summary>
/// Option name.
@@ -40,11 +40,6 @@ public class OptionSchema(
/// </summary>
public bool IsRequired { get; } = isRequired;
/// <summary>
/// Option description.
/// </summary>
public string? Description { get; } = description;
internal bool MatchesName(string? name) =>
!string.IsNullOrWhiteSpace(Name)
&& string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);

View File

@@ -15,7 +15,7 @@ public class ParameterSchema(
string? description,
IBindingConverter converter,
IReadOnlyList<IBindingValidator> validators
) : InputSchema(property, converter, validators)
) : InputSchema(property, description, converter, validators)
{
/// <summary>
/// Order, in which the parameter is bound from the command-line arguments.
@@ -32,11 +32,6 @@ public class ParameterSchema(
/// </summary>
public bool IsRequired { get; } = isRequired;
/// <summary>
/// Parameter description.
/// </summary>
public string? Description { get; } = description;
internal override string GetKind() => "Parameter";
internal override string GetFormattedIdentifier() => IsSequence ? $"<{Name}>" : $"<{Name}...>";

View File

@@ -51,7 +51,7 @@ internal class NoPreambleEncoding(Encoding underlyingEncoding)
public override bool IsMailNewsSave => underlyingEncoding.IsMailNewsSave;
// This is the only part that changes
public override byte[] GetPreamble() => Array.Empty<byte>();
public override byte[] GetPreamble() => [];
[ExcludeFromCodeCoverage]
public override int GetByteCount(char[] chars, int index, int count) =>