Permalink
Browse files

[Parameter] is obsolete

  • Loading branch information...
1 parent f60467b commit 9807083009a820d5237143e487e3437d72689b61 @adrianaisemberg committed Apr 5, 2012
Showing with 124 additions and 64 deletions.
  1. +16 −17 CLAP/Parameter.cs
  2. +60 −0 CLAP/ParameterAttribute.cs
  3. +10 −12 CLAP/ParserRunner.cs
  4. +38 −35 Tests/Samples.cs
View
@@ -60,28 +60,27 @@ internal Parameter(ParameterInfo parameter)
ParameterInfo = parameter;
+ Required = parameter.HasAttribute<RequiredAttribute>();
- var parameterAttribute = parameter.GetAttribute<ParameterAttribute>();
-
- // a parameter doesn't have to be marked with [ParameterAttribute]
- //
- if (parameterAttribute != null)
+ if (parameter.HasAttribute<DefaultValueAttribute>())
{
- Default = parameterAttribute.Default;
+ Default = parameter.GetAttribute<DefaultValueAttribute>().DefaultValue;
+ }
- if (parameterAttribute.DefaultProvider != null)
- {
- DefaultProvider = (DefaultProvider)Activator.CreateInstance(parameterAttribute.DefaultProvider);
- }
+ if (parameter.HasAttribute<DefaultProviderAttribute>())
+ {
+ DefaultProvider = (DefaultProvider)Activator.CreateInstance(
+ parameter.GetAttribute<DefaultProviderAttribute>().DefaultProviderType);
+ }
- Required = parameterAttribute.Required;
- Description = parameterAttribute.Description;
+ if (parameter.HasAttribute<DescriptionAttribute>())
+ {
+ Description = parameter.GetAttribute<DescriptionAttribute>().Description;
+ }
- if (parameterAttribute.Aliases != null)
- {
- Names.AddRange(
- parameterAttribute.Aliases.ToLowerInvariant().CommaSplit());
- }
+ if (parameter.HasAttribute<AliasesAttribute>())
+ {
+ Names.AddRange(parameter.GetAttribute<AliasesAttribute>().Aliases.ToLowerInvariant().CommaSplit());
}
}
View
@@ -7,11 +7,13 @@ namespace CLAP
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
+ [Obsolete("Use DefaultValueAttribute, DefaultProviderAttribute, RequiredAttribute, AliasesAttribute and DescriptionAttribute")]
public sealed class ParameterAttribute : Attribute
{
/// <summary>
/// The default value
/// </summary>
+ [Obsolete("Use DefaultValueAttribute")]
public object Default { get; set; }
/// <summary>
@@ -21,21 +23,79 @@ public sealed class ParameterAttribute : Attribute
/// The type must derive from CLAP.DefaultProvider.
/// A parameter cannot have both a Default and a DefaultProvider defined.
/// </remarks>
+ [Obsolete("Use DefaultProviderAttribute")]
public Type DefaultProvider { get; set; }
/// <summary>
/// Whether this parameter is required
/// </summary>
+ [Obsolete("Use RequiredAttribute")]
public Boolean Required { get; set; }
/// <summary>
/// The parameter additional names
/// </summary>
+ [Obsolete("Use AliasesAttribute")]
public string Aliases { get; set; }
/// <summary>
/// The description of the verb. Used to generate the help string
/// </summary>
+ [Obsolete("Use DescriptionAttribute")]
public string Description { get; set; }
}
+
+ [Serializable]
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public sealed class DefaultValueAttribute : Attribute
+ {
+ public object DefaultValue { get; private set; }
+
+ public DefaultValueAttribute(object defaultValue)
+ {
+ DefaultValue = defaultValue;
+ }
+ }
+
+ [Serializable]
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public sealed class DefaultProviderAttribute : Attribute
+ {
+ public Type DefaultProviderType { get; private set; }
+
+ public DefaultProviderAttribute(Type defaultProviderType)
+ {
+ DefaultProviderType = defaultProviderType;
+ }
+ }
+
+ [Serializable]
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public sealed class RequiredAttribute : Attribute
+ {
+ }
+
+ [Serializable]
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public sealed class AliasesAttribute : Attribute
+ {
+ public string Aliases { get; private set; }
+
+ public AliasesAttribute(string aliases)
+ {
+ Aliases = aliases;
+ }
+ }
+
+ [Serializable]
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public sealed class DescriptionAttribute : Attribute
+ {
+ public string Description { get; private set; }
+
+ public DescriptionAttribute(string description)
+ {
+ Description = description;
+ }
+ }
}
View
@@ -402,29 +402,27 @@ private static void ValidateParameterDefaults(IEnumerable<Method> verbs)
{
var parameters = verbs.SelectMany(v => v.MethodInfo.GetParameters()).ToList();
- var dict = parameters.
- Where(p => p.HasAttribute<ParameterAttribute>()).
- ToDictionary(p => p, p => p.GetAttribute<ParameterAttribute>());
-
// find one with both a Default and a DefaultProvider
//
- var bad = dict.Where(kvp => kvp.Value.DefaultProvider != null && kvp.Value.Default != null);
+ var hasBothDefaults = parameters.Where(p =>
+ p.HasAttribute<DefaultValueAttribute>() &&
+ p.HasAttribute<DefaultProviderAttribute>());
- if (bad.Any())
+ if (hasBothDefaults.Any())
{
- throw new AmbiguousParameterDefaultException(bad.First().Key);
+ throw new AmbiguousParameterDefaultException(hasBothDefaults.First());
}
// make sure all default providers are DefaultProvider
//
- bad = dict.Where(kvp =>
- kvp.Value.DefaultProvider != null &&
- !typeof(DefaultProvider).IsAssignableFrom(kvp.Value.DefaultProvider));
+ var hasInvalidDefaultProvider = parameters.Where(p =>
+ p.HasAttribute<DefaultProviderAttribute>() &&
+ !typeof(DefaultProvider).IsAssignableFrom(p.GetAttribute<DefaultProviderAttribute>().DefaultProviderType));
- if (bad.Any())
+ if (hasInvalidDefaultProvider.Any())
{
- throw new InvalidParameterDefaultProviderException(bad.First().Key);
+ throw new InvalidParameterDefaultProviderException(hasInvalidDefaultProvider.First());
}
}
Oops, something went wrong.

0 comments on commit 9807083

Please sign in to comment.