-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathEnumeratedValueArgument.cs
More file actions
319 lines (296 loc) · 12.9 KB
/
EnumeratedValueArgument.cs
File metadata and controls
319 lines (296 loc) · 12.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;
namespace CommandLineParser.Arguments
{
/// <summary>
/// Use EnumeratedValueArgument for an argument whose values must be from certain finite set
/// (see <see cref="AllowedValues"/>)
/// </summary>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <include file='..\Doc\CommandLineParser.xml' path='CommandLineParser/Arguments/EnumeratedValueArgument/*'/>
public class EnumeratedValueArgument<TValue> : CertifiedValueArgument<TValue>
{
private ICollection<TValue> _allowedValues;
private bool _ignoreCase;
/// <summary>
/// Set of values that are allowed for the argument.
/// </summary>
public ICollection<TValue> AllowedValues
{
get { return _allowedValues; }
set { _allowedValues = value; }
}
/// <summary>
/// Initilazes <see cref="AllowedValues"/> by a string of values separated by commas or semicolons.
/// </summary>
/// <param name="valuesString">Allowed values (separated by comas or semicolons)</param>
public void InitAllowedValues(string valuesString)
{
string[] splitted = valuesString.Split(';', ',');
TValue[] typedValues = new TValue[splitted.Length];
int i = 0;
foreach (string value in splitted)
{
typedValues[i] = Convert(value);
i++;
}
AllowedValues = typedValues;
}
/// <summary>
/// String arguments will be accepted even with differences in capitalisation (e.g. INFO will be accepted for info).
/// </summary>
public bool IgnoreCase
{
get { return _ignoreCase; }
set
{
if (!(typeof(TValue) == typeof(string)) && value)
{
throw new ArgumentException(string.Format("Ignore case can be used only for string arguments, type of TValue is {0}", typeof(TValue)));
}
_ignoreCase = value;
}
}
#region constructor
/// <summary>
/// Creates new command line argument with a <see cref="Argument.ShortName">short name</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
public EnumeratedValueArgument(char shortName)
: base(shortName)
{
_allowedValues = new TValue[0];
}
/// <summary>
/// Creates new command line argument with a <see cref="Argument.LongName">long name</see>.
/// </summary>
/// <param name="longName">Long name of the argument</param>
public EnumeratedValueArgument(string longName) : base(longName) { }
/// <summary>
/// Creates new command line argument with a <see cref="Argument.ShortName">short name</see>
/// and <see cref="Argument.LongName">long name</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="longName">Long name of the argument </param>
public EnumeratedValueArgument(char shortName, string longName)
: base(shortName, longName)
{
_allowedValues = new TValue[0];
}
/// <summary>
/// Creates new command line argument with a <see cref="Argument.ShortName">short name</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="allowedValues">Allowed values</param>
public EnumeratedValueArgument(char shortName, ICollection<TValue> allowedValues)
: base(shortName)
{
_allowedValues = allowedValues;
}
/// <summary>
/// Creates new command line argument with a <see cref="Argument.LongName">long name</see>.
/// </summary>
/// <param name="longName">Short name of the argument</param>
/// <param name="allowedValues">Allowed values</param>
public EnumeratedValueArgument(string longName, ICollection<TValue> allowedValues)
: base(longName)
{
_allowedValues = allowedValues;
}
/// <summary>
/// Creates new command line argument with a <see cref="Argument.ShortName">short name</see>
/// and <see cref="Argument.LongName">long name</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="longName">Long name of the argument </param>
/// <param name="allowedValues">Allowed values</param>
public EnumeratedValueArgument(char shortName, string longName, ICollection<TValue> allowedValues)
: base(shortName, longName)
{
_allowedValues = allowedValues;
}
/// <summary>
/// Creates new command line argument with a <see cref="Argument.ShortName">short name</see>,
/// <see cref="Argument.LongName">long name</see> and <see cref="Argument.Description">description</see>
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="longName">Long name of the argument </param>
/// <param name="description">Description of the argument</param>
/// <param name="allowedValues">Allowed values</param>
public EnumeratedValueArgument(char shortName, string longName, string description, ICollection<TValue> allowedValues)
: base(shortName, longName, description)
{
_allowedValues = allowedValues;
}
#endregion
/// <summary>
/// Checks whether the specified value belongs to
/// the set of <see cref="AllowedValues">allowed values</see>.
/// </summary>
/// <param name="value">value to certify</param>
/// <exception cref="CommandLineArgumentOutOfRangeException">thrown when <paramref name="value"/> does not belong to the set of allowed values.</exception>
protected override void Certify(TValue value)
{
bool ok;
if (IgnoreCase && typeof(TValue) == typeof(string) && value is string)
{
TValue found = _allowedValues.FirstOrDefault(av => StringComparer.CurrentCultureIgnoreCase.Compare(value.ToString(), av.ToString()) == 0);
ok = found != null;
if (ok)
{
base.Value = found;
}
}
else
{
ok = _allowedValues.Contains(value);
}
if (!ok)
throw new CommandLineArgumentOutOfRangeException(String.Format(
Messages.EXC_ARG_ENUM_OUT_OF_RANGE, Value,
Name), Name);
}
}
/// <summary>
/// <para>
/// Attribute for declaring a class' field a <see cref="EnumeratedValueArgument{TValue}"/> and
/// thus binding a field's value to a certain command line switch argument.
/// </para>
/// <para>
/// Instead of creating an argument explicitly, you can assign a class' field an argument
/// attribute and let the CommandLineParse take care of binding the attribute to the field.
/// </para>
/// </summary>
/// <remarks>Applicable to fields and properties (public).</remarks>
/// <remarks>Use <see cref="CommandLineParser.ExtractArgumentAttributes"/> for each object
/// you where you have declared argument attributes.</remarks>
public sealed class EnumeratedValueArgumentAttribute : ArgumentAttribute
{
private readonly Type _argumentType;
/// <summary>
/// Creates proper generic <see cref="BoundedValueArgument{TValue}"/> type for <paramref name="type"/>.
/// </summary>
/// <param name="type">type of the argument value</param>
/// <returns>generic type</returns>
private static Type CreateProperValueArgumentType(Type type)
{
Type genericType = typeof(EnumeratedValueArgument<>);
Type constructedType = genericType.MakeGenericType(type);
return constructedType;
}
/// <summary>
/// Creates new instance of EnumeratedValueArgumentAttribute. EnumeratedValueArgument
/// uses underlying <see cref="EnumeratedValueArgument{TValue}"/>.
/// </summary>
/// <param name="type">Type of the generic parameter of <see cref="EnumeratedValueArgument{TValue}"/>.</param>
/// <param name="shortName"><see cref="Argument.ShortName">short name</see> of the underlying argument</param>
/// <remarks>
/// Parameter <paramref name="type"/> has to be either built-in
/// type or has to define a static Parse(String, CultureInfo)
/// method for reading the value from string.
/// </remarks>
public EnumeratedValueArgumentAttribute(Type type, char shortName)
: base(CreateProperValueArgumentType(type), shortName)
{
_argumentType = CreateProperValueArgumentType(type);
}
/// <summary>
/// Creates new instance of EnumeratedValueArgumentAttribute. EnumeratedValueArgument
/// uses underlying <see cref="EnumeratedValueArgument{TValue}"/>.
/// </summary>
/// <param name="type">Type of the generic parameter of <see cref="EnumeratedValueArgument{TValue}"/>.</param>
/// <param name="longName"><see cref="Argument.LongName">short name</see> of the underlying argument</param>
/// <remarks>
/// Parameter <paramref name="type"/> has to be either built-in
/// type or has to define a static Parse(String, CultureInfo)
/// method for reading the value from string.
/// </remarks>
public EnumeratedValueArgumentAttribute(Type type, string longName)
: base(CreateProperValueArgumentType(type), longName)
{
_argumentType = CreateProperValueArgumentType(type);
}
/// <summary>
/// Creates new instance of EnumeratedValueArgument. EnumeratedValueArgument
/// uses underlying <see cref="EnumeratedValueArgument{TValue}"/>.
/// </summary>
/// <param name="type">Type of the generic parameter of <see cref="EnumeratedValueArgument{TValue}"/>.</param>
/// <param name="shortName"><see cref="Argument.ShortName">short name</see> of the underlying argument</param>
/// <param name="longName"><see cref="Argument.LongName">long name</see> of the underlying argument</param>
/// <remarks>
/// Parameter <paramref name="type"/> has to be either built-in
/// type or has to define a static Parse(String, CultureInfo)
/// method for reading the value from string.
/// </remarks>
public EnumeratedValueArgumentAttribute(Type type, char shortName, string longName)
: base(CreateProperValueArgumentType(type), shortName, longName)
{
_argumentType = CreateProperValueArgumentType(type);
}
/// <summary>
/// Allowed values of the argument, separated by commas or semicolons.
/// </summary>
public string AllowedValues
{
get
{
return string.Empty;
}
set
{
_argumentType.InvokeMethod("InitAllowedValues", Argument, value);
}
}
/// <summary>
/// Default value
/// </summary>
public object DefaultValue
{
get
{
return _argumentType.GetPropertyValue<object>("DefaultValue", Argument);
}
set
{
_argumentType.SetPropertyValue("DefaultValue", Argument, value);
}
}
/// <summary>
/// When set to true, argument can appear on the command line with or without value, e.g. both is allowed:
/// <code>
/// myexe.exe -Arg Value
/// OR
/// myexe.exe -Arg
/// </code>
/// </summary>
public bool ValueOptional
{
get
{
return _argumentType.GetPropertyValue<bool>("ValueOptional", Argument);
}
set
{
_argumentType.SetPropertyValue("ValueOptional", Argument, value);
}
}
/// <summary>
/// String arguments will be accepted even with differences in capitalisation (e.g. INFO will be accepted for info).
/// </summary>
public bool IgnoreCase
{
get
{
return _argumentType.GetPropertyValue<bool>("IgnoreCase", Argument);
}
set
{
_argumentType.SetPropertyValue("IgnoreCase", Argument, value);
}
}
}
}