-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathError.cs
516 lines (462 loc) · 17.4 KB
/
Error.cs
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
namespace CommandLine
{
/// <summary>
/// Discriminator enumeration of <see cref="CommandLine.Error"/> derivates.
/// </summary>
public enum ErrorType
{
/// <summary>
/// Value of <see cref="CommandLine.BadFormatTokenError"/> type.
/// </summary>
BadFormatTokenError,
/// <summary>
/// Value of <see cref="CommandLine.MissingValueOptionError"/> type.
/// </summary>
MissingValueOptionError,
/// <summary>
/// Value of <see cref="CommandLine.UnknownOptionError"/> type.
/// </summary>
UnknownOptionError,
/// <summary>
/// Value of <see cref="CommandLine.MissingRequiredOptionError"/> type.
/// </summary>
MissingRequiredOptionError,
/// <summary>
/// Value of <see cref="CommandLine.MutuallyExclusiveSetError"/> type.
/// </summary>
MutuallyExclusiveSetError,
/// <summary>
/// Value of <see cref="CommandLine.BadFormatConversionError"/> type.
/// </summary>
BadFormatConversionError,
/// <summary>
/// Value of <see cref="CommandLine.SequenceOutOfRangeError"/> type.
/// </summary>
SequenceOutOfRangeError,
/// <summary>
/// Value of <see cref="CommandLine.RepeatedOptionError"/> type.
/// </summary>
RepeatedOptionError,
/// <summary>
/// Value of <see cref="CommandLine.NoVerbSelectedError"/> type.
/// </summary>
NoVerbSelectedError,
/// <summary>
/// Value of <see cref="CommandLine.BadVerbSelectedError"/> type.
/// </summary>
BadVerbSelectedError,
/// <summary>
/// Value of <see cref="CommandLine.HelpRequestedError"/> type.
/// </summary>
HelpRequestedError,
/// <summary>
/// Value of <see cref="CommandLine.HelpVerbRequestedError"/> type.
/// </summary>
HelpVerbRequestedError,
/// <summary>
/// Value of <see cref="CommandLine.VersionRequestedError"/> type.
/// </summary>
VersionRequestedError,
/// <summary>
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
/// </summary>
SetValueExceptionError
VersionRequestedError,
/// <summary>
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
/// </summary>
InvalidAttributeConfigurationError
}
/// <summary>
/// Base type of all errors.
/// </summary>
/// <remarks>All errors are defined within the system. There's no reason to create custom derivate types.</remarks>
public abstract class Error : IEquatable<Error>
{
private readonly ErrorType tag;
private readonly bool stopsProcessing;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Error"/> class.
/// </summary>
/// <param name="tag">Type discriminator tag.</param>
/// <param name="stopsProcessing">Tells if error stops parsing process.</param>
protected internal Error(ErrorType tag, bool stopsProcessing)
{
this.tag = tag;
this.stopsProcessing = stopsProcessing;
}
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Error"/> class.
/// </summary>
/// <param name="tag">Type discriminator tag.</param>
protected internal Error(ErrorType tag)
: this(tag, false)
{
}
/// <summary>
/// Error type discriminator, defined as <see cref="CommandLine.ErrorType"/> enumeration.
/// </summary>
public ErrorType Tag
{
get { return tag; }
}
/// <summary>
/// Tells if error stops parsing process.
/// Filtered by <see cref="CommandLine.ErrorExtensions.OnlyMeaningfulOnes(System.Collections.Generic.IEnumerable{Error})"/>.
/// </summary>
public bool StopsProcessing
{
get { return stopsProcessing; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as Error;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { Tag, StopsProcessing }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.Error"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.Error"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.Error"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(Error other)
{
if (other == null)
{
return false;
}
return Tag.Equals(other.Tag);
}
}
/// <summary>
/// Base type of all errors related to bad token detection.
/// </summary>
public abstract class TokenError : Error, IEquatable<TokenError>
{
private readonly string token;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.TokenError"/> class.
/// </summary>
/// <param name="tag">Error type.</param>
/// <param name="token">Problematic token.</param>
protected internal TokenError(ErrorType tag, string token)
: base(tag)
{
if (token == null) throw new ArgumentNullException("token");
this.token = token;
}
/// <summary>
/// The string containing the token text.
/// </summary>
public string Token
{
get { return token; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as TokenError;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new {Tag, StopsProcessing, Token}.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.TokenError"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.TokenError"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.TokenError"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(TokenError other)
{
if (other == null)
{
return false;
}
return Tag.Equals(other.Tag) && Token.Equals(other.Token);
}
}
/// <summary>
/// Models an error generated when an invalid token is detected.
/// </summary>
public sealed class BadFormatTokenError : TokenError
{
internal BadFormatTokenError(string token)
: base(ErrorType.BadFormatTokenError, token)
{
}
}
/// <summary>
/// Base type of all erros with name information.
/// </summary>
public abstract class NamedError : Error, IEquatable<NamedError>
{
private readonly NameInfo nameInfo;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.NamedError"/> class.
/// </summary>
/// <param name="tag">Error type.</param>
/// <param name="nameInfo">Problematic name.</param>
protected internal NamedError(ErrorType tag, NameInfo nameInfo)
: base(tag)
{
this.nameInfo = nameInfo;
}
/// <summary>
/// Name information relative to this error instance.
/// </summary>
public NameInfo NameInfo
{
get { return nameInfo; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as NamedError;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new {Tag, StopsProcessing, NameInfo}.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.NamedError"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.NamedError"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.NamedError"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(NamedError other)
{
if (other == null)
{
return false;
}
return Tag.Equals(other.Tag) && NameInfo.Equals(other.NameInfo);
}
}
/// <summary>
/// Models an error generated when an option lacks its value.
/// </summary>
public sealed class MissingValueOptionError : NamedError
{
internal MissingValueOptionError(NameInfo nameInfo)
: base(ErrorType.MissingValueOptionError, nameInfo)
{
}
}
/// <summary>
/// Models an error generated when an unknown option is detected.
/// </summary>
public sealed class UnknownOptionError : TokenError
{
internal UnknownOptionError(string token)
: base(ErrorType.UnknownOptionError, token)
{
}
}
/// <summary>
/// Models an error generated when a required option is required.
/// </summary>
public sealed class MissingRequiredOptionError : NamedError
{
internal MissingRequiredOptionError(NameInfo nameInfo)
: base(ErrorType.MissingRequiredOptionError, nameInfo)
{
}
}
/// <summary>
/// Models an error generated when a an option from another set is defined.
/// </summary>
public sealed class MutuallyExclusiveSetError : NamedError
{
private readonly string setName;
internal MutuallyExclusiveSetError(NameInfo nameInfo, string setName)
: base(ErrorType.MutuallyExclusiveSetError, nameInfo)
{
this.setName = setName;
}
/// <summary>
/// Option's set name.
/// </summary>
public string SetName
{
get { return setName; }
}
}
/// <summary>
/// Models an error generated when a value conversion fails.
/// </summary>
public sealed class BadFormatConversionError : NamedError
{
internal BadFormatConversionError(NameInfo nameInfo)
: base(ErrorType.BadFormatConversionError, nameInfo)
{
}
}
/// <summary>
/// Models an error generated when a sequence value lacks elements.
/// </summary>
public sealed class SequenceOutOfRangeError : NamedError
{
internal SequenceOutOfRangeError(NameInfo nameInfo)
: base(ErrorType.SequenceOutOfRangeError, nameInfo)
{
}
}
/// <summary>
/// Models an error generated when an option is repeated two or more times.
/// </summary>
public sealed class RepeatedOptionError : NamedError
{
internal RepeatedOptionError(NameInfo nameInfo)
: base(ErrorType.RepeatedOptionError, nameInfo)
{
}
}
/// <summary>
/// Models an error generated when an unknown verb is detected.
/// </summary>
public sealed class BadVerbSelectedError : TokenError
{
internal BadVerbSelectedError(string token)
: base(ErrorType.BadVerbSelectedError, token)
{
}
}
/// <summary>
/// Models an error generated when a user explicitly requests help.
/// </summary>
public sealed class HelpRequestedError : Error
{
internal HelpRequestedError()
: base(ErrorType.HelpRequestedError, true)
{
}
}
/// <summary>
/// Models an error generated when a user explicitly requests help in verb commands scenario.
/// </summary>
public sealed class HelpVerbRequestedError : Error
{
private readonly string verb;
private readonly Type type;
private readonly bool matched;
internal HelpVerbRequestedError(string verb, Type type, bool matched)
: base(ErrorType.HelpVerbRequestedError, true)
{
this.verb = verb;
this.type = type;
this.matched = matched;
}
/// <summary>
/// Verb command string.
/// </summary>
public string Verb
{
get { return verb; }
}
/// <summary>
/// <see cref="System.Type"/> of verb command.
/// </summary>
public Type Type
{
get { return type; }
}
/// <summary>
/// <value>true</value> if verb command is found; otherwise <value>false</value>.
/// </summary>
public bool Matched
{
get { return matched; }
}
}
/// <summary>
/// Models an error generated when no verb is selected.
/// </summary>
public sealed class NoVerbSelectedError : Error
{
internal NoVerbSelectedError()
: base(ErrorType.NoVerbSelectedError)
{
}
}
/// <summary>
/// Models an error generated when a user explicitly requests version.
/// </summary>
public sealed class VersionRequestedError : Error
{
internal VersionRequestedError()
: base(ErrorType.VersionRequestedError, true)
{
}
}
/// <summary>
/// Models as error generated when exception is thrown at Property.SetValue
/// </summary>
public sealed class SetValueExceptionError : NamedError
{
private readonly Exception exception;
private readonly object value;
internal SetValueExceptionError(NameInfo nameInfo, Exception exception, object value)
: base(ErrorType.SetValueExceptionError, nameInfo)
{
this.exception = exception;
this.value = value;
}
/// <summary>
/// The expection thrown from Property.SetValue
/// </summary>
public Exception Exception
{
get { return exception; }
}
/// <summary>
/// The value that had to be set to the property
/// </summary>
public object Value
{
get { return value; }
}
}
}