-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathSpecificationGuards.cs
More file actions
49 lines (42 loc) · 2.23 KB
/
SpecificationGuards.cs
File metadata and controls
49 lines (42 loc) · 2.23 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
// 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 CSharpx;
namespace CommandLine.Core
{
static class SpecificationGuards
{
public static readonly IEnumerable<Tuple<Func<Specification, bool>, string>> Lookup = new List<Tuple<Func<Specification, bool>, string>>
{
Tuple.Create(GuardAgainstScalarWithRange(), "Scalar option specifications do not support range specification."),
Tuple.Create(GuardAgainstSequenceWithWrongRange(), "Bad range in sequence option specifications."),
Tuple.Create(GuardAgainstSequenceWithZeroRange(), "Zero is not allowed in range of sequence option specifications."),
Tuple.Create(GuardAgainstOneCharLongName(), "Long name should be longer than one character."),
Tuple.Create(GaurdAgainstUnsupportedSequenceTypes(), "Unsupported sequence type specification.")
};
private static Func<Specification, bool> GuardAgainstScalarWithRange()
{
return spec => spec.TargetType == TargetType.Scalar
&& (spec.Min.IsJust() || spec.Max.IsJust());
}
private static Func<Specification, bool> GuardAgainstSequenceWithWrongRange()
{
return spec => spec.TargetType == TargetType.Sequence
&& spec.HavingRange((min, max) => min > max);
}
private static Func<Specification, bool> GuardAgainstOneCharLongName()
{
return spec => spec.IsOption() && ((OptionSpecification)spec).LongName.Length == 1;
}
private static Func<Specification, bool> GuardAgainstSequenceWithZeroRange()
{
return spec => spec.TargetType == TargetType.Sequence
&& (spec.HavingMin(min => min == 0)
|| spec.HavingMax(max => max == 0));
}
private static Func<Specification, bool> GaurdAgainstUnsupportedSequenceTypes()
{
return spec => spec.TargetType == TargetType.Sequence && spec.ConversionType.GetGenericArguments().Length != 1;
}
}
}