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

Commit bbe1a66

Browse files
committed
Do not treat unrecognized switches as formatting targets
1 parent c86cbed commit bbe1a66

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/CodeFormatter/CommandLineParser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ public static CommandLineParseResult Parse(string[] args)
245245
{
246246
return CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules);
247247
}
248+
else if (arg.StartsWith("/", comparison))
249+
{
250+
return CommandLineParseResult.CreateError($"Unrecognized option \"{arg}\"");
251+
}
248252
else
249253
{
250254
formatTargets.Add(arg);

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

Lines changed: 27 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
{
@@ -114,5 +123,23 @@ public void CopyrightEnable2()
114123
Assert.True(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
115124
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
116125
}
126+
127+
[Fact]
128+
public void SingleUnrecognizedOption()
129+
{
130+
FailToParse("/unrecognized");
131+
}
132+
133+
[Fact]
134+
public void UnrecognizedOptionWithFormatTarget()
135+
{
136+
FailToParse("test.csproj", "/unrecognized");
137+
}
138+
139+
[Fact]
140+
public void UnrecognizedOptionWithOtherwiseValidArguments()
141+
{
142+
FailToParse("test.csproj", "/nocopyright", "/unrecognized");
143+
}
117144
}
118145
}

0 commit comments

Comments
 (0)