Move Spectre.Console.Cli to it's own package

This commit is contained in:
Patrik Svensson
2022-05-14 22:56:36 +02:00
committed by Patrik Svensson
parent b600832e00
commit 36ca22ffac
262 changed files with 736 additions and 48 deletions

View File

@ -0,0 +1,50 @@
namespace Spectre.Console.Cli;
/// <summary>
/// An attribute representing a command argument.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class CommandArgumentAttribute : Attribute
{
/// <summary>
/// Gets the argument position.
/// </summary>
/// <value>The argument position.</value>
public int Position { get; }
/// <summary>
/// Gets the value name of the argument.
/// </summary>
/// <value>The value name of the argument.</value>
public string ValueName { get; }
/// <summary>
/// Gets a value indicating whether the argument is required.
/// </summary>
/// <value>
/// <c>true</c> if the argument is required; otherwise, <c>false</c>.
/// </value>
public bool IsRequired { get; }
/// <summary>
/// Initializes a new instance of the <see cref="CommandArgumentAttribute"/> class.
/// </summary>
/// <param name="position">The argument position.</param>
/// <param name="template">The argument template.</param>
public CommandArgumentAttribute(int position, string template)
{
if (template == null)
{
throw new ArgumentNullException(nameof(template));
}
// Parse the option template.
var result = TemplateParser.ParseArgumentTemplate(template);
// Assign the result.
Position = position;
ValueName = result.Value;
IsRequired = result.Required;
}
}

View File

@ -0,0 +1,65 @@
namespace Spectre.Console.Cli;
/// <summary>
/// An attribute representing a command option.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class CommandOptionAttribute : Attribute
{
/// <summary>
/// Gets the long names of the option.
/// </summary>
/// <value>The option's long names.</value>
public IReadOnlyList<string> LongNames { get; }
/// <summary>
/// Gets the short names of the option.
/// </summary>
/// <value>The option's short names.</value>
public IReadOnlyList<string> ShortNames { get; }
/// <summary>
/// Gets the value name of the option.
/// </summary>
/// <value>The option's value name.</value>
public string? ValueName { get; }
/// <summary>
/// Gets a value indicating whether the value is optional.
/// </summary>
public bool ValueIsOptional { get; }
/// <summary>
/// Gets or sets a value indicating whether this option is hidden from the help text.
/// </summary>
public bool IsHidden { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CommandOptionAttribute"/> class.
/// </summary>
/// <param name="template">The option template.</param>
public CommandOptionAttribute(string template)
{
if (template == null)
{
throw new ArgumentNullException(nameof(template));
}
// Parse the option template.
var result = TemplateParser.ParseOptionTemplate(template);
// Assign the result.
LongNames = result.LongNames;
ShortNames = result.ShortNames;
ValueName = result.Value;
ValueIsOptional = result.ValueIsOptional;
}
internal bool IsMatch(string name)
{
return
ShortNames.Contains(name, StringComparer.Ordinal) ||
LongNames.Contains(name, StringComparer.Ordinal);
}
}

View File

@ -0,0 +1,28 @@
namespace Spectre.Console.Cli;
/// <summary>
/// Specifies what type to use as a pair deconstructor for
/// the property this attribute is bound to.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class PairDeconstructorAttribute : Attribute
{
/// <summary>
/// Gets the <see cref="string"/> that represents the type of the
/// pair deconstructor class to use for data conversion for the
/// object this attribute is bound to.
/// </summary>
public Type Type { get; }
/// <summary>
/// Initializes a new instance of the <see cref="PairDeconstructorAttribute"/> class.
/// </summary>
/// <param name="type">
/// A System.Type that represents the type of the pair deconstructor
/// class to use for data conversion for the object this attribute is bound to.
/// </param>
public PairDeconstructorAttribute(Type type)
{
Type = type ?? throw new ArgumentNullException(nameof(type));
}
}

View File

@ -0,0 +1,31 @@
namespace Spectre.Console.Cli;
/// <summary>
/// A base class attribute used for parameter validation.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public abstract class ParameterValidationAttribute : Attribute
{
/// <summary>
/// Gets the validation error message.
/// </summary>
/// <value>The validation error message.</value>
public string ErrorMessage { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ParameterValidationAttribute"/> class.
/// </summary>
/// <param name="errorMessage">The validation error message.</param>
protected ParameterValidationAttribute(string errorMessage)
{
ErrorMessage = errorMessage;
}
/// <summary>
/// Validates the parameter value.
/// </summary>
/// <param name="context">The parameter context.</param>
/// <returns>The validation result.</returns>
public abstract ValidationResult Validate(CommandParameterContext context);
}

View File

@ -0,0 +1,17 @@
namespace Spectre.Console.Cli;
/// <summary>
/// A base class attribute used for parameter completion.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public abstract class ParameterValueProviderAttribute : Attribute
{
/// <summary>
/// Gets a value for the parameter.
/// </summary>
/// <param name="context">The parameter context.</param>
/// <param name="result">The resulting value.</param>
/// <returns><c>true</c> if a value was provided; otherwise, <c>false</c>.</returns>
public abstract bool TryGetValue(CommandParameterContext context, out object? result);
}