Skip to content

Commit

Permalink
Fixed missing option value error
Browse files Browse the repository at this point in the history
  • Loading branch information
sakya committed Aug 6, 2022
1 parent 7c4e0f8 commit 0a89482
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CmdLineArgsParser/Parser.Parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public partial class Parser
bool first = true;
foreach (var arg in args) {
if (arg.StartsWith("--")) {
if (lastOption != null && !lastOption.Set)
errors.Add(new ParserError(lastOption.Option.Name, $"Missing value for option '{ lastOption.Option.Name }'"));
lastOption = ParseNameOption(res, properties, arg, errors);
} else if (arg.StartsWith(("-"))) {
if (lastOption != null && !lastOption.Set)
errors.Add(new ParserError(lastOption.Option.Name, $"Missing value for option '{ lastOption.Option.Name }'"));
lastOption = ParseShortNameOption(res, properties, arg, errors);
} else {
if (lastOption != null) {
Expand All @@ -79,6 +83,9 @@ public partial class Parser
first = false;
}

if (lastOption != null && !lastOption.Set)
errors.Add(new ParserError(lastOption.Option.Name, $"Missing value for option '{ lastOption.Option.Name }'"));

CheckRequiredOptions(properties, errors);
CheckMutuallyExclusiveOptions(properties, errors);

Expand Down
2 changes: 1 addition & 1 deletion SolutionInfo.proj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<RespositoryType>git</RespositoryType>
<PackageTags>command line commandline argument option shell parser</PackageTags>
<Description>CmdLineArgsParser helps you parse command line arguments.</Description>
<Version>0.5.0.0</Version>
<Version>0.5.1.0</Version>
</PropertyGroup>
</Project>
31 changes: 31 additions & 0 deletions Tests/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ public void InvalidValue()
Assert.Pass();
}


[Test]
public void MissingOptionValue()
{
Parser.Parse<Options>(
new []
{
"--stringwithvalues",
},
out Errors);
CheckErrors(new [] { "Missing value for option 'stringwithvalues'" });

Parser.Parse<Options>(
new []
{
"--stringwithvalues",
"--int", "5"
},
out Errors);
CheckErrors(new [] { "Missing value for option 'stringwithvalues'" });

Parser.Parse<Options>(
new []
{
"--int", "5",
"--stringwithvalues",
},
out Errors);
CheckErrors(new [] { "Missing value for option 'stringwithvalues'" });
}

[Test]
public void InvalidOptionValue()
{
Expand Down

0 comments on commit 0a89482

Please sign in to comment.