BadScript 2
Loading...
Searching...
No Matches
Token.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;
4
5namespace CommandLine.Core
6{
7 internal enum TokenType
8 {
9 Name,
10 Value,
11 }
12
13 internal abstract class Token
14 {
15 protected Token(TokenType tag, string text)
16 {
17 Tag = tag;
18 Text = text;
19 }
20
21 public TokenType Tag { get; }
22
23 public string Text { get; }
24
25 public static Token Name(string text)
26 {
27 return new Name(text);
28 }
29
30 public static Token Value(string text)
31 {
32 return new Value(text);
33 }
34
35 public static Token Value(string text, bool explicitlyAssigned)
36 {
37 return new Value(text, explicitlyAssigned);
38 }
39
40 public static Token ValueForced(string text)
41 {
42 return new Value(text, false, true, false);
43 }
44
45 public static Token ValueFromSeparator(string text)
46 {
47 return new Value(text, false, false, true);
48 }
49 }
50
51 internal class Name : Token, IEquatable<Name>
52 {
53 public Name(string text)
54 : base(TokenType.Name, text) { }
55
56#region IEquatable<Name> Members
57
58 public bool Equals(Name other)
59 {
60 if (other == null)
61 {
62 return false;
63 }
64
65 return Tag.Equals(other.Tag) && Text.Equals(other.Text);
66 }
67
68#endregion
69
70 public override bool Equals(object obj)
71 {
72 Name other = obj as Name;
73
74 if (other != null)
75 {
76 return Equals(other);
77 }
78
79 return base.Equals(obj);
80 }
81
82 public override int GetHashCode()
83 {
84 return new { Tag, Text }.GetHashCode();
85 }
86 }
87
88 internal class Value : Token, IEquatable<Value>
89 {
90 public Value(string text)
91 : this(text, false, false, false) { }
92
93 public Value(string text, bool explicitlyAssigned)
94 : this(text, explicitlyAssigned, false, false) { }
95
96 public Value(string text, bool explicitlyAssigned, bool forced, bool fromSeparator)
97 : base(TokenType.Value, text)
98 {
99 ExplicitlyAssigned = explicitlyAssigned;
100 Forced = forced;
101 FromSeparator = fromSeparator;
102 }
103
107 public bool ExplicitlyAssigned { get; }
108
112 public bool FromSeparator { get; }
113
117 public bool Forced { get; }
118
119#region IEquatable<Value> Members
120
121 public bool Equals(Value other)
122 {
123 if (other == null)
124 {
125 return false;
126 }
127
128 return Tag.Equals(other.Tag) && Text.Equals(other.Text) && Forced == other.Forced;
129 }
130
131#endregion
132
133 public override bool Equals(object obj)
134 {
135 Value other = obj as Value;
136
137 if (other != null)
138 {
139 return Equals(other);
140 }
141
142 return base.Equals(obj);
143 }
144
145 public override int GetHashCode()
146 {
147 return new { Tag, Text }.GetHashCode();
148 }
149 }
150
151 internal static class TokenExtensions
152 {
153 public static bool IsName(this Token token)
154 {
155 return token.Tag == TokenType.Name;
156 }
157
158 public static bool IsValue(this Token token)
159 {
160 return token.Tag == TokenType.Value;
161 }
162
163 public static bool IsValueFromSeparator(this Token token)
164 {
165 return token.IsValue() && ((Value)token).FromSeparator;
166 }
167
168 public static bool IsValueForced(this Token token)
169 {
170 return token.IsValue() && ((Value)token).Forced;
171 }
172 }
173}
Name(string text)
Definition Token.cs:53
override int GetHashCode()
Definition Token.cs:82
override bool Equals(object obj)
Definition Token.cs:70
bool Equals(Name other)
Definition Token.cs:58
static bool IsValueFromSeparator(this Token token)
Definition Token.cs:163
static bool IsValueForced(this Token token)
Definition Token.cs:168
static bool IsName(this Token token)
Definition Token.cs:153
static bool IsValue(this Token token)
Definition Token.cs:158
static Token Value(string text, bool explicitlyAssigned)
Definition Token.cs:35
static Token Value(string text)
Definition Token.cs:30
static Token Name(string text)
Definition Token.cs:25
Token(TokenType tag, string text)
Definition Token.cs:15
static Token ValueForced(string text)
Definition Token.cs:40
static Token ValueFromSeparator(string text)
Definition Token.cs:45
bool Forced
Whether this value came from args after the – separator (when EnableDashDash = true)
Definition Token.cs:117
bool Equals(Value other)
Definition Token.cs:121
Value(string text)
Definition Token.cs:90
override bool Equals(object obj)
Definition Token.cs:133
bool FromSeparator
Whether this value came from a sequence specified with a separator (e.g., "--files a....
Definition Token.cs:112
Value(string text, bool explicitlyAssigned, bool forced, bool fromSeparator)
Definition Token.cs:96
Value(string text, bool explicitlyAssigned)
Definition Token.cs:93
bool ExplicitlyAssigned
Whether this value came from a long option with "=" separating the name from the value.
Definition Token.cs:107
override int GetHashCode()
Definition Token.cs:145