Skip to content

Commit

Permalink
Merge pull request #33 from AlexGhiondea/Fix32
Browse files Browse the repository at this point in the history
Add null check for argument which caused strange behavior
  • Loading branch information
AlexGhiondea authored Mar 26, 2019
2 parents fbdfaee + 952ce42 commit 53933e7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.5.1</Version>
<AssemblyVersion>1.5.1.0</AssemblyVersion>
<Version>1.5.2</Version>
<AssemblyVersion>1.5.2.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
</PropertyGroup>

Expand Down
8 changes: 5 additions & 3 deletions src/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ private static string[] ExpandResponseFiles(string[] args)
bool shouldExpand = false;
for (int i = 0; i < args.Length; i++)
{
if (args[i][0] == '@')
// it might be possible that the args[i] value is an empty string.
// we should check and make sure we have a valid value here before trying to check the '@' char
if (!string.IsNullOrEmpty(args[i]) && args[i][0] == '@')
{
shouldExpand = true;
break;
Expand All @@ -138,7 +140,7 @@ private static string[] ExpandResponseFiles(string[] args)

for (int i = 0; i < args.Length; i++)
{
if (args[i][0] == '@')
if (!string.IsNullOrEmpty(args[i]) && args[i][0] == '@')
{
string fileName = args[i].Substring(1);
// does the file exist?
Expand Down Expand Up @@ -278,7 +280,7 @@ private static void SplitCommandLineIntoSegments(string line, ref List<string> n

private static List<PropertyInfo> ParseOptionalParameters<TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo TypeArgumentInfo, TOptions options, ref int currentLogicalPosition) where TOptions : new()
{
// we are going to assume that all optionl parameters are not matched to values in 'args'
// we are going to assume that all optional parameters are not matched to values in 'args'
List<PropertyInfo> unmatched = new List<PropertyInfo>(TypeArgumentInfo.OptionalArguments.Values);
// process the optional arguments
while (offsetInArray + currentLogicalPosition < args.Length)
Expand Down
26 changes: 26 additions & 0 deletions test/CommandLineTests.Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,5 +656,31 @@ public void HelpForTypeWithRequiredAndOptionalEnumsAndLists()
new TextAndColor(ConsoleColor.Black, ")")
);
}

[Fact]
public void HelpWhenPassMoreParametersThanExpected()
{
TestWriter _printer = new TestWriter();

var options = Helpers.Parse<MorePassedInThanRequired>("this expects 2 args", _printer);

Validate(_printer,
new TextAndColor(ConsoleColor.Red, "Error"),
new TextAndColor(ConsoleColor.Black, @": Optional parameter name should start with '-'
"),
new TextAndColor(ConsoleColor.Black, "Usage: "),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.White, "testhost.exe"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "a"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Cyan, "b"),
new TextAndColor(ConsoleColor.Black, " "),
new TextAndColor(ConsoleColor.Black, "For detailed information run '"),
new TextAndColor(ConsoleColor.White, "testhost --help"),
new TextAndColor(ConsoleColor.Black, "'." )
);
}

}
}
9 changes: 9 additions & 0 deletions test/TestObjects/Negative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ internal class RequiredNegative_InvalidPositionForRequiredArg
[RequiredArgument(1, "a", "")]
public Enum1 p1 { get; set; }
}

internal class MorePassedInThanRequired
{
[RequiredArgument(0, "a", "")]
public string p1 { get; set; }

[RequiredArgument(1, "b", "")]
public string p2 { get; set; }
}
}

0 comments on commit 53933e7

Please sign in to comment.