AuroraRuntime/Source/CmdLine/CmdLine.cpp

109 lines
2.7 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: CmdLine.cpp
Date: 2022-1-31
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "CmdLine.hpp"
#include "CmdLinePlatform.hpp"
namespace Aurora::CmdLine
{
static const AuString kEmptyString;
static AuList<AuString> gCmdFlags;
static AuList<AuString> gCmdValues;
static AuHashMap<AuString, AuString> gCmdValueMap;
static AuHashMap<AuString, bool> gCmdFlagLookup;
static AuList<AuString> gCmdLineString;
AUKN_SYM const AuList<AuString> &GetCommandLineArguments()
{
return gCmdLineString;
}
AUKN_SYM bool HasFlag(const AuString &key)
{
return gCmdFlagLookup.find(key) != gCmdFlagLookup.end();
}
AUKN_SYM bool HasValue(const AuString &key)
{
return gCmdValueMap.find(key) != gCmdValueMap.end();
}
AUKN_SYM const AuString &GetValue(const AuString &key, const AuString &defaultValue)
{
auto itr = gCmdValueMap.find(key);
if (itr == gCmdValueMap.end()) return defaultValue;
return itr->second;
}
AUKN_SYM const AuString &GetValue(const AuString &key)
{
return GetValue(key, kEmptyString);
}
AUKN_SYM const AuList<AuString> &GetFlags()
{
return gCmdFlags;
}
AUKN_SYM const AuList<AuString> &GetValues()
{
return gCmdValues;
}
static void ProcessArgs()
{
AuString extendedLine;
AuString key;
for (const auto &arg : gCmdLineString)
{
#if defined(AURORA_PLATFORM_WIN32)
if (arg[arg.size() - 1] == '\\' && (arg.size() > 1))
{
extendedLine += arg.substr(0, arg.size() - 1);
if (arg[arg.size() - 2] != '\\')
{
extendedLine += ' ';
continue;
}
}
else
#endif
{
extendedLine += arg;
}
auto valueAssignment = extendedLine.find('=');
if (valueAssignment == extendedLine.npos)
{
gCmdFlags.push_back(extendedLine);
gCmdFlagLookup[extendedLine] = true;
}
else
{
key = extendedLine.substr(0, valueAssignment);
extendedLine = extendedLine.substr(valueAssignment + 1);
gCmdValues.push_back(key);
gCmdValueMap[key] = extendedLine;
}
extendedLine.clear();
}
}
void Init()
{
gCmdLineString = GetCmdString();
if (gCmdLineString.size())
{
gCmdLineString.erase(gCmdLineString.begin());
}
ProcessArgs();
}
}