This repository was archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathCommandLineParser.cs
178 lines (167 loc) · 7.61 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
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.CodeFormatting;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
namespace CodeFormatter
{
public static class CommandLineParser
{
private const string FileSwitch = "/file:";
private const string ConfigSwitch = "/c:";
private const string CopyrightWithFileSwitch = "/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>]] [/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(+|-) - Enables or disables (default) updating the copyright
header in files, optionally specifying a file
containing a custom copyright header.
/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
/help - Displays this usage message (short form: /?)
";
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 = ImmutableArray<string>.Empty;
var ruleMap = ImmutableDictionary<string, bool>.Empty;
var language = LanguageNames.CSharp;
var allowTables = false;
var verbose = false;
foreach (var arg in args)
{
if (arg.StartsWith(ConfigSwitch, comparison))
{
var all = arg.Substring(ConfigSwitch.Length);
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
configBuilder.Add(configs);
}
else if (comparer.Equals(arg, "/copyright+") || comparer.Equals(arg, "/copyright"))
{
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);
copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
}
else if (arg.StartsWith(CopyrightWithFileSwitch, comparison))
{
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);
var fileName = arg.Substring(CopyrightWithFileSwitch.Length);
try
{
copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
}
catch (Exception ex)
{
string error = $"Could not read {fileName}{Environment.NewLine}{ex.Message}";
return CommandLineParseResult.CreateError(error);
}
}
else if (comparer.Equals(arg, "/copyright-") || comparer.Equals(arg, "/nocopyright"))
{ // We still check /nocopyright for backwards compat
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
}
else if (arg.StartsWith(LanguageSwitch, comparison))
{
language = arg.Substring(LanguageSwitch.Length);
}
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 if (comparer.Equals(arg, "/?") || comparer.Equals(arg, "-?") || comparer.Equals(arg, "/help") ||
comparer.Equals(arg, "-help") || comparer.Equals(arg, "--help"))
{
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ShowHelp);
}
else if (arg.StartsWith("/", comparison))
{
return CommandLineParseResult.CreateError($"Unrecognized option \"{arg}\"");
}
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);
}
}
}
}