Skip to content

Commit f52ae40

Browse files
committed
Split classes and Fix dotnet#264
1 parent 38ef57a commit f52ae40

File tree

4 files changed

+129
-126
lines changed

4 files changed

+129
-126
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Immutable;
2+
3+
namespace CodeFormatter
4+
{
5+
public sealed class CommandLineOptions
6+
{
7+
public static readonly CommandLineOptions ListRules = new CommandLineOptions(
8+
Operation.ListRules,
9+
ImmutableArray<string[]>.Empty,
10+
ImmutableArray<string>.Empty,
11+
ImmutableDictionary<string, bool>.Empty,
12+
ImmutableArray<string>.Empty,
13+
ImmutableArray<string>.Empty,
14+
null,
15+
allowTables: false,
16+
verbose: false);
17+
18+
public static readonly CommandLineOptions ShowHelp = new CommandLineOptions(
19+
Operation.ShowHelp,
20+
ImmutableArray<string[]>.Empty,
21+
ImmutableArray<string>.Empty,
22+
ImmutableDictionary<string, bool>.Empty,
23+
ImmutableArray<string>.Empty,
24+
ImmutableArray<string>.Empty,
25+
null,
26+
allowTables: false,
27+
verbose: false);
28+
29+
30+
public readonly Operation Operation;
31+
public readonly ImmutableArray<string[]> PreprocessorConfigurations;
32+
public readonly ImmutableArray<string> CopyrightHeader;
33+
public readonly ImmutableDictionary<string, bool> RuleMap;
34+
public readonly ImmutableArray<string> FormatTargets;
35+
public readonly ImmutableArray<string> FileNames;
36+
public readonly string Language;
37+
public readonly bool AllowTables;
38+
public readonly bool Verbose;
39+
40+
public CommandLineOptions(
41+
Operation operation,
42+
ImmutableArray<string[]> preprocessorConfigurations,
43+
ImmutableArray<string> copyrightHeader,
44+
ImmutableDictionary<string, bool> ruleMap,
45+
ImmutableArray<string> formatTargets,
46+
ImmutableArray<string> fileNames,
47+
string language,
48+
bool allowTables,
49+
bool verbose)
50+
{
51+
Operation = operation;
52+
PreprocessorConfigurations = preprocessorConfigurations;
53+
CopyrightHeader = copyrightHeader;
54+
RuleMap = ruleMap;
55+
FileNames = fileNames;
56+
FormatTargets = formatTargets;
57+
Language = language;
58+
AllowTables = allowTables;
59+
Verbose = verbose;
60+
}
61+
}
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Diagnostics;
2+
3+
namespace CodeFormatter
4+
{
5+
public sealed class CommandLineParseResult
6+
{
7+
private readonly CommandLineOptions _options;
8+
private readonly string _error;
9+
10+
public bool IsSuccess
11+
{
12+
get { return _options != null; }
13+
}
14+
15+
public bool IsError
16+
{
17+
get { return !IsSuccess; }
18+
}
19+
20+
public CommandLineOptions Options
21+
{
22+
get
23+
{
24+
Debug.Assert(IsSuccess);
25+
return _options;
26+
}
27+
}
28+
29+
public string Error
30+
{
31+
get
32+
{
33+
Debug.Assert(IsError);
34+
return _error;
35+
}
36+
}
37+
38+
private CommandLineParseResult(CommandLineOptions options = null, string error = null)
39+
{
40+
_options = options;
41+
_error = error;
42+
}
43+
44+
public static CommandLineParseResult CreateSuccess(CommandLineOptions options)
45+
{
46+
return new CommandLineParseResult(options: options);
47+
}
48+
49+
public static CommandLineParseResult CreateError(string error)
50+
{
51+
return new CommandLineParseResult(error: error);
52+
}
53+
}
54+
}

src/CodeFormatter/CommandLineParser.cs

Lines changed: 4 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,10 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
6-
using System.Diagnostics;
76
using System.IO;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
117

128
namespace CodeFormatter
139
{
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-
12910
public static class CommandLineParser
13011
{
13112
private const string FileSwitch = "/file:";
@@ -184,9 +65,8 @@ public static CommandLineParseResult Parse(string[] args)
18465
var allowTables = false;
18566
var verbose = false;
18667

187-
for (int i = 0; i < args.Length; i++)
68+
foreach (var arg in args)
18869
{
189-
string arg = args[i];
19070
if (arg.StartsWith(ConfigSwitch, comparison))
19171
{
19272
var all = arg.Substring(ConfigSwitch.Length);
@@ -209,10 +89,7 @@ public static CommandLineParseResult Parse(string[] args)
20989
}
21090
catch (Exception ex)
21191
{
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}";
21693
return CommandLineParseResult.CreateError(error);
21794
}
21895
}
@@ -257,7 +134,8 @@ public static CommandLineParseResult Parse(string[] args)
257134
{
258135
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules);
259136
}
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"))
261139
{
262140
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ShowHelp);
263141
}

src/CodeFormatter/Operation.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CodeFormatter
2+
{
3+
public enum Operation
4+
{
5+
Format = 0,
6+
ListRules = 1,
7+
ShowHelp = 2
8+
}
9+
}

0 commit comments

Comments
 (0)