#region Copyright 2009-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace CSharpTest.Net.Commands
{
///
/// Defines an alias name for a command
///
[Serializable]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = true)]
public class AliasNameAttribute : Attribute
{
private readonly string _alias;
/// Constructs an AliasNameAttribute
public AliasNameAttribute(string commandAlias)
{
_alias = commandAlias;
}
/// Returns the name of the alias
public string Name { get { return _alias; } }
}
///
/// Instructs the CommandInterpreter to ignore a specific method/property
///
[Serializable]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false)]
public class IgnoreMemberAttribute : Attribute
{
/// Constructs an IgnoreMemberAttribute
public IgnoreMemberAttribute()
{
}
}
///
/// Defines that the string[] argument accepts all arguments provided to the command, useage:
/// void MyCommand([AllArguments] string[] arguments)
/// or
/// void MyCommand([AllArguments] string[] arguments, ICommandInterpreter ci)
///
[Serializable]
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public class AllArgumentsAttribute : Attribute
{
/// Constructs an AllArgumentsAttribute
public AllArgumentsAttribute()
{ }
}
///
/// Provides all the display properties.
///
public abstract class DisplayInfoAttribute : Attribute, IDisplayInfo
{
private string _displayName;
private string[] _aliasNames;
private string _category;
private string _description;
private bool _visible;
/// Constructs the attribute
protected DisplayInfoAttribute(string displayName, params string[] aliasNames)
{
_displayName = displayName;
_aliasNames = aliasNames;
_category = _description = null;
_visible = true;
}
/// Returns the DisplayName
public string DisplayName { get { return _displayName; } set { _displayName = value; } }
/// Just the alias names
public string[] AliasNames { get { return (string[])_aliasNames.Clone(); } set { _aliasNames = Check.NotNull(value); } }
/// Returns the name list
public string[] AllNames
{
get
{
List names = new List();
if (_displayName != null) names.Add(_displayName);
names.AddRange(_aliasNames);
return names.ToArray();
}
}
/// Returns the Category
public string Category { get { return _category; } set { _category = value; } }
/// Returns the Description
public string Description { get { return _description; } set { _description = value; } }
/// Returns the visibility of the command
public virtual bool Visible { get { return _visible; } set { _visible = value; } }
Type IDisplayInfo.ReflectedType { get { return null; } }
void IDisplayInfo.AddAttribute(T attribute) { }
bool IDisplayInfo.TryGetAttribute(out T found)
{
found = null;
return false;
}
void IDisplayInfo.Help() { }
}
/// Contains display info and a default value
public abstract class DisplayInfoAndValueAttribute : DisplayInfoAttribute
{
private object _defaultValue;
private bool _hasDefault;
/// Constructs the attribute
protected DisplayInfoAndValueAttribute(string displayName, params string[] aliasNames)
: base(displayName, aliasNames)
{ }
/// Gets/sets the default value for the option
public object DefaultValue { get { return _defaultValue; } set { _hasDefault = true; _defaultValue = value; } }
/// Returns true if a default value was specified
internal bool HasDefault { get { return _hasDefault; } }
}
///
/// Provides all the properties available for a command 'filter' that is
/// called for every command invoked enabling custom processing of arguments
/// and pre/post processing. The attribute is optional, the format of the
/// the method prototype is not and must be:
/// void (ICommandInterpreter interpreter, ICommandChain chain, string[] arguments);
///
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CommandFilterAttribute : DisplayInfoAttribute
{
char[] _keys;
/// Constructs the attribute
public CommandFilterAttribute(char key)
: this(new char[] { key})
{ }
/// Constructs the attribute
public CommandFilterAttribute( params char[] keys )
: base(null, new string[0])
{
_keys = keys;
base.Visible = false;
}
/// Returns the keys associated with this filter
public Char[] Keys { get { return _keys; } }
/// Ignored.
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override bool Visible { get { return false; } set { } }
}
///
/// Provides all the properties available for a command.
///
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CommandAttribute : DisplayInfoAttribute
{
/// Constructs the attribute
public CommandAttribute()
: base(null, new string[0])
{ }
/// Constructs the attribute
public CommandAttribute(string displayName)
: base(displayName, new string[0])
{ }
/// Constructs the attribute
public CommandAttribute(string displayName, params string[] aliasNames)
: base(displayName, aliasNames)
{ }
}
///
/// Provides all the properties available for an argument.
///
[Serializable]
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public class ArgumentAttribute : DisplayInfoAndValueAttribute
{
/// Constructs the attribute
public ArgumentAttribute()
: base(null, new string[0])
{ }
/// Constructs the attribute
public ArgumentAttribute(string displayName)
: base(displayName, new string[0])
{ }
/// Constructs the attribute
public ArgumentAttribute(string displayName, params string[] aliasNames)
: base(displayName, aliasNames)
{ }
}
///
/// Provides all the properties available for an argument.
///
[Serializable]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class OptionAttribute : DisplayInfoAndValueAttribute
{
/// Constructs the attribute
public OptionAttribute()
: base(null, new string[0])
{ }
/// Constructs the attribute
public OptionAttribute(string displayName)
: base(displayName, new string[0])
{ }
/// Constructs the attribute
public OptionAttribute(string displayName, params string[] aliasNames)
: base(displayName, aliasNames)
{ }
}
}