BadScript 2
Loading...
Searching...
No Matches
ParserResult.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;
6
7namespace CommandLine
8{
9 public sealed class TypeInfo
10 {
11 private TypeInfo(Type current, IEnumerable<Type> choices)
12 {
13 Current = current;
14 Choices = choices;
15 }
16
17 public Type Current { get; }
18
19 public IEnumerable<Type> Choices { get; }
20
21 internal static TypeInfo Create(Type current)
22 {
23 return new TypeInfo(current, Enumerable.Empty<Type>());
24 }
25
26 internal static TypeInfo Create(Type current, IEnumerable<Type> choices)
27 {
28 return new TypeInfo(current, choices);
29 }
30 }
31
35 public enum ParserResultType
36 {
40 Parsed,
41
46 }
47
56 public abstract class ParserResult<T>
57 {
58 internal ParserResult(IEnumerable<Error> errors, TypeInfo typeInfo)
59 {
60 Tag = ParserResultType.NotParsed;
61 TypeInfo = typeInfo ?? TypeInfo.Create(typeof(T));
62 Errors = errors ?? new Error[0];
63 Value = default;
64 }
65
66 internal ParserResult(T value, TypeInfo typeInfo)
67 {
68 Value = value ?? throw new ArgumentNullException(nameof(value));
69 Tag = ParserResultType.Parsed;
70 TypeInfo = typeInfo ?? TypeInfo.Create(value.GetType());
71 Errors = new Error[0];
72 }
73
77 public ParserResultType Tag { get; }
78
79 public TypeInfo TypeInfo { get; }
80
84 public T Value { get; }
85
89 public IEnumerable<Error> Errors { get; }
90 }
91
96 public sealed class Parsed<T> : ParserResult<T>, IEquatable<Parsed<T>>
97 {
98 internal Parsed(T value, TypeInfo typeInfo)
99 : base(value, typeInfo) { }
100
101 internal Parsed(T value)
102 : this(value, TypeInfo.Create(value.GetType())) { }
103
104#region IEquatable<Parsed<T>> Members
105
118 public bool Equals(Parsed<T> other)
119 {
120 if (other == null)
121 {
122 return false;
123 }
124
125 return Tag.Equals(other.Tag) && Value.Equals(other.Value);
126 }
127
128#endregion
129
130
141 public override bool Equals(object obj)
142 {
143 if (obj is Parsed<T> other)
144 {
145 return Equals(other);
146 }
147
148 return base.Equals(obj);
149 }
150
155 public override int GetHashCode()
156 {
157 return new { Tag, Value }.GetHashCode();
158 }
159 }
160
165 public sealed class NotParsed<T> : ParserResult<T>, IEquatable<NotParsed<T>>
166 {
167 internal NotParsed(TypeInfo typeInfo, IEnumerable<Error> errors)
168 : base(errors, typeInfo) { }
169
170#region IEquatable<NotParsed<T>> Members
171
184 public bool Equals(NotParsed<T> other)
185 {
186 if (other == null)
187 {
188 return false;
189 }
190
191 return Tag.Equals(other.Tag) && Errors.SequenceEqual(other.Errors);
192 }
193
194#endregion
195
196
207 public override bool Equals(object obj)
208 {
209 if (obj is NotParsed<T> other)
210 {
211 return Equals(other);
212 }
213
214 return base.Equals(obj);
215 }
216
221 public override int GetHashCode()
222 {
223 return new { Tag, Errors }.GetHashCode();
224 }
225 }
226}
Base type of all errors.
Definition Error.cs:110
It contains a sequence of CommandLine.Error.
override int GetHashCode()
Serves as a hash function for a particular type.
NotParsed(TypeInfo typeInfo, IEnumerable< Error > errors)
bool Equals(NotParsed< T > other)
Returns a value that indicates whether the current instance and a specified CommandLine....
override bool Equals(object obj)
Determines whether the specified System.Object is equal to the current System.Object.
It contains an instance of type T with parsed values.
override bool Equals(object obj)
Determines whether the specified System.Object is equal to the current System.Object.
bool Equals(Parsed< T > other)
Returns a value that indicates whether the current instance and a specified CommandLine....
override int GetHashCode()
Serves as a hash function for a particular type.
Parsed(T value, TypeInfo typeInfo)
Models a parser result. When inherited by CommandLine.Parsed<T>, it contains an instance of type T w...
ParserResult(T value, TypeInfo typeInfo)
ParserResult(IEnumerable< Error > errors, TypeInfo typeInfo)
IEnumerable< Error > Errors
Gets the sequence of parsing errors. If there are no errors, then an empty IEnumerable is returned.
ParserResultType Tag
Parser result type discriminator, defined as CommandLine.ParserResultType enumeration.
T Value
Gets the instance with parsed values. If one or more errors occures, default is returned.
IEnumerable< Type > Choices
TypeInfo(Type current, IEnumerable< Type > choices)
static TypeInfo Create(Type current, IEnumerable< Type > choices)
static TypeInfo Create(Type current)
ParserResultType
Discriminator enumeration of CommandLine.ParserResultType derivates.