181 lines
5.0 KiB
C++
181 lines
5.0 KiB
C++
/***
|
|
Copyright (C) 2022-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuCmdLine.cpp
|
|
Date: 2022-1-31
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuCmdLine.hpp"
|
|
#include "AuCmdLinePlatform.hpp"
|
|
|
|
namespace Aurora::CmdLine
|
|
{
|
|
static const AuString kEmptyString;
|
|
static AuList<AuString> gCmdFlags;
|
|
static AuList<AuString> gCmdValues;
|
|
static AuHashMap<AuString, AuString> gCmdValueMap;
|
|
static AuHashMap<AuString, AuList<AuString>> gCmdValueArrayMap;
|
|
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 gCmdValueArrayMap.find(key) != gCmdValueArrayMap.end() ||
|
|
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> &GetValues(const AuString &key)
|
|
{
|
|
static AuList<AuString> kMissing;
|
|
auto itr = gCmdValueArrayMap.find(key);
|
|
if (itr != gCmdValueArrayMap.end())
|
|
{
|
|
return itr->second;
|
|
}
|
|
return kMissing;
|
|
}
|
|
|
|
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)
|
|
for (int i = 0; i < gCmdLineString.size(); i++)
|
|
{
|
|
const auto &arg = gCmdLineString[i];
|
|
|
|
#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 doesStartWithSwitch = [](auto &in)
|
|
{
|
|
return (in[0] == '/') ||
|
|
(in.find("--") == 0);
|
|
};
|
|
|
|
auto containsAssign = [](auto &in)
|
|
{
|
|
return (in.find("=") != AuString::npos);
|
|
};
|
|
|
|
auto valueAssignment = extendedLine.find('=');
|
|
if (valueAssignment == extendedLine.npos)
|
|
{
|
|
if (doesStartWithSwitch(extendedLine))
|
|
{
|
|
if (i != gCmdLineString.size() - 1)
|
|
{
|
|
const auto &next = gCmdLineString[i + 1];
|
|
|
|
if (!doesStartWithSwitch(next) && !containsAssign(next))
|
|
{
|
|
extendedLine += '=';
|
|
continue;
|
|
}
|
|
}
|
|
|
|
extendedLine = extendedLine.substr(1 + (extendedLine[0] != '/'));
|
|
}
|
|
|
|
gCmdFlags.push_back(extendedLine);
|
|
gCmdFlagLookup[extendedLine] = true;
|
|
}
|
|
else
|
|
{
|
|
key = extendedLine.substr(0, valueAssignment);
|
|
extendedLine = extendedLine.substr(valueAssignment + 1);
|
|
|
|
if (doesStartWithSwitch(key))
|
|
{
|
|
key = key.substr(1 + (key[0] != '/'));
|
|
}
|
|
|
|
gCmdValues.push_back(key);
|
|
{
|
|
auto itr = gCmdValueMap.find(key);
|
|
if (itr != gCmdValueMap.end())
|
|
{
|
|
auto val = (*itr).second;
|
|
gCmdValueMap.erase(itr);
|
|
auto &arry = gCmdValueArrayMap[key];
|
|
arry.push_back(val);
|
|
arry.push_back(extendedLine);
|
|
}
|
|
else
|
|
{
|
|
gCmdValueMap[key] = extendedLine;
|
|
}
|
|
}
|
|
}
|
|
|
|
extendedLine.clear();
|
|
}
|
|
}
|
|
|
|
void Init()
|
|
{
|
|
gCmdLineString = GetCmdString();
|
|
if (gCmdLineString.size())
|
|
{
|
|
gCmdLineString.erase(gCmdLineString.begin());
|
|
}
|
|
|
|
ProcessArgs();
|
|
}
|
|
|
|
void Deinit()
|
|
{
|
|
gCmdFlags.clear();
|
|
gCmdValues.clear();
|
|
gCmdValueMap.clear();
|
|
gCmdFlagLookup.clear();
|
|
gCmdLineString.clear();
|
|
}
|
|
} |