Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 6fd1555

Browse files
committed
Merge pull request #219 from davkean/Switches
Support for /help and better error for unrecognized switches
2 parents c86cbed + ee0effe commit 6fd1555

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/CodeFormatter/CommandLineParser.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public enum Operation
1515
{
1616
Format,
1717
ListRules,
18+
ShowHelp
1819
}
1920

2021
public sealed class CommandLineOptions
@@ -30,6 +31,18 @@ public sealed class CommandLineOptions
3031
allowTables: false,
3132
verbose: false);
3233

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+
3346
public readonly Operation Operation;
3447
public readonly ImmutableArray<string[]> PreprocessorConfigurations;
3548
public readonly ImmutableArray<string> CopyrightHeader;
@@ -143,8 +156,7 @@ containing a custom copyright header.
143156
/rule(+|-) - Enable (default) or disable the specified rule
144157
/rules - List the available rules
145158
/verbose - Verbose output
146-
147-
Use ConvertTests to convert MSTest tests to xUnit.
159+
/help - Displays this usage message (short form: /?)
148160
";
149161

150162
public static void PrintUsage()
@@ -245,6 +257,14 @@ public static CommandLineParseResult Parse(string[] args)
245257
{
246258
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules);
247259
}
260+
else if (comparer.Equals(arg, "/?") || comparer.Equals(arg, "/help"))
261+
{
262+
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ShowHelp);
263+
}
264+
else if (arg.StartsWith("/", comparison))
265+
{
266+
return CommandLineParseResult.CreateError($"Unrecognized option \"{arg}\"");
267+
}
248268
else
249269
{
250270
formatTargets.Add(arg);

src/CodeFormatter/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ private static int Main(string[] args)
3131
int exitCode;
3232
switch (options.Operation)
3333
{
34+
case Operation.ShowHelp:
35+
CommandLineParser.PrintUsage();
36+
exitCode = 0;
37+
break;
38+
3439
case Operation.ListRules:
3540
RunListRules();
3641
exitCode = 0;

src/Microsoft.DotNet.CodeFormatting.Tests/CommandLineParserTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ private CommandLineOptions Parse(params string[] args)
1717
return options;
1818
}
1919

20+
private CommandLineOptions FailToParse(params string[] args)
21+
{
22+
CommandLineOptions options;
23+
Assert.False(CommandLineParser.TryParse(args, out options));
24+
Assert.Null(options);
25+
26+
return options;
27+
}
28+
2029
[Fact]
2130
public void Rules()
2231
{
@@ -99,6 +108,28 @@ public void NoCopyright()
99108
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
100109
}
101110

111+
112+
[Fact]
113+
public void Help()
114+
{
115+
var options = Parse("/help");
116+
Assert.Equal(options.Operation, Operation.ShowHelp);
117+
}
118+
119+
[Fact]
120+
public void HelpShortForm()
121+
{
122+
var options = Parse("/?");
123+
Assert.Equal(options.Operation, Operation.ShowHelp);
124+
}
125+
126+
[Fact]
127+
public void HelpWithOtherwiseValidArguments()
128+
{
129+
var options = Parse("test.csproj", "/nocopyright", "/help");
130+
Assert.Equal(options.Operation, Operation.ShowHelp);
131+
}
132+
102133
[Fact]
103134
public void CopyrightEnable1()
104135
{
@@ -114,5 +145,23 @@ public void CopyrightEnable2()
114145
Assert.True(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
115146
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
116147
}
148+
149+
[Fact]
150+
public void SingleUnrecognizedOption()
151+
{
152+
FailToParse("/unrecognized");
153+
}
154+
155+
[Fact]
156+
public void UnrecognizedOptionWithFormatTarget()
157+
{
158+
FailToParse("test.csproj", "/unrecognized");
159+
}
160+
161+
[Fact]
162+
public void UnrecognizedOptionWithOtherwiseValidArguments()
163+
{
164+
FailToParse("test.csproj", "/nocopyright", "/unrecognized");
165+
}
117166
}
118167
}

0 commit comments

Comments
 (0)