-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathToken.cs
186 lines (153 loc) · 4.67 KB
/
Token.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
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
namespace CommandLine.Core
{
enum TokenType { Name, Value }
abstract class Token
{
private readonly TokenType tag;
private readonly string text;
protected Token(TokenType tag, string text)
{
this.tag = tag;
this.text = text;
}
public static Token Name(string text)
{
return new Name(text);
}
public static Token Value(string text)
{
return new Value(text);
}
public static Token Value(string text, bool explicitlyAssigned)
{
return new Value(text, explicitlyAssigned);
}
public static Token ValueForced(string text)
{
return new Value(text, false, true, false);
}
public static Token ValueFromSeparator(string text)
{
return new Value(text, false, false, true);
}
public TokenType Tag
{
get { return tag; }
}
public string Text
{
get { return text; }
}
}
class Name : Token, IEquatable<Name>
{
public Name(string text)
: base(TokenType.Name, text)
{
}
public override bool Equals(object obj)
{
if (obj is Name other)
{
return Equals(other);
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return new {Tag, Text}.GetHashCode();
}
public bool Equals(Name other)
{
if (other == null)
{
return false;
}
return Tag.Equals(other.Tag) && Text.Equals(other.Text);
}
}
class Value : Token, IEquatable<Value>
{
private readonly bool explicitlyAssigned;
private readonly bool forced;
private readonly bool fromSeparator;
public Value(string text)
: this(text, false, false, false)
{
}
public Value(string text, bool explicitlyAssigned)
: this(text, explicitlyAssigned, false, false)
{
}
public Value(string text, bool explicitlyAssigned, bool forced, bool fromSeparator)
: base(TokenType.Value, text)
{
this.explicitlyAssigned = explicitlyAssigned;
this.forced = forced;
this.fromSeparator = fromSeparator;
}
/// <summary>
/// Whether this value came from a long option with "=" separating the name from the value
/// </summary>
public bool ExplicitlyAssigned
{
get { return explicitlyAssigned; }
}
/// <summary>
/// Whether this value came from a sequence specified with a separator (e.g., "--files a.txt,b.txt,c.txt")
/// </summary>
public bool FromSeparator
{
get { return fromSeparator; }
}
/// <summary>
/// Whether this value came from args after the -- separator (when EnableDashDash = true)
/// </summary>
public bool Forced
{
get { return forced; }
}
public override bool Equals(object obj)
{
var other = obj as Value;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return new { Tag, Text }.GetHashCode();
}
public bool Equals(Value other)
{
if (other == null)
{
return false;
}
return Tag.Equals(other.Tag) && Text.Equals(other.Text) && this.Forced == other.Forced;
}
}
static class TokenExtensions
{
public static bool IsName(this Token token)
{
return token.Tag == TokenType.Name;
}
public static bool IsValue(this Token token)
{
return token.Tag == TokenType.Value;
}
public static bool IsValueFromSeparator(this Token token)
{
return token.IsValue() && ((Value)token).FromSeparator;
}
public static bool IsValueForced(this Token token)
{
return token.IsValue() && ((Value)token).Forced;
}
}
}