AronaCore/core/application/command_line.cpp
2024-01-31 15:02:34 +08:00

79 lines
1.5 KiB
C++

#include "command_line.h"
void command_line::init(int argc, char** argv)
{
// parser argv
args_.clear();
for (int i = 0; i < argc; ++i)
{
std::string arg = argv[i];
if (arg[0] == '-')
{
std::string key = arg.substr(1);
std::string value;
if (i + 1 < argc)
{
std::string next_arg = argv[i + 1];
if (next_arg[0] != '-')
{
value = next_arg;
++i;
}
}
args_[key] = value;
}
}
}
void command_line::get_arg(const std::string& key, std::string& out) const
{
const auto it = args_.find(key);
if (it != args_.end())
{
out = it->second;
}
else
{
out = "";
}
}
void command_line::get_arg(const std::string& key, int& out) const
{
const auto it = args_.find(key);
if (it != args_.end())
{
out = std::stoi(it->second);
}
else
{
out = 0;
}
}
void command_line::get_arg(const std::string& key, float& out) const
{
const auto it = args_.find(key);
if (it != args_.end())
{
out = std::stof(it->second);
}
else
{
out = 0.0f;
}
}
void command_line::get_arg(const std::string& key, bool& out) const
{
const auto it = args_.find(key);
if (it != args_.end())
{
out = it->second != "false";
}
else
{
out = false;
}
}