BadScript 2
Loading...
Searching...
No Matches
SpecificationPropertyExtensions.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Reflection;
7
8using CSharpx;
9
10namespace CommandLine.Core
11{
12 internal static class SpecificationPropertyExtensions
13 {
15 Specification newSpecification)
16 {
17 if (newSpecification == null)
18 {
19 throw new ArgumentNullException(nameof(newSpecification));
20 }
21
22 return SpecificationProperty.Create(newSpecification, specProp.Property, specProp.Value);
23 }
24
26 {
27 if (newValue == null)
28 {
29 throw new ArgumentNullException(nameof(newValue));
30 }
31
32 return SpecificationProperty.Create(specProp.Specification, specProp.Property, newValue);
33 }
34
35 public static Type GetConversionType(this SpecificationProperty specProp)
36 {
37 switch (specProp.Specification.TargetType)
38 {
39 case TargetType.Sequence:
40 return specProp.Property.PropertyType.GetTypeInfo()
41 .GetGenericArguments()
42 .SingleOrDefault()
43 .ToMaybe()
44 .FromJustOrFail(new
45 InvalidOperationException("Sequence properties should be of type IEnumerable<T>."
46 )
47 );
48 default:
49 return specProp.Property.PropertyType;
50 }
51 }
52
53 public static IEnumerable<Error> Validate(this IEnumerable<SpecificationProperty> specProps,
54 IEnumerable<Func<IEnumerable<SpecificationProperty>,
55 IEnumerable<Error>>> rules)
56 {
57 return rules.SelectMany(rule => rule(specProps));
58 }
59 }
60}
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
static Type GetConversionType(this SpecificationProperty specProp)
static SpecificationProperty WithSpecification(this SpecificationProperty specProp, Specification newSpecification)
static SpecificationProperty WithValue(this SpecificationProperty specProp, Maybe< object > newValue)
static IEnumerable< Error > Validate(this IEnumerable< SpecificationProperty > specProps, IEnumerable< Func< IEnumerable< SpecificationProperty >, IEnumerable< Error > > > rules)
static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe< object > value)