Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

seperator

  • Loading branch information...
commit 6dc996cf9b40d70a516ad9e1d19c83c91eec845d 1 parent a8854bf
@adrianaisemberg authored
View
16 CLAP/HelpGenerator.cs
@@ -95,6 +95,11 @@ private static string GetHelpString(HelpInfo helpInfo)
sb.Append("(Required) ");
}
+ if (p.Separator != null && p.Separator != SeparatorAttribute.DefaultSeparator)
+ {
+ sb.AppendFormat("(Separator = {0}) ", p.Separator);
+ }
+
if (p.Default != null)
{
sb.AppendFormat("(Default = {0}) ", p.Default);
@@ -145,6 +150,11 @@ private static string GetHelpString(HelpInfo helpInfo)
sb.AppendFormat("({0}) ", GetTypeName(g.Type));
+ if (g.Separator != null && g.Separator != SeparatorAttribute.DefaultSeparator)
+ {
+ sb.AppendFormat("(Separator = {0}) ", g.Separator);
+ }
+
if (g.Validations.Any())
{
sb.AppendFormat("({0}) ", g.Validations.StringJoin(", "));
@@ -195,6 +205,8 @@ private static ParserHelpInfo GetParserHelp(ParserRunner parser)
Default = p.DefaultProvider != null ? p.DefaultProvider.Description : p.Default,
Description = p.Description,
Validations = p.ParameterInfo.GetAttributes<ValidationAttribute>().Select(v => v.Description).ToList(),
+ Separator = p.ParameterInfo.ParameterType.IsArray ?
+ p.Separator ?? SeparatorAttribute.DefaultSeparator : null,
}).ToList(),
}).ToList(),
Globals = parser.GetDefinedGlobals().Select(g =>
@@ -208,6 +220,10 @@ private static ParserHelpInfo GetParserHelp(ParserRunner parser)
Type = parameter != null ? parameter.ParameterInfo.ParameterType : typeof(bool),
Description = att.Description,
Validations = g.GetInterfaceAttributes<ICollectionValidation>().Select(v => v.Description).ToList(),
+ Separator = parameter != null ?
+ parameter.ParameterInfo.ParameterType.IsArray ?
+ parameter.Separator ?? SeparatorAttribute.DefaultSeparator :
+ null : null,
};
}).Union(parser.Register.RegisteredGlobalHandlers.Values.Select(handler => new GlobalParameterHelpInfo
{
View
2  CLAP/HelpInfo.cs
@@ -32,6 +32,7 @@ internal class ParameterHelpInfo
internal Type Type { get; set; }
internal object Default { get; set; }
internal string Description { get; set; }
+ internal string Separator { get; set; }
}
internal class GlobalParameterHelpInfo
@@ -40,5 +41,6 @@ internal class GlobalParameterHelpInfo
internal List<string> Validations { get; set; }
internal Type Type { get; set; }
internal string Description { get; set; }
+ internal string Separator { get; set; }
}
}
View
10 CLAP/Parameter.cs
@@ -39,6 +39,11 @@ public sealed class Parameter
public string Description { get; private set; }
/// <summary>
+ /// The parameter array separator
+ /// </summary>
+ public string Separator { get; private set; }
+
+ /// <summary>
/// The <see cref="ParameterInfo"/> this parameter describes
/// </summary>
public ParameterInfo ParameterInfo { get; private set; }
@@ -82,6 +87,11 @@ internal Parameter(ParameterInfo parameter)
{
Names.AddRange(parameter.GetAttribute<AliasesAttribute>().Aliases.ToLowerInvariant().CommaSplit());
}
+
+ if (parameter.HasAttribute<SeparatorAttribute>())
+ {
+ Separator = parameter.GetAttribute<SeparatorAttribute>().Separator;
+ }
}
#endregion Constructors
View
26 CLAP/ParameterAttribute.cs
@@ -45,6 +45,9 @@ public sealed class ParameterAttribute : Attribute
public string Description { get; set; }
}
+ /// <summary>
+ /// Sets a default value for a parameter
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class DefaultValueAttribute : Attribute
@@ -57,6 +60,10 @@ public DefaultValueAttribute(object defaultValue)
}
}
+ /// <summary>
+ /// Sets a default value provider type for a parameter.
+ /// The type must derive from DefaultProvider
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class DefaultProviderAttribute : Attribute
@@ -69,12 +76,18 @@ public DefaultProviderAttribute(Type defaultProviderType)
}
}
+ /// <summary>
+ /// Marks a parameter to be required
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class RequiredAttribute : Attribute
{
}
+ /// <summary>
+ /// Sets additional names to a parameter
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class AliasesAttribute : Attribute
@@ -87,6 +100,9 @@ public AliasesAttribute(string aliases)
}
}
+ /// <summary>
+ /// Sets a description to a parameter
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class DescriptionAttribute : Attribute
@@ -99,12 +115,22 @@ public DescriptionAttribute(string description)
}
}
+ /// <summary>
+ /// Sets an array parameter separator
+ /// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class SeparatorAttribute : Attribute
{
public string Separator { get; private set; }
+ public const string DefaultSeparator = ",";
+
+ /// <summary>
+ /// The separator to use.
+ /// The default is a comma (",")
+ /// </summary>
+ /// <param name="separator"></param>
public SeparatorAttribute(string separator)
{
Separator = separator;
View
48 CLAP/ParserRegistration.cs
@@ -175,7 +175,7 @@ public void ParameterHandler(string names, Action action)
ParameterHandler(
names,
new Action<bool>(delegate { action(); }),
- null);
+ new ParameterOptions());
}
/// <summary>
@@ -194,7 +194,10 @@ public void ParameterHandler(string names, Action action, string description)
ParameterHandler(
names,
new Action<bool>(delegate { action(); }),
- description);
+ new ParameterOptions
+ {
+ Description = description,
+ });
}
/// <summary>
@@ -213,7 +216,7 @@ public void ParameterHandler<TParameter>(string names, Action<TParameter> action
ParameterHandler(
names,
action,
- null);
+ new ParameterOptions());
}
/// <summary>
@@ -223,6 +226,7 @@ public void ParameterHandler<TParameter>(string names, Action<TParameter> action
/// <param name="names">The names (CSV) to be registered as parameters</param>
/// <param name="action">The action to execute</param>
/// <param name="description">The parameter description (for help generation)</param>
+ [Obsolete("Use ParameterOptions")]
public void ParameterHandler<TParameter>(string names, Action<TParameter> action, string description)
{
if (action == null)
@@ -233,14 +237,30 @@ public void ParameterHandler<TParameter>(string names, Action<TParameter> action
RegisterParameterHandlerInternal(
names,
action,
- description);
+ new ParameterOptions
+ {
+ Description = description,
+ });
+ }
+
+ public void ParameterHandler<TParameter>(string names, Action<TParameter> action, ParameterOptions options)
+ {
+ if (action == null)
+ {
+ throw new ArgumentNullException("action");
+ }
+
+ RegisterParameterHandlerInternal(
+ names,
+ action,
+ options);
}
#endregion Public Methods
#region Private Methods
- private void RegisterParameterHandlerInternal<TParameter>(string names, Action<TParameter> action, string description)
+ private void RegisterParameterHandlerInternal<TParameter>(string names, Action<TParameter> action, ParameterOptions options)
{
var objectAction = new Action<string>(str =>
{
@@ -264,7 +284,8 @@ private void RegisterParameterHandlerInternal<TParameter>(string names, Action<T
{
Names = names.CommaSplit(),
Handler = objectAction,
- Desription = description,
+ Desription = options.Description,
+ Separator = options.Separator,
Type = typeof(TParameter),
});
}
@@ -294,11 +315,26 @@ private void RegisterHelpHandlerInternal(IEnumerable<string> names, Action<strin
#endregion Private Methods
}
+ public sealed class ParameterOptions
+ {
+ /// <summary>
+ /// The parameter description
+ /// </summary>
+ public string Description { get; set; }
+
+ /// <summary>
+ /// In case of an array type - the values separator.
+ /// Unless specified, the default is a comma (",")
+ /// </summary>
+ public string Separator { get; set; }
+ }
+
internal class GlobalParameterHandler
{
internal IEnumerable<string> Names { get; set; }
internal Action<string> Handler { get; set; }
internal string Desription { get; set; }
+ internal string Separator { get; set; }
internal Type Type { get; set; }
}
}
View
14 CLAP/ParserRunner.cs
@@ -368,7 +368,7 @@ internal static void Validate(Type type, ParserRegistration registration)
// [Separator] can be applied only to array parameters
//
- ValidateSeparators(verbMethods);
+ ValidateSeparators(verbMethods, registration);
// no duplicate globals
//
@@ -430,8 +430,10 @@ private static void ValidateParameterDefaults(IEnumerable<Method> verbs)
}
}
- private static void ValidateSeparators(IEnumerable<Method> verbs)
+ private static void ValidateSeparators(IEnumerable<Method> verbs, ParserRegistration registration)
{
+ // check non-arrays
+ //
var parameters = verbs.SelectMany(v => v.MethodInfo.GetParameters()).ToList();
var nonArrayWithSeparator = parameters.Where(p => !p.ParameterType.IsArray && p.HasAttribute<SeparatorAttribute>());
@@ -441,6 +443,8 @@ private static void ValidateSeparators(IEnumerable<Method> verbs)
throw new NonArrayParameterWithSeparatorException(nonArrayWithSeparator.First());
}
+ // check invalid separators
+ //
var separators = parameters.
Where(p => p.HasAttribute<SeparatorAttribute>()).
Select(p => Pair.Create(p, p.GetAttribute<SeparatorAttribute>().Separator));
@@ -452,6 +456,12 @@ private static void ValidateSeparators(IEnumerable<Method> verbs)
{
throw new InvalidSeparatorException(invalidSeparator.First);
}
+
+
+ var invalidRegisteredHandlers = registration.RegisteredGlobalHandlers.
+ FirstOrDefault(a =>
+ a.Value.Type.IsArray &&
+ (string.IsNullOrEmpty(a.Value.Separator) || a.Value.Separator.Contains(" ")));
}
private static void ValidateDefinedEmptyHandlers(Type type)
View
2  CLAP/ValuesFactory.cs
@@ -81,7 +81,7 @@ internal static class ValuesFactory
// if array
if (parameterType.IsArray)
{
- var separator = ",";
+ var separator = SeparatorAttribute.DefaultSeparator;
if (parameter != null && parameter.HasAttribute<SeparatorAttribute>())
{
View
11 ConsoleTest/Program.cs
@@ -51,6 +51,17 @@ static void Help(string h)
{
Console.WriteLine(h);
}
+
+ [Global]
+ static void Sum([Separator("+")] int[] nums)
+ {
+ Console.WriteLine(nums.Sum());
+ }
+
+ [Verb]
+ static void Bring(string[] arr1, [Separator("-")] int[] arr2)
+ {
+ }
}
class EmptyApp
View
14 Tests/Samples.cs
@@ -355,7 +355,17 @@ public void Mish([LessThan(10)] int count)
}
[Global]
- public void Abra(BindingFlags f)
+ public void Abra1(BindingFlags f)
+ {
+ }
+
+ [Global]
+ public void Abra2(int[] nums)
+ {
+ }
+
+ [Global]
+ public void Abra3([Separator("-")] string[] strs)
{
}
@@ -371,7 +381,7 @@ public void Cat(Dictionary<string, Sample_10> x)
}
[Verb]
- public void WithARequiredSwitch(string str, [Required] bool sw)
+ public void WithARequiredSwitch(string str, [Required] bool sw, int[] nums, [Separator("-")] string[] strs)
{
}
View
4 Tests/Tests.cs
@@ -629,7 +629,7 @@ public void Global_Defined_BadConvertion()
var p = new Parser<Sample_10>();
- p.Run("print -abra:cadabra".Split(' '), sample);
+ p.Run("print -abra1:cadabra".Split(' '), sample);
}
[Test]
@@ -2232,7 +2232,7 @@ public void Register_ParameterHandler_Null_Exception_4()
{
var p = new Parser<Sample_02>();
- p.Register.ParameterHandler<string>("p", null, "description");
+ p.Register.ParameterHandler<string>("p", null, new ParameterOptions { Description = "description" });
}
[Test]
Please sign in to comment.
Something went wrong with that request. Please try again.