BadScript 2
Loading...
Searching...
No Matches
Specification.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
9
10using CSharpx;
11
12namespace CommandLine.Core
13{
14 internal enum SpecificationType
15 {
16 Option,
17 Value,
18 }
19
20 internal enum TargetType
21 {
22 Switch,
23 Scalar,
25 }
26
27 internal abstract class Specification
28 {
30 bool required,
31 Maybe<int> min,
32 Maybe<int> max,
33 Maybe<object> defaultValue,
34 string helpText,
35 string metaValue,
36 IEnumerable<string> enumValues,
37 Type conversionType,
38 TargetType targetType,
39 bool hidden = false)
40 {
41 Tag = tag;
42 Required = required;
43 Min = min;
44 Max = max;
45 DefaultValue = defaultValue;
46 ConversionType = conversionType;
47 TargetType = targetType;
48 HelpText = helpText;
49 MetaValue = metaValue;
50 EnumValues = enumValues;
51 Hidden = hidden;
52 }
53
54 public SpecificationType Tag { get; }
55
56 public bool Required { get; }
57
58 public Maybe<int> Min { get; }
59
60 public Maybe<int> Max { get; }
61
63
64 public string HelpText { get; }
65
66 public string MetaValue { get; }
67
68 public IEnumerable<string> EnumValues { get; }
69
71 public Type ConversionType { get; }
72
73 public TargetType TargetType { get; }
74
75 public bool Hidden { get; }
76
77 public static Specification FromProperty(PropertyInfo property)
78 {
79 object[] attrs = property.GetCustomAttributes(true);
80 IEnumerable<OptionAttribute> oa = attrs.OfType<OptionAttribute>();
81
82 if (oa.Count() == 1)
83 {
85 property.PropertyType,
87 .GetNamesOfEnum(property.PropertyType)
88 );
89
90 if (spec.ShortName.Length == 0 && spec.LongName.Length == 0)
91 {
92 return spec.WithLongName(property.Name.ToLowerInvariant());
93 }
94
95 return spec;
96 }
97
98 IEnumerable<ValueAttribute> va = attrs.OfType<ValueAttribute>();
99
100 if (va.Count() == 1)
101 {
102 return ValueSpecification.FromAttribute(va.Single(),
103 property.PropertyType,
104 property.PropertyType.GetTypeInfo()
105 .IsEnum
106 ? Enum.GetNames(property.PropertyType)
107 : Enumerable.Empty<string>()
108 );
109 }
110
111 throw new InvalidOperationException();
112 }
113 }
114}
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 OptionSpecification FromAttribute(OptionAttribute attribute, Type conversionType, IEnumerable< string > enumValues)
Specification(SpecificationType tag, bool required, Maybe< int > min, Maybe< int > max, Maybe< object > defaultValue, string helpText, string metaValue, IEnumerable< string > enumValues, Type conversionType, TargetType targetType, bool hidden=false)
static Specification FromProperty(PropertyInfo property)
Type ConversionType
This information is denormalized to decouple Specification from PropertyInfo.
IEnumerable< string > EnumValues
static ValueSpecification FromAttribute(ValueAttribute attribute, Type conversionType, IEnumerable< string > enumValues)
static IEnumerable< string > GetNamesOfEnum(Type t)
Models an option specification.
Models an value specification, or better how to handle values not bound to options.