Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes boolean options falsible #133

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 63 additions & 52 deletions src/libcmdline/Parsing/OptionGroupParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,72 +47,83 @@ public override PresentParserState Parse(IArgumentEnumerator argumentEnumerator,

option.IsDefined = true;

ArgumentParser.EnsureOptionArrayAttributeIsNotBoundToScalar(option);

if (!option.IsBoolean)
ArgumentParser.EnsureOptionArrayAttributeIsNotBoundToScalar(option);

//previous place

if (!option.IsBoolean && argumentEnumerator.IsLast && optionGroup.IsLast)
{
if (argumentEnumerator.IsLast && optionGroup.IsLast)
{
return PresentParserState.Failure;
}

bool valueSetting;
if (!optionGroup.IsLast)
{
if (!option.IsArray)
{
valueSetting = option.SetValue(optionGroup.GetRemainingFromNext(), options);
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting);
}

ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

var items = ArgumentParser.GetNextInputValues(argumentEnumerator);
items.Insert(0, optionGroup.GetRemainingFromNext());

valueSetting = option.SetValue(items, options);
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting, true);
}

if (!argumentEnumerator.IsLast && !ArgumentParser.IsInputValue(argumentEnumerator.Next))
{
return PresentParserState.Failure;
}
return PresentParserState.Failure;
}

bool valueSetting;
if (!optionGroup.IsLast)
{
if (!option.IsArray)
{
valueSetting = option.SetValue(argumentEnumerator.Next, options);
if (!valueSetting)
valueSetting = option.SetValue(optionGroup.GetRemainingFromNext(), options);
if (!valueSetting && !option.IsBoolean)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting, true);
if (valueSetting || !option.IsBoolean)
return ArgumentParser.BooleanToParserState(valueSetting);
}

if (!option.IsBoolean)
{
ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

var items = ArgumentParser.GetNextInputValues(argumentEnumerator);
items.Insert(0, optionGroup.GetRemainingFromNext());

valueSetting = option.SetValue(items, options);
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting, true);
}
}

ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

var moreItems = ArgumentParser.GetNextInputValues(argumentEnumerator);
if (!option.IsBoolean && !argumentEnumerator.IsLast && !ArgumentParser.IsInputValue(argumentEnumerator.Next))
{
return PresentParserState.Failure;
}

valueSetting = option.SetValue(moreItems, options);
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
if (!option.IsArray)
{
valueSetting = option.SetValue(argumentEnumerator.Next, options);
if (!option.IsBoolean || valueSetting)
{
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting, true);
}

return ArgumentParser.BooleanToParserState(valueSetting);
}

if (!option.IsBoolean)
{
ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

var moreItems = ArgumentParser.GetNextInputValues(argumentEnumerator);

valueSetting = option.SetValue(moreItems, options);
if (!valueSetting)
{
DefineOptionThatViolatesFormat(option);
}

return ArgumentParser.BooleanToParserState(valueSetting);
}

//old place close

if (!optionGroup.IsLast && map[optionGroup.Next] == null)
{
return PresentParserState.Failure;
Expand Down
18 changes: 17 additions & 1 deletion src/tests/Unit/Parser/ParserFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,23 @@ public void Parser_should_report_missing_value()

options.LastParserState.Errors.Count.Should().BeGreaterThan(0);
}
#endregion
#endregion

[Fact]
public void Parse_falsible_boolean()
{
var options = new BooleanSetOptions();
var parser = new CommandLine.Parser();
var result = parser.ParseArguments(new string[] { "-a true", "-c false", "-d65" }, options);

result.Should().BeTrue();
options.BooleanOne.Should().BeTrue();
options.BooleanTwo.Should().BeFalse();
options.BooleanThree.Should().BeFalse();
options.NonBooleanValue.Should().Be(65D);
Console.WriteLine(options);
}

}
}