Browse Source

Change remainder param to allow options first

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
dd5d1c3496
  1. 34
      sources/Commands/CommandParser.uc
  2. 10
      sources/Commands/Tests/TEST_Command.uc

34
sources/Commands/CommandParser.uc

@ -400,23 +400,29 @@ private final function bool ParseSingleValue(
AssociativeArray parsedParameters, AssociativeArray parsedParameters,
Command.Parameter expectedParameter) Command.Parameter expectedParameter)
{ {
// First we try `CPT_Remainder` parameter, since it is a special case that
// consumes all further input
if (expectedParameter.type == CPT_Remainder) {
return ParseRemainderValue(parsedParameters, expectedParameter);
}
// Before parsing any other value we need to check if user has // Before parsing any other value we need to check if user has
// specified any options instead. // specified any options instead.
// However this might lead to errors if we are already parsing // However this might lead to errors if we are already parsing
// necessary parameters of another option: // necessary parameters of another option:
// we must handle such situation and report an error. // we must handle such situation and report an error.
if ( currentTargetIsOption && currentTarget != CPT_ExtraParameter if (currentTargetIsOption)
&& TryParsingOptions())
{ {
DeclareError(CET_NoRequiredParamForOption, targetOption.longName); // There is no problem is option's parameter is remainder
return false; if (expectedParameter.type == CPT_Remainder) {
return ParseRemainderValue(parsedParameters, expectedParameter);
}
if (currentTarget != CPT_ExtraParameter && TryParsingOptions())
{
DeclareError(CET_NoRequiredParamForOption, targetOption.longName);
return false;
}
} }
while (TryParsingOptions()); while (TryParsingOptions());
// First we try `CPT_Remainder` parameter, since it is a special case that
// consumes all further input
if (expectedParameter.type == CPT_Remainder) {
return ParseRemainderValue(parsedParameters, expectedParameter);
}
// Propagate errors after parsing options // Propagate errors after parsing options
if (nextResult.parsingError != CET_None) { if (nextResult.parsingError != CET_None) {
return false; return false;
@ -567,15 +573,13 @@ private final function bool ParseRemainderValue(
AssociativeArray parsedParameters, AssociativeArray parsedParameters,
Command.Parameter expectedParameter) Command.Parameter expectedParameter)
{ {
local string textValue; local MutableText value;
// TODO: use parsing methods into `Text`
// (needs some work for reading formatting `string`s from `Text` objects) commandParser.Skip().MUntil(value);
commandParser.Skip().MUntilS(textValue);
if (!commandParser.Ok()) { if (!commandParser.Ok()) {
return false; return false;
} }
RecordParameter(parsedParameters, expectedParameter, RecordParameter(parsedParameters, expectedParameter, value.IntoText());
_.text.FromString(textValue));
return true; return true;
} }

10
sources/Commands/Tests/TEST_Command.uc

@ -395,24 +395,24 @@ protected static function SubTest_MockBQ2()
protected static function SubTest_MockBQ3Remainder() protected static function SubTest_MockBQ3Remainder()
{ {
local Command.CallData result; local Command.CallData result;
local DynamicArray subArray; local DynamicArray subArray;
local AssociativeArray options, subObject; local AssociativeArray options, subObject;
Issue("Cannot parse command queries with `CPT_Remainder` type parameters."); Issue("Cannot parse command queries with `CPT_Remainder` type parameters.");
result = class'MockCommandB'.static.GetInstance() result = class'MockCommandB'.static.GetInstance()
.ParseInputWith(PRS(default.queryBSuccess3), none); .ParseInputWith(PRS(default.queryBSuccess3), none);
TEST_ExpectTrue(result.Parameters.GetLength() == 1); TEST_ExpectTrue(result.parameters.GetLength() == 1);
subArray = DynamicArray(result.Parameters.GetItem(P("list"))); subArray = DynamicArray(result.parameters.GetItem(P("list")));
TEST_ExpectTrue(FloatBox(subArray.GetItem(0)).Get() == 3); TEST_ExpectTrue(FloatBox(subArray.GetItem(0)).Get() == 3);
TEST_ExpectTrue(FloatBox(subArray.GetItem(1)).Get() == -76); TEST_ExpectTrue(FloatBox(subArray.GetItem(1)).Get() == -76);
options = result.Options; options = result.options;
TEST_ExpectTrue(options.GetLength() == 1); TEST_ExpectTrue(options.GetLength() == 1);
TEST_ExpectTrue(options.HasKey(P("remainder"))); TEST_ExpectTrue(options.HasKey(P("remainder")));
subObject = AssociativeArray(options.GetItem(P("remainder"))); subObject = AssociativeArray(options.GetItem(P("remainder")));
TEST_ExpectTrue( Text(subObject.GetItem(P("everything"))).ToString() TEST_ExpectTrue( Text(subObject.GetItem(P("everything"))).ToString()
== "--type \"value\" -va 8 -sV --forced -T \"\" 32"); == "--type \"value\" -va 8 -sV --forced -T \"\" 32");
} }
// [1, 2, 3, 6]
defaultproperties defaultproperties
{ {
caseName = "Command" caseName = "Command"

Loading…
Cancel
Save