Skip to content

Commit d0c4b99

Browse files
authored
Merge pull request #56 from AlexGhiondea/fixTryParse
Fix try parse
2 parents 8ee1b60 + 49193c7 commit d0c4b99

File tree

6 files changed

+61
-21
lines changed

6 files changed

+61
-21
lines changed

src/CommandLine.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<PropertyGroup>
10-
<AssemblyVersion>2.0.0</AssemblyVersion>
10+
<AssemblyVersion>2.1.0</AssemblyVersion>
1111
<FileVersion>$(AssemblyVersion)</FileVersion>
12-
<VersionPrefix>2.0.0</VersionPrefix>
12+
<VersionPrefix>2.1.0</VersionPrefix>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
@@ -18,12 +18,14 @@
1818
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
1919
<Authors>Alex Ghiondea</Authors>
2020
<Description>Parse command line arguments into user defined objects</Description>
21-
<releaseNotes>Fix an issue where the argument name in an exception was the wrong one.</releaseNotes>
21+
<releaseNotes>Fix an issue where TryParse returns true when parsing help which leads to a null reference later.
22+
Also ensure that Parse throws a ParseException when the help was requested to ensure we don't end up with null options after parsing.</releaseNotes>
2223
<Copyright>Alex Ghiondea (c) 2019</Copyright>
2324
<PackageLicenseUrl>https://raw.githubusercontent.com/AlexGhiondea/CommandLine/master/LICENSE</PackageLicenseUrl>
2425
<PackageProjectUrl>https://github.com/AlexGhiondea/CommandLine</PackageProjectUrl>
2526
<RepositoryType>Git</RepositoryType>
2627
<PackageTags>CommandLine, Command line, Command, Line, parser, objects, custom</PackageTags>
28+
<LangVersion>7.1</LangVersion>
2729
</PropertyGroup>
2830

2931
<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)