1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-27 00:38:46 +00:00
VpSharp/VpSharp.Commands/Attributes/AliasesAttribute.cs

59 lines
1.9 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
namespace VpSharp.Commands.Attributes;
2022-11-27 20:43:21 +00:00
/// <summary>
/// Defines the aliases of a command.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
[CLSCompliant(false)]
2022-11-27 20:43:21 +00:00
public sealed class AliasesAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="AliasesAttribute" /> class.
/// </summary>
/// <param name="alias">The first alias.</param>
/// <param name="aliases">Additional aliases.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="alias" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="aliases" /> is <see langword="null" />.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="alias" /> is empty, or consists of only whitespace.</para>
/// -or-
/// <para>An element in <paramref name="aliases" /> is null, empty, or consists of only whitespace.</para>
/// </exception>
#pragma warning disable CA1019
2022-11-27 20:43:21 +00:00
public AliasesAttribute(string alias, params string[] aliases)
#pragma warning restore CA1019
2022-11-27 20:43:21 +00:00
{
ArgumentNullException.ThrowIfNull(alias);
ArgumentNullException.ThrowIfNull(aliases);
if (string.IsNullOrWhiteSpace(alias))
{
throw new ArgumentException("Alias cannot be empty");
}
foreach (string a in aliases)
{
if (string.IsNullOrWhiteSpace(a))
{
throw new ArgumentException("Cannot have a null alias");
}
}
var buffer = new string[aliases.Length + 1];
buffer[0] = alias;
Array.Copy(aliases, 0, buffer, 1, aliases.Length);
Aliases = new ReadOnlyCollection<string>(buffer);
2022-11-27 20:43:21 +00:00
}
/// <summary>
/// Gets the command aliases.
/// </summary>
/// <value>The command aliases.</value>
public IReadOnlyList<string> Aliases { get; }
2022-11-27 20:43:21 +00:00
}