Skip to content

Commit 3cede41

Browse files
committed
Make sure that TryParse and Parse throw when help is requested.
This ensures that when the help is requested we don't return 'true' and give back a null options object. With this approach we are going to throw a new exception HelpRequestedException for the case when we tried to parse the arguments and they turned out to be a help request.
1 parent 8ee1b60 commit 3cede41

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

src/CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageProjectUrl>https://github.com/AlexGhiondea/CommandLine</PackageProjectUrl>
2525
<RepositoryType>Git</RepositoryType>
2626
<PackageTags>CommandLine, Command line, Command, Line, parser, objects, custom</PackageTags>
27+
<LangVersion>7.1</LangVersion>
2728
</PropertyGroup>
2829

2930
<ItemGroup>

src/HelpRequestedException.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CommandLine
2+
{
3+
public class HelpRequestedException : ParserException
4+
{
5+
public HelpRequestedException() : base("Help was requested.", null)
6+
{
7+
}
8+
}
9+
}

src/Parser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ private static bool InternalTryParse<TOptions>(string[] args, ParserOptions pars
160160
if (args[0] == HelpGenerator.RequestShortHelpParameter || args[0] == "/?")
161161
{
162162
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
163-
return true;
163+
ex = new HelpRequestedException();
164+
return false;
164165
}
165166
else if (args[0] == HelpGenerator.RequestLongHelpParameter)
166167
{
167168
HelpGenerator.DisplayHelp(HelpFormat.Full, arguments, ColorScheme.Get());
168-
return true;
169+
ex = new HelpRequestedException();
170+
return false;
169171
}
170172
}
171173

test/CommandLineTests.Functional.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,35 @@ public void BasicTest15()
231231
Assert.Equal('b', options.Character);
232232
}
233233

234+
[Trait("Category", "Basic")]
235+
[Fact]
236+
public void TryParseShouldFailWhenFullHelpRequested()
237+
{
238+
var outcome = Parser.TryParse("--help", out Options2 options);
239+
240+
Assert.Null(options);
241+
Assert.False(outcome);
242+
}
243+
244+
[Trait("Category", "Basic")]
245+
[Fact]
246+
public void TryParseShouldFailWhenShortHelpRequested1()
247+
{
248+
var outcome = Parser.TryParse("-?", out Options2 options);
249+
250+
Assert.Null(options);
251+
Assert.False(outcome);
252+
}
253+
254+
[Trait("Category", "Basic")]
255+
[Fact]
256+
public void TryParseShouldFailWhenShortHelpRequested2()
257+
{
258+
var outcome = Parser.TryParse("/?", out Options2 options);
259+
260+
Assert.Null(options);
261+
Assert.False(outcome);
262+
}
234263
public static IEnumerable<object[]> GetParserOptions()
235264
{
236265
yield return new object[] { (ParserOptions)null };

test/CommandLineTests.Help.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
1+
using CommandLine.ColorScheme;
2+
using System;
23
using System.Collections.Generic;
34
using System.Reflection;
4-
using CommandLine.ColorScheme;
55
using Xunit;
6-
using Xunit.Extensions;
76

87
namespace CommandLine.Tests
98
{
@@ -25,7 +24,7 @@ private void Validate(TestWriter _printer, params TextAndColor[] values)
2524
public void HelpTest1(IColors color)
2625
{
2726
TestWriter _printer = new TestWriter();
28-
var options = Helpers.Parse<Options3NoRequired>("-?", _printer, color);
27+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("-?", _printer, color));
2928

3029
Validate(_printer,
3130
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -79,8 +78,7 @@ public void HelpTestViaApi1(IColors color)
7978
public void HelpTest4(IColors color)
8079
{
8180
TestWriter _printer = new TestWriter();
82-
83-
var options = Helpers.Parse<Options3NoRequired>("/?", _printer, color);
81+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("/?", _printer, color));
8482

8583
Validate(_printer,
8684
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -107,7 +105,7 @@ public void HelpTest4(IColors color)
107105
public void HelpTest2(IColors color)
108106
{
109107
TestWriter _printer = new TestWriter();
110-
var options = Helpers.Parse<Options3NoRequired>("-?", _printer, color);
108+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("-?", _printer, color));
111109

112110
Validate(_printer,
113111
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -135,7 +133,7 @@ public void HelpTest2(IColors color)
135133
public void HelpTest3(IColors color)
136134
{
137135
TestWriter _printer = new TestWriter();
138-
var options = Helpers.Parse<OptionsNegative1>("-?", _printer, color);
136+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<OptionsNegative1>("-?", _printer, color));
139137

140138
Validate(_printer,
141139
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -157,7 +155,7 @@ public void HelpTest3(IColors color)
157155
public void DetailedHelp1(IColors color)
158156
{
159157
TestWriter _printer = new TestWriter();
160-
var options = Helpers.Parse<Options1>("--help", _printer, color);
158+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options1>("--help", _printer, color));
161159

162160
Validate(_printer,
163161
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -229,7 +227,7 @@ public void DetailedHelp1(IColors color)
229227
public void DetailedHelp2(IColors color)
230228
{
231229
TestWriter _printer = new TestWriter();
232-
var options = Helpers.Parse<Options3NoRequired>("--help", _printer, color);
230+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("--help", _printer, color));
233231

234232
Validate(_printer,
235233
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -345,7 +343,7 @@ public void DetailedHelpViaApi1(IColors color)
345343
public void DetailedHelpForGroups1(IColors color)
346344
{
347345
TestWriter _printer = new TestWriter();
348-
var options = Helpers.Parse<Groups1>("--help", _printer, color);
346+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Groups1>("--help", _printer, color));
349347

350348
Validate(_printer,
351349
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -477,7 +475,7 @@ public void HelpForCommandWithSlashQuestionMark(IColors color)
477475
public void HelpForTypeWithEnum(IColors color)
478476
{
479477
TestWriter _printer = new TestWriter();
480-
var options = Helpers.Parse<Options3NoRequired>("/?", _printer, color);
478+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("/?", _printer, color));
481479

482480
Validate(_printer,
483481
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -506,7 +504,7 @@ public void HelpForTypeWithEnumWithNoHelpSettingSet(IColors color)
506504
// the help is not covered by the error flag.
507505
ParserOptions po = new ParserOptions() { LogParseErrorToConsole = false };
508506
TestWriter _printer = new TestWriter();
509-
var options = Helpers.Parse<Options3NoRequired>("/?", _printer, color, po);
507+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("/?", _printer, color, po));
510508

511509
Validate(_printer,
512510
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -533,7 +531,7 @@ public void HelpForTypeWithEnumWithNoHelpSettingSet(IColors color)
533531
public void DetailedHelpForGroups2WithCommonArgs(IColors color)
534532
{
535533
TestWriter _printer = new TestWriter();
536-
var options = Helpers.Parse<Groups2>("--help", _printer, color);
534+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Groups2>("--help", _printer, color));
537535

538536
Validate(_printer,
539537
new TextAndColor(_printer.ForegroundColor, "Usage: "),
@@ -628,7 +626,7 @@ public void InvalidHelpFormat()
628626
public void HelpForTypeWithRequiredAndOptionalEnumsAndLists(IColors color)
629627
{
630628
TestWriter _printer = new TestWriter();
631-
var options = Helpers.Parse<HelpGeneratorObject>("--help", _printer, color);
629+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<HelpGeneratorObject>("--help", _printer, color));
632630

633631
Validate(_printer,
634632
new TextAndColor(_printer.ForegroundColor, "Usage: "),

test/CommandLineTests.ResponseFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void HelpTest1_WithResponseFile(IColors color)
7575
string responseFile = Path.Combine(Helpers.GetTestLocation(), Path.Combine("SampleRspFiles", "response4.rsp"));
7676

7777
TestWriter _printer = new TestWriter();
78-
var options = Helpers.Parse<Options3NoRequired>($"@{responseFile}", _printer, color);
78+
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>($"@{responseFile}", _printer, color));
7979

8080
Validate(_printer,
8181
new TextAndColor(_printer.ForegroundColor, "Usage: "),

0 commit comments

Comments
 (0)