forked from dotnet/codeformatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineParser.cs
More file actions
269 lines (248 loc) · 9.7 KB
/
Copy pathCommandLineParser.cs
File metadata and controls
269 lines (248 loc) · 9.7 KB
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
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.CodeFormatting;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFormatter
{
public enum Operation
{
Format,
ListRules,
}
public sealed class CommandLineOptions
{
public static readonly CommandLineOptions ListRules = new CommandLineOptions(
Operation.ListRules,
ImmutableArray<string[]>.Empty,
ImmutableArray<string>.Empty,
ImmutableDictionary<string, bool>.Empty,
ImmutableArray<string>.Empty,
ImmutableArray<string>.Empty,
null,
allowTables: false,
verbose: false);
public readonly Operation Operation;
public readonly ImmutableArray<string[]> PreprocessorConfigurations;
public readonly ImmutableArray<string> CopyrightHeader;
public readonly ImmutableDictionary<string, bool> RuleMap;
public readonly ImmutableArray<string> FormatTargets;
public readonly ImmutableArray<string> FileNames;
public readonly string Language;
public readonly bool AllowTables;
public readonly bool Verbose;
public CommandLineOptions(
Operation operation,
ImmutableArray<string[]> preprocessorConfigurations,
ImmutableArray<string> copyrightHeader,
ImmutableDictionary<string, bool> ruleMap,
ImmutableArray<string> formatTargets,
ImmutableArray<string> fileNames,
string language,
bool allowTables,
bool verbose)
{
Operation = operation;
PreprocessorConfigurations = preprocessorConfigurations;
CopyrightHeader = copyrightHeader;
RuleMap = ruleMap;
FileNames = fileNames;
FormatTargets = formatTargets;
Language = language;
AllowTables = allowTables;
Verbose = verbose;
}
}
public sealed class CommandLineParseResult
{
private readonly CommandLineOptions _options;
private readonly string _error;
public bool IsSuccess
{
get { return _options != null; }
}
public bool IsError
{
get { return !IsSuccess; }
}
public CommandLineOptions Options
{
get
{
Debug.Assert(IsSuccess);
return _options;
}
}
public string Error
{
get
{
Debug.Assert(IsError);
return _error;
}
}
private CommandLineParseResult(CommandLineOptions options = null, string error = null)
{
_options = options;
_error = error;
}
public static CommandLineParseResult CreateSuccess(CommandLineOptions options)
{
return new CommandLineParseResult(options: options);
}
public static CommandLineParseResult CreateError(string error)
{
return new CommandLineParseResult(error: error);
}
}
public static class CommandLineParser
{
private const string FileSwitch = "/file:";
private const string ConfigSwitch = "/c:";
private const string CopyrightSwitch = "/copyright:";
private const string LanguageSwitch = "/lang:";
private const string RuleEnabledSwitch1 = "/rule+:";
private const string RuleEnabledSwitch2 = "/rule:";
private const string RuleDisabledSwitch = "/rule-:";
private const string Usage =
@"CodeFormatter [/file:<filename>] [/lang:<language>] [/c:<config>[,<config>...]>]
[/copyright:<file> | /nocopyright] [/tables] [/nounicode]
[/rule(+|-):rule1,rule2,...] [/verbose]
<project, solution or response file>
/file - Only apply changes to files with specified name
/lang - Specifies the language to use when a responsefile is
specified. i.e. 'C#', 'Visual Basic', ... (default: 'C#')
/c - Additional preprocessor configurations the formatter
should run under.
/copyright - Specifies file containing copyright header.
Use ConvertTests to convert MSTest tests to xUnit.
/nocopyright - Do not update the copyright message.
/tables - Let tables opt out of formatting by defining
DOTNET_FORMATTER
/nounicode - Do not convert unicode strings to escape sequences
/rule(+|-) - Enable (default) or disable the specified rule
/rules - List the available rules
/verbose - Verbose output
";
public static void PrintUsage()
{
Console.WriteLine(Usage);
}
public static bool TryParse(string[] args, out CommandLineOptions options)
{
var result = Parse(args);
options = result.IsSuccess ? result.Options : null;
return result.IsSuccess;
}
public static CommandLineParseResult Parse(string[] args)
{
var comparer = StringComparer.OrdinalIgnoreCase;
var comparison = StringComparison.OrdinalIgnoreCase;
var formatTargets = new List<string>();
var fileNames = new List<string>();
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
var copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
var ruleMap = ImmutableDictionary<string, bool>.Empty;
var language = LanguageNames.CSharp;
var allowTables = false;
var verbose = false;
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith(ConfigSwitch, StringComparison.OrdinalIgnoreCase))
{
var all = arg.Substring(ConfigSwitch.Length);
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
configBuilder.Add(configs);
}
else if (arg.StartsWith(CopyrightSwitch, StringComparison.OrdinalIgnoreCase))
{
var fileName = arg.Substring(CopyrightSwitch.Length);
try
{
copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
}
catch (Exception ex)
{
string error = string.Format("Could not read {0}{1}{2}",
fileName,
Environment.NewLine,
ex.Message);
return CommandLineParseResult.CreateError(error);
}
}
else if (arg.StartsWith(LanguageSwitch, StringComparison.OrdinalIgnoreCase))
{
language = arg.Substring(LanguageSwitch.Length);
}
else if (comparer.Equals(arg, "/nocopyright"))
{
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
}
else if (comparer.Equals(arg, "/nounicode"))
{
ruleMap = ruleMap.SetItem(FormattingDefaults.UnicodeLiteralsRuleName, false);
}
else if (comparer.Equals(arg, "/verbose"))
{
verbose = true;
}
else if (arg.StartsWith(FileSwitch, comparison))
{
fileNames.Add(arg.Substring(FileSwitch.Length));
}
else if (arg.StartsWith(RuleEnabledSwitch1, comparison))
{
UpdateRuleMap(ref ruleMap, arg.Substring(RuleEnabledSwitch1.Length), enabled: true);
}
else if (arg.StartsWith(RuleEnabledSwitch2, comparison))
{
UpdateRuleMap(ref ruleMap, arg.Substring(RuleEnabledSwitch2.Length), enabled: true);
}
else if (arg.StartsWith(RuleDisabledSwitch, comparison))
{
UpdateRuleMap(ref ruleMap, arg.Substring(RuleDisabledSwitch.Length), enabled: false);
}
else if (comparer.Equals(arg, "/tables"))
{
allowTables = true;
}
else if (comparer.Equals(arg, "/rules"))
{
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules);
}
else
{
formatTargets.Add(arg);
}
}
if (formatTargets.Count == 0)
{
return CommandLineParseResult.CreateError("Must specify at least one project / solution / rsp to format");
}
var options = new CommandLineOptions(
Operation.Format,
configBuilder.ToImmutableArray(),
copyrightHeader,
ruleMap,
formatTargets.ToImmutableArray(),
fileNames.ToImmutableArray(),
language,
allowTables,
verbose);
return CommandLineParseResult.CreateSuccess(options);
}
private static void UpdateRuleMap(ref ImmutableDictionary<string, bool> ruleMap, string data, bool enabled)
{
foreach (var current in data.Split(','))
{
ruleMap = ruleMap.SetItem(current, enabled);
}
}
}
}