forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecificationPropertyExtensions.cs
More file actions
50 lines (43 loc) · 1.99 KB
/
Copy pathSpecificationPropertyExtensions.cs
File metadata and controls
50 lines (43 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using CSharpx;
using System.Reflection;
namespace CommandLine.Core
{
static class SpecificationPropertyExtensions
{
public static SpecificationProperty WithSpecification(this SpecificationProperty specProp, Specification newSpecification)
{
if (newSpecification == null) throw new ArgumentNullException(nameof(newSpecification));
return SpecificationProperty.Create(newSpecification, specProp.Property, specProp.Value);
}
public static SpecificationProperty WithValue(this SpecificationProperty specProp, Maybe<object> newValue)
{
if (newValue == null) throw new ArgumentNullException(nameof(newValue));
return SpecificationProperty.Create(specProp.Specification, specProp.Property, newValue);
}
public static Type GetConversionType(this SpecificationProperty specProp)
{
switch (specProp.Specification.TargetType)
{
case TargetType.Sequence:
return specProp.Property.PropertyType.GetTypeInfo().GetGenericArguments()
.SingleOrDefault()
.ToMaybe()
.FromJustOrFail(
new InvalidOperationException("Sequence properties should be of type IEnumerable<T>."));
default:
return specProp.Property.PropertyType;
}
}
public static IEnumerable<Error> Validate(
this IEnumerable<SpecificationProperty> specProps,
IEnumerable<Func<IEnumerable<SpecificationProperty>,
IEnumerable<Error>>> rules)
{
return rules.SelectMany(rule => rule(specProps));
}
}
}