forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CommandLineParser.cs
268 lines (211 loc) · 8.21 KB
/
CommandLineParser.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
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;
namespace EffectCompiler
{
// Reusable, reflection based helper for parsing commandline options.
//
// From Shawn Hargreaves Blog:
// http://blogs.msdn.com/b/shawnhar/archive/2012/04/20/a-reusable-reflection-based-command-line-parser.aspx
//
public class CommandLineParser
{
object _optionsObject;
Queue<FieldInfo> _requiredOptions = new Queue<FieldInfo>();
Dictionary<string, FieldInfo> _optionalOptions = new Dictionary<string, FieldInfo>();
List<string> _requiredUsageHelp = new List<string>();
List<string> _optionalUsageHelp = new List<string>();
// Constructor.
public CommandLineParser(object optionsObject)
{
this._optionsObject = optionsObject;
// Reflect to find what commandline options are available.
foreach (var field in optionsObject.GetType().GetFields())
{
String description;
var fieldName = GetOptionNameAndDescription(field, out description);
if (GetAttribute<RequiredAttribute>(field) != null)
{
// Record a required option.
_requiredOptions.Enqueue(field);
_requiredUsageHelp.Add(string.Format("<{0}> {1}", fieldName, description));
}
else
{
// Record an optional option.
_optionalOptions.Add(fieldName.ToLowerInvariant(), field);
if (field.FieldType == typeof(bool))
_optionalUsageHelp.Add(string.Format("/{0} {1}", fieldName, description));
else
_optionalUsageHelp.Add(string.Format("/{0}:value {1}", fieldName, description));
}
}
}
public bool ParseCommandLine(string[] args)
{
// Parse each argument in turn.
foreach (var arg in args)
{
if (!ParseArgument(arg.Trim()))
return false;
}
// Make sure we got all the required options.
var missingRequiredOption = _requiredOptions.FirstOrDefault(field => !IsList(field) || GetList(field).Count == 0);
if (missingRequiredOption != null)
{
ShowError("Missing argument '{0}'", GetOptionName(missingRequiredOption));
return false;
}
return true;
}
bool ParseArgument(string arg)
{
if (_requiredOptions.Count > 0)
{
// Parse the next non escaped argument.
var field = _requiredOptions.Peek();
if (!IsList(field))
_requiredOptions.Dequeue();
return SetOption(field, arg);
}
else if (arg.StartsWith("/"))
{
// After the first escaped argument we can no
// longer read non-escaped arguments.
_requiredOptions.Clear();
// Parse an optional argument.
char[] separators = { ':' };
var split = arg.Substring(1).Split(separators, 2, StringSplitOptions.None);
var name = split[0];
var value = (split.Length > 1) ? split[1] : "true";
FieldInfo field;
if (!_optionalOptions.TryGetValue(name.ToLowerInvariant(), out field))
{
ShowError("Unknown option '{0}'", name);
return false;
}
return SetOption(field, value);
}
ShowError("Too many arguments");
return false;
}
bool SetOption(FieldInfo field, string value)
{
try
{
if (IsList(field))
{
// Append this value to a list of options.
GetList(field).Add(ChangeType(value, ListElementType(field)));
}
else
{
// Set the value of a single option.
field.SetValue(_optionsObject, ChangeType(value, field.FieldType));
}
return true;
}
catch
{
ShowError("Invalid value '{0}' for option '{1}'", value, GetOptionName(field));
return false;
}
}
static object ChangeType(string value, Type type)
{
var converter = TypeDescriptor.GetConverter(type);
return converter.ConvertFromInvariantString(value);
}
static bool IsList(FieldInfo field)
{
return typeof(IList).IsAssignableFrom(field.FieldType);
}
IList GetList(FieldInfo field)
{
return (IList)field.GetValue(_optionsObject);
}
static Type ListElementType(FieldInfo field)
{
var interfaces = from i in field.FieldType.GetInterfaces()
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>)
select i;
return interfaces.First().GetGenericArguments()[0];
}
static string GetOptionName(FieldInfo field)
{
var nameAttribute = GetAttribute<NameAttribute>(field);
if (nameAttribute != null)
return nameAttribute.Name;
else
return field.Name;
}
static string GetOptionNameAndDescription(FieldInfo field, out String description)
{
var nameAttribute = GetAttribute<NameAttribute>(field);
if (nameAttribute != null)
{
description = nameAttribute.Description;
return nameAttribute.Name;
}
else
{
description = null;
return field.Name;
}
}
public string Title { get; set; }
void ShowError(string message, params object[] args)
{
var name = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName);
if (!string.IsNullOrEmpty(Title))
{
Console.Error.WriteLine(Title);
Console.Error.WriteLine();
}
Console.Error.WriteLine(message, args);
Console.Error.WriteLine();
Console.Error.WriteLine("Usage: {0} {1}", name, string.Join(" ", _requiredUsageHelp));
if (_optionalUsageHelp.Count > 0)
{
Console.Error.WriteLine();
Console.Error.WriteLine("Options:");
foreach (string optional in _optionalUsageHelp)
Console.Error.WriteLine(" {0}", optional);
}
}
static T GetAttribute<T>(ICustomAttributeProvider provider) where T : Attribute
{
return provider.GetCustomAttributes(typeof(T), false).OfType<T>().FirstOrDefault();
}
// Used on optionsObject fields to indicate which options are required.
[AttributeUsage(AttributeTargets.Field)]
public sealed class RequiredAttribute : Attribute
{
}
// Used on an optionsObject field to rename the corresponding commandline option.
[AttributeUsage(AttributeTargets.Field)]
public class NameAttribute : Attribute
{
public NameAttribute(string name)
{
Name = name;
Description = null;
}
public NameAttribute(string name, string description)
{
Name = name;
Description = description;
}
public string Name { get; private set; }
public string Description { get; protected set; }
}
}
}