Skip to content

Commit

Permalink
Merge pull request #56 from AlexGhiondea/fixTryParse
Browse files Browse the repository at this point in the history
Fix try parse
  • Loading branch information
AlexGhiondea authored Mar 9, 2020
2 parents 8ee1b60 + 49193c7 commit d0c4b99
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<PropertyGroup>
<AssemblyVersion>2.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -18,12 +18,14 @@
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Authors>Alex Ghiondea</Authors>
<Description>Parse command line arguments into user defined objects</Description>
<releaseNotes>Fix an issue where the argument name in an exception was the wrong one.</releaseNotes>
<releaseNotes>Fix an issue where TryParse returns true when parsing help which leads to a null reference later.
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>
<Copyright>Alex Ghiondea (c) 2019</Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/AlexGhiondea/CommandLine/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https:/AlexGhiondea/CommandLine</PackageProjectUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>CommandLine, Command line, Command, Line, parser, objects, custom</PackageTags>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/HelpRequestedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CommandLine
{
public class HelpRequestedException : ParserException
{
public HelpRequestedException() : base("Help was requested.", null)
{
}
}
}
6 changes: 4 additions & 2 deletions src/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ private static bool InternalTryParse<TOptions>(string[] args, ParserOptions pars
if (args[0] == HelpGenerator.RequestShortHelpParameter || args[0] == "/?")
{
HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
return true;
ex = new HelpRequestedException();
return false;
}
else if (args[0] == HelpGenerator.RequestLongHelpParameter)
{
HelpGenerator.DisplayHelp(HelpFormat.Full, arguments, ColorScheme.Get());
return true;
ex = new HelpRequestedException();
return false;
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/CommandLineTests.Functional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,35 @@ public void BasicTest15()
Assert.Equal('b', options.Character);
}

[Trait("Category", "Basic")]
[Fact]
public void TryParseShouldFailWhenFullHelpRequested()
{
var outcome = Parser.TryParse("--help", out Options2 options);

Assert.Null(options);
Assert.False(outcome);
}

[Trait("Category", "Basic")]
[Fact]
public void TryParseShouldFailWhenShortHelpRequested1()
{
var outcome = Parser.TryParse("-?", out Options2 options);

Assert.Null(options);
Assert.False(outcome);
}

[Trait("Category", "Basic")]
[Fact]
public void TryParseShouldFailWhenShortHelpRequested2()
{
var outcome = Parser.TryParse("/?", out Options2 options);

Assert.Null(options);
Assert.False(outcome);
}
public static IEnumerable<object[]> GetParserOptions()
{
yield return new object[] { (ParserOptions)null };
Expand Down
28 changes: 13 additions & 15 deletions test/CommandLineTests.Help.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using CommandLine.ColorScheme;
using System;
using System.Collections.Generic;
using System.Reflection;
using CommandLine.ColorScheme;
using Xunit;
using Xunit.Extensions;

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

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -79,8 +78,7 @@ public void HelpTestViaApi1(IColors color)
public void HelpTest4(IColors color)
{
TestWriter _printer = new TestWriter();

var options = Helpers.Parse<Options3NoRequired>("/?", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("/?", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand All @@ -107,7 +105,7 @@ public void HelpTest4(IColors color)
public void HelpTest2(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Options3NoRequired>("-?", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("-?", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -135,7 +133,7 @@ public void HelpTest2(IColors color)
public void HelpTest3(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<OptionsNegative1>("-?", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<OptionsNegative1>("-?", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand All @@ -157,7 +155,7 @@ public void HelpTest3(IColors color)
public void DetailedHelp1(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Options1>("--help", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options1>("--help", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -229,7 +227,7 @@ public void DetailedHelp1(IColors color)
public void DetailedHelp2(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Options3NoRequired>("--help", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("--help", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -345,7 +343,7 @@ public void DetailedHelpViaApi1(IColors color)
public void DetailedHelpForGroups1(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Groups1>("--help", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Groups1>("--help", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -477,7 +475,7 @@ public void HelpForCommandWithSlashQuestionMark(IColors color)
public void HelpForTypeWithEnum(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Options3NoRequired>("/?", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Options3NoRequired>("/?", _printer, color));

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

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand All @@ -533,7 +531,7 @@ public void HelpForTypeWithEnumWithNoHelpSettingSet(IColors color)
public void DetailedHelpForGroups2WithCommonArgs(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<Groups2>("--help", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<Groups2>("--help", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down Expand Up @@ -628,7 +626,7 @@ public void InvalidHelpFormat()
public void HelpForTypeWithRequiredAndOptionalEnumsAndLists(IColors color)
{
TestWriter _printer = new TestWriter();
var options = Helpers.Parse<HelpGeneratorObject>("--help", _printer, color);
var options = Assert.Throws<HelpRequestedException>(() => Helpers.Parse<HelpGeneratorObject>("--help", _printer, color));

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down
2 changes: 1 addition & 1 deletion test/CommandLineTests.ResponseFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void HelpTest1_WithResponseFile(IColors color)
string responseFile = Path.Combine(Helpers.GetTestLocation(), Path.Combine("SampleRspFiles", "response4.rsp"));

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

Validate(_printer,
new TextAndColor(_printer.ForegroundColor, "Usage: "),
Expand Down

0 comments on commit d0c4b99

Please sign in to comment.