3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Collections . Immutable ;
6
- using System . Diagnostics ;
7
6
using System . IO ;
8
- using System . Linq ;
9
- using System . Text ;
10
- using System . Threading . Tasks ;
11
7
12
8
namespace CodeFormatter
13
9
{
14
- public enum Operation
15
- {
16
- Format ,
17
- ListRules ,
18
- ShowHelp
19
- }
20
-
21
- public sealed class CommandLineOptions
22
- {
23
- public static readonly CommandLineOptions ListRules = new CommandLineOptions (
24
- Operation . ListRules ,
25
- ImmutableArray < string [ ] > . Empty ,
26
- ImmutableArray < string > . Empty ,
27
- ImmutableDictionary < string , bool > . Empty ,
28
- ImmutableArray < string > . Empty ,
29
- ImmutableArray < string > . Empty ,
30
- null ,
31
- allowTables : false ,
32
- verbose : false ) ;
33
-
34
- public static readonly CommandLineOptions ShowHelp = new CommandLineOptions (
35
- Operation . ShowHelp ,
36
- ImmutableArray < string [ ] > . Empty ,
37
- ImmutableArray < string > . Empty ,
38
- ImmutableDictionary < string , bool > . Empty ,
39
- ImmutableArray < string > . Empty ,
40
- ImmutableArray < string > . Empty ,
41
- null ,
42
- allowTables : false ,
43
- verbose : false ) ;
44
-
45
-
46
- public readonly Operation Operation ;
47
- public readonly ImmutableArray < string [ ] > PreprocessorConfigurations ;
48
- public readonly ImmutableArray < string > CopyrightHeader ;
49
- public readonly ImmutableDictionary < string , bool > RuleMap ;
50
- public readonly ImmutableArray < string > FormatTargets ;
51
- public readonly ImmutableArray < string > FileNames ;
52
- public readonly string Language ;
53
- public readonly bool AllowTables ;
54
- public readonly bool Verbose ;
55
-
56
- public CommandLineOptions (
57
- Operation operation ,
58
- ImmutableArray < string [ ] > preprocessorConfigurations ,
59
- ImmutableArray < string > copyrightHeader ,
60
- ImmutableDictionary < string , bool > ruleMap ,
61
- ImmutableArray < string > formatTargets ,
62
- ImmutableArray < string > fileNames ,
63
- string language ,
64
- bool allowTables ,
65
- bool verbose )
66
- {
67
- Operation = operation ;
68
- PreprocessorConfigurations = preprocessorConfigurations ;
69
- CopyrightHeader = copyrightHeader ;
70
- RuleMap = ruleMap ;
71
- FileNames = fileNames ;
72
- FormatTargets = formatTargets ;
73
- Language = language ;
74
- AllowTables = allowTables ;
75
- Verbose = verbose ;
76
- }
77
- }
78
-
79
- public sealed class CommandLineParseResult
80
- {
81
- private readonly CommandLineOptions _options ;
82
- private readonly string _error ;
83
-
84
- public bool IsSuccess
85
- {
86
- get { return _options != null ; }
87
- }
88
-
89
- public bool IsError
90
- {
91
- get { return ! IsSuccess ; }
92
- }
93
-
94
- public CommandLineOptions Options
95
- {
96
- get
97
- {
98
- Debug . Assert ( IsSuccess ) ;
99
- return _options ;
100
- }
101
- }
102
-
103
- public string Error
104
- {
105
- get
106
- {
107
- Debug . Assert ( IsError ) ;
108
- return _error ;
109
- }
110
- }
111
-
112
- private CommandLineParseResult ( CommandLineOptions options = null , string error = null )
113
- {
114
- _options = options ;
115
- _error = error ;
116
- }
117
-
118
- public static CommandLineParseResult CreateSuccess ( CommandLineOptions options )
119
- {
120
- return new CommandLineParseResult ( options : options ) ;
121
- }
122
-
123
- public static CommandLineParseResult CreateError ( string error )
124
- {
125
- return new CommandLineParseResult ( error : error ) ;
126
- }
127
- }
128
-
129
10
public static class CommandLineParser
130
11
{
131
12
private const string FileSwitch = "/file:" ;
@@ -184,9 +65,8 @@ public static CommandLineParseResult Parse(string[] args)
184
65
var allowTables = false ;
185
66
var verbose = false ;
186
67
187
- for ( int i = 0 ; i < args . Length ; i ++ )
68
+ foreach ( var arg in args )
188
69
{
189
- string arg = args [ i ] ;
190
70
if ( arg . StartsWith ( ConfigSwitch , comparison ) )
191
71
{
192
72
var all = arg . Substring ( ConfigSwitch . Length ) ;
@@ -209,10 +89,7 @@ public static CommandLineParseResult Parse(string[] args)
209
89
}
210
90
catch ( Exception ex )
211
91
{
212
- string error = string . Format ( "Could not read {0}{1}{2}" ,
213
- fileName ,
214
- Environment . NewLine ,
215
- ex . Message ) ;
92
+ string error = $ "Could not read { fileName } { Environment . NewLine } { ex . Message } ";
216
93
return CommandLineParseResult . CreateError ( error ) ;
217
94
}
218
95
}
@@ -257,7 +134,8 @@ public static CommandLineParseResult Parse(string[] args)
257
134
{
258
135
return CommandLineParseResult . CreateSuccess ( CommandLineOptions . ListRules ) ;
259
136
}
260
- else if ( comparer . Equals ( arg , "/?" ) || comparer . Equals ( arg , "/help" ) )
137
+ else if ( comparer . Equals ( arg , "/?" ) || comparer . Equals ( arg , "-?" ) || comparer . Equals ( arg , "/help" ) ||
138
+ comparer . Equals ( arg , "-help" ) || comparer . Equals ( arg , "--help" ) )
261
139
{
262
140
return CommandLineParseResult . CreateSuccess ( CommandLineOptions . ShowHelp ) ;
263
141
}
0 commit comments