Add function for parsing command-line arguments into argument list

This commit is contained in:
2025-12-18 21:01:42 +02:00
parent 46fd5a5855
commit c8ee2fc155
3 changed files with 152 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include "CommandLine.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Core/Utilities.h"
#include "Engine/Core/Types/StringView.h"
#include <iostream>
CommandLine::OptionsData CommandLine::Options;
@@ -169,3 +170,63 @@ bool CommandLine::Parse(const Char* cmdLine)
return false;
}
bool CommandLine::ParseArguments(const StringView& cmdLine, Array<StringAnsi>& arguments)
{
int32 start = 0;
int32 quotesStart = -1;
int32 length = cmdLine.Length();
for (int32 i = 0; i < length; i++)
{
if (cmdLine[i] == ' ' && quotesStart == -1)
{
int32 count = i - start;
if (count > 0)
arguments.Add(StringAnsi(cmdLine.Substring(start, count)));
start = i + 1;
}
else if (cmdLine[i] == '\"')
{
if (quotesStart >= 0)
{
if (i + 1 < length && cmdLine[i + 1] != ' ')
{
// End quotes are in the middle of the current word,
// continue until the end of the current word.
}
else
{
int32 offset = 1;
if (quotesStart == start && cmdLine[start] == '\"')
{
// Word starts and ends with quotes, only include the quoted content.
quotesStart++;
offset--;
}
else if (quotesStart != start)
{
// Start quotes in the middle of the word, include the whole word.
quotesStart = start;
}
int32 count = i - quotesStart + offset;
if (count > 0)
arguments.Add(StringAnsi(cmdLine.Substring(quotesStart, count)));
start = i + 1;
}
quotesStart = -1;
}
else
{
quotesStart = i;
}
}
}
const int32 count = length - start;
if (count > 0)
arguments.Add(StringAnsi(cmdLine.Substring(start, count)));
if (quotesStart >= 0)
return true; // Missing last closing quote
return false;
}

View File

@@ -4,6 +4,7 @@
#include "Engine/Core/Types/String.h"
#include "Engine/Core/Types/Nullable.h"
#include "Engine/Core/Collections/Array.h"
/// <summary>
/// Command line options helper.
@@ -214,4 +215,12 @@ public:
/// <param name="cmdLine">The command line.</param>
/// <returns>True if failed, otherwise false.</returns>
static bool Parse(const Char* cmdLine);
/// <summary>
/// Parses the command line arguments string into string list of arguments.
/// </summary>
/// <param name="cmdLine">The command line.</param>
/// <param name="arguments">The parsed arguments</param>
/// <returns>True if failed, otherwise false.</returns>
static bool ParseArguments(const StringView& cmdLine, Array<StringAnsi>& arguments);
};