-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdlineParseItem.cs
303 lines (253 loc) · 9.96 KB
/
CmdlineParseItem.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
namespace SearchAThing.Cmdline;
/// <summary>
/// types for cmd line parser item
/// </summary>
public enum CmdlineParseItemType
{
/// <summary>
/// command if present are mandatory to match one between commands at this parser level
/// </summary>
command,
/// <summary>
/// flag can be short, long or short/long and optional or mandatory; flag can require to have value association
/// </summary>
flag,
/// <summary>
/// parameter can be optional or mandatory and are discovered after commands, flags are parsed
/// </summary>
parameter,
/// <summary>
/// parameter array are strings at end of cmdline
/// </summary>
parameterArray
}
/// <summary>
/// describe properties required for a command, a flag or a parameter on the syntax for the cmdline parser;
/// enumerating returns the list of values if this is a flag with value or a parameter or a parameter array
/// </summary>
public class CmdlineParseItem : IEnumerable<string>
{
/// <summary>
/// parser which this item belongs to
/// </summary>
public CmdlineParser Parser { get; private set; }
/// <summary>
/// type of this cmdline parser
/// </summary>
public CmdlineParseItemType Type { get; private set; }
/// <summary>
/// true if this is a command
/// </summary>
public bool IsCommand => Type == CmdlineParseItemType.command;
/// <summary>
/// true if this is a flag
/// </summary>
public bool IsFlag => Type == CmdlineParseItemType.flag;
/// <summary>
/// true if this is a parameter or array of parameters
/// </summary>
public bool IsParameterOrArray => Type == CmdlineParseItemType.parameter || Type == CmdlineParseItemType.parameterArray;
/// <summary>
/// true if this is a parameter
/// </summary>
public bool IsParameter => Type == CmdlineParseItemType.parameter;
/// <summary>
/// true is this is an array of parameters
/// </summary>
public bool IsParameterArray => Type == CmdlineParseItemType.parameterArray;
/// <summary>
/// stores actiont to execute if this global flag item matches
/// </summary>
public Action<CmdlineParseItem> GlobalFlagAction { get; private set; }
internal bool GlobalFlagActionExecuted = false;
/// <summary>
/// if false global flag action will executed immeditaly; if true execution will deferred to nested parser
/// </summary>
public bool GlobalFlagActionNested { get; private set; }
/// <summary>
/// states if this parse item is a global flag
/// </summary>
public bool IsGlobal => GlobalFlagAction != null;
/// <summary>
/// true if this item has a short name ( maybe a command, parameter or a flag with short name )
/// </summary>
public bool HasShortName => !string.IsNullOrWhiteSpace(ShortName);
/// <summary>
/// short name of this element
/// </summary>
public string ShortName { get; private set; }
/// <summary>
/// short flag final string ( without value if any )
/// </summary>
public string ShortFlag => $"-{ShortName}";
/// <summary>
/// states if this element has a long name ( used only for flags )
/// </summary>
public bool HasLongName => !string.IsNullOrWhiteSpace(LongName);
/// <summary>
/// long name ( used only for flags )
/// </summary>
public string LongName { get; private set; }
/// <summary>
/// states if this item have a long name ( used only for flags )
/// </summary>
public string LongFlag => $"--{LongName}";
/// <summary>
/// short/long flag final string ( without value if any )
/// </summary>
public string ShortLongFlag => $"{(HasShortName ? ShortFlag : "")}{((HasShortName && HasLongName) ? "," : "")}{(HasLongName ? LongFlag : "")}";
/// <summary>
/// states if this flag requires a value
/// </summary>
public bool HasValueName => !string.IsNullOrWhiteSpace(ValueName);
/// <summary>
/// symbolic name for the flag value to display in the usage
/// </summary>
public string ValueName { get; private set; }
/// <summary>
/// description for this parse item to use in the usage or is parser description if this is a command
/// </summary>
public string Description { get; private set; }
/// <summary>
/// true if this item is mandatory
/// </summary>
public bool Mandatory { get; private set; }
/// <summary>
/// helper to sort items in groups with follow order Commands, Flags, Parameters, Parameters array
/// </summary>
public int SortOrder
{
get
{
switch (Type)
{
case CmdlineParseItemType.command: return 0;
case CmdlineParseItemType.flag:
{
if (Mandatory) return 1; else return 2;
}
case CmdlineParseItemType.parameter: return 3;
case CmdlineParseItemType.parameterArray: return 4;
default: return 100;
}
}
}
/// <summary>
/// custom function that user can set for parameter parser item type to define runtime completion rules
/// </summary>
internal Func<string, IEnumerable<string>> onCompletion = null;
/// <summary>
/// set a runtime completion function ( used for parameter item types )
/// </summary>
public void OnCompletion(Func<string, IEnumerable<string>> func)
{
onCompletion = func;
}
#region matches
/// <summary>
/// remove matches state and association to parser argument.
/// used internally to re-evaluate global flags so that they can associate with sub parsers
/// </summary>
internal void Unmatch()
{
Matches = false;
MatchParser = null;
if (MatchArgument != null) MatchArgument.MatchedItem = null;
MatchArgument = null;
}
/// <summary>
/// associate this item to given parser and argument
/// </summary>
internal void Match(CmdlineParser parser, CmdlineArgument arg)
{
if (IsCommand) parser.Command = this;
arg.MatchedItem = this;
this.MatchParser = parser;
this.Matches = true;
this.MatchArgument = arg;
}
/// <summary>
/// states if this flag have a matching with required usage
/// </summary>
public bool Matches { get; internal set; }
/// <summary>
/// used to distinguish sub parser for global flags
/// </summary>
public CmdlineParser MatchParser { get; private set; }
/// <summary>
/// original argument with that this parse item matches
/// </summary>
public CmdlineArgument MatchArgument { get; private set; }
/// <summary>
/// helper to check if this parse item meet usage rules
/// </summary>
public static implicit operator bool(CmdlineParseItem item) => item.Matches;
#endregion
#region values
List<CmdlineArgument> argValues = new List<CmdlineArgument>();
/// <summary>
/// original cmdline argument associated as value to this item (may a flag with value or parameter)
/// </summary>
public IReadOnlyList<CmdlineArgument> ArgValues => argValues;
List<string> values = new List<string>();
/// <summary>
/// values associated to this item (may a flag with value or parameter)
/// </summary>
public IReadOnlyList<string> Values => values;
/// <summary>
/// sets a list of cmdline arguments as values associated to this item
/// </summary>
internal void SetValues(IEnumerable<CmdlineArgument> valArgs)
{
argValues = valArgs.ToList();
values = valArgs.Select(w => w.Argument).ToList();
}
/// <summary>
/// set a single cmdline argument as value associated to this item
/// </summary>
internal void SetValue(CmdlineArgument valArg)
{
argValues = new List<CmdlineArgument>() { valArg };
values = new List<string>() { valArg.Argument };
}
/// <summary>
/// used when flag value argument mixes flag name and value
/// </summary>
internal void SetValue(CmdlineArgument valArg, string overrideVal)
{
argValues = new List<CmdlineArgument>() { valArg };
values = new List<string>() { overrideVal };
}
public IEnumerator<string> GetEnumerator() => Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
/// <summary>
/// helper to retrieve this item value ( used for flag value or single parameter )
/// </summary>
public static implicit operator string(CmdlineParseItem item) => item.FirstOrDefault();
#endregion
/// <summary>
/// construct a parse item
/// </summary>
/// <param name="parser">parser owner</param>
/// <param name="type">type of parse item</param>
/// <param name="shortName">short name (command,flag,parameter)</param>
/// <param name="longName">long name (flag)</param>
/// <param name="valueName">value name (flag with val)</param>
/// <param name="description">description (any item)</param>
/// <param name="mandatory">if true and not matches a message will reported</param>
/// <param name="globalFlagAction">if non null sets this flag as a global that can matched independently that command and other mandatory items</param>
/// <param name="globalFlagActionNested">if false global flag action will executed immeditaly; if true execution will deferred to nested parser</param>
internal CmdlineParseItem(CmdlineParser parser, CmdlineParseItemType type,
string shortName, string longName, string valueName, string description, bool mandatory, Action<CmdlineParseItem> globalFlagAction, bool globalFlagActionNested = true)
{
Parser = parser;
Type = type;
ShortName = shortName;
LongName = longName;
ValueName = valueName;
Description = description;
Mandatory = mandatory;
GlobalFlagAction = globalFlagAction;
GlobalFlagActionNested = globalFlagActionNested;
}
}