using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console.Cli
{
///
/// An attribute representing a command option.
///
///
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class CommandOptionAttribute : Attribute
{
///
/// Gets the long names of the option.
///
/// The option's long names.
public IReadOnlyList LongNames { get; }
///
/// Gets the short names of the option.
///
/// The option's short names.
public IReadOnlyList ShortNames { get; }
///
/// Gets the value name of the option.
///
/// The option's value name.
public string? ValueName { get; }
///
/// Gets a value indicating whether the value is optional.
///
public bool ValueIsOptional { get; }
///
/// Initializes a new instance of the class.
///
/// The option template.
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);
}
}
}