Improve parsing command line in build tools when using lots of quotes

This commit is contained in:
Wojtek Figat
2023-09-20 10:06:16 +02:00
parent 1740cbf2eb
commit 34a36d822a
2 changed files with 32 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ namespace Flax.Build.Tests
[Test, Sequential] [Test, Sequential]
public void TestParseOptionOnly([Values( public void TestParseOptionOnly([Values(
"-something", "-something",
"something",
" \t \t-\t \tsomething\t ", " \t \t-\t \tsomething\t ",
"-something=")] "-something=")]
string commandLine) string commandLine)
@@ -25,6 +26,11 @@ namespace Flax.Build.Tests
[Test, Sequential] [Test, Sequential]
public void TestParseOneValue([Values( public void TestParseOneValue([Values(
"-something=value", "-something=value",
"something=value",
"-something=\"value\"",
"-something=\\\"value\\\"",
"\"-something=\"value\"\"",
"\"-something=\\\"value\\\"\"",
" \t \t-\t \tsomething\t =value ", " \t \t-\t \tsomething\t =value ",
"-something=value ")] "-something=value ")]
string commandLine) string commandLine)

View File

@@ -249,6 +249,8 @@ namespace Flax.Build
var wholeQuote = commandLine[i] == '\"'; var wholeQuote = commandLine[i] == '\"';
if (wholeQuote) if (wholeQuote)
i++; i++;
if (i == length)
break;
if (commandLine[i] == '-') if (commandLine[i] == '-')
i++; i++;
else if (commandLine[i] == '/') else if (commandLine[i] == '/')
@@ -279,7 +281,7 @@ namespace Flax.Build
}); });
if (wholeQuote) if (wholeQuote)
i++; i++;
if (i != length && commandLine[i] != '\"') if (i < length && commandLine[i] != '\"')
i++; i++;
continue; continue;
} }
@@ -287,23 +289,36 @@ namespace Flax.Build
// Read value // Read value
i++; i++;
int valueStart, valueEnd; int valueStart, valueEnd;
if (commandLine[i] == '\"') if (commandLine.Length > i + 1 && commandLine[i] == '\\' && commandLine[i + 1] == '\"')
{ {
valueStart = i + 1; valueStart = i + 2;
i++; i++;
while (i < length && commandLine[i] != '\"') while (i + 1 < length && commandLine[i] != '\\' && commandLine[i + 1] != '\"')
i++; i++;
valueEnd = i; valueEnd = i;
i++; i += 2;
if (wholeQuote)
{
while (i < length && commandLine[i] != '\"')
i++;
i++;
}
} }
else if (commandLine[i] == '\'') else if (commandLine[i] == '\"' || commandLine[i] == '\'')
{ {
var quoteChar = commandLine[i];
valueStart = i + 1; valueStart = i + 1;
i++; i++;
while (i < length && commandLine[i] != '\'') while (i < length && commandLine[i] != quoteChar)
i++; i++;
valueEnd = i; valueEnd = i;
i++; i++;
if (wholeQuote)
{
while (i < length && commandLine[i] != '\"')
i++;
i++;
}
} }
else if (wholeQuote) else if (wholeQuote)
{ {
@@ -321,10 +336,13 @@ namespace Flax.Build
valueEnd = i; valueEnd = i;
} }
string value = commandLine.Substring(valueStart, valueEnd - valueStart); string value = commandLine.Substring(valueStart, valueEnd - valueStart);
value = value.Trim();
if (value.StartsWith("\\\"") && value.EndsWith("\\\""))
value = value.Substring(2, value.Length - 4);
options.Add(new Option options.Add(new Option
{ {
Name = name, Name = name,
Value = value.Trim() Value = value
}); });
} }