Core: Add CommandLineParameters

This commit is contained in:
SirLynix
2023-07-30 11:46:55 +02:00
parent 651261d28a
commit 218b75558a
3 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_COMMANDLINEPARAMETERS_HPP
#define NAZARA_CORE_COMMANDLINEPARAMETERS_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <NazaraUtils/Algorithm.hpp>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
namespace Nz
{
class CommandLineParameters
{
public:
inline bool GetParameter(const std::string& name, std::string_view* value) const;
inline bool HasFlag(const std::string& flag) const;
inline bool operator==(const CommandLineParameters& params) const;
inline bool operator!=(const CommandLineParameters& params) const;
static inline CommandLineParameters Parse(int argc, char** argv);
static inline CommandLineParameters Parse(int argc, const Pointer<const char>* argv);
private:
std::unordered_map<std::string, std::string> m_parameters;
std::unordered_set<std::string> m_flags;
};
}
#include <Nazara/Core/CommandLineParameters.inl>
#endif // NAZARA_CORE_COMMANDLINEPARAMETERS_HPP

View File

@@ -0,0 +1,78 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
inline bool CommandLineParameters::GetParameter(const std::string& name, std::string_view* value) const
{
auto it = m_parameters.find(name);
if (it == m_parameters.end())
return false;
if (value)
*value = it->second;
return true;
}
inline bool CommandLineParameters::HasFlag(const std::string& flag) const
{
return m_flags.find(flag) != m_flags.end();
}
inline bool CommandLineParameters::operator==(const CommandLineParameters& params) const
{
return m_flags == params.m_flags && m_parameters == params.m_parameters;
}
inline bool CommandLineParameters::operator!=(const CommandLineParameters& params) const
{
return !operator==(params);
}
inline CommandLineParameters CommandLineParameters::Parse(int argc, char** argv)
{
return Parse(argc, const_cast<const Pointer<const char>*>(argv));
}
inline CommandLineParameters CommandLineParameters::Parse(int argc, const Pointer<const char>* argv)
{
CommandLineParameters cmdParams;
// Parse commandline parameters
for (int i = 1; i < argc; ++i)
{
const char* value = argv[i];
if (value[0] != '-' || value[1] != '-')
continue;
std::string_view arg(value + 2);
if (arg.empty())
continue;
std::size_t sepIdx = arg.find_first_of(":= ");
std::string_view name = arg.substr(0, sepIdx);
if (sepIdx != arg.npos)
// --param=value | --param:value | "--param value"
cmdParams.m_parameters.emplace(name, arg.substr(sepIdx + 1));
else if (i < argc - 1)
{
// Check the following parameter to handle --param value
const char* nextValue = argv[i + 1];
if (value[0] != '-')
cmdParams.m_parameters.emplace(name, nextValue);
else
cmdParams.m_flags.emplace(name);
}
else
cmdParams.m_flags.emplace(name);
}
return cmdParams;
}
}
#include <Nazara/Core/DebugOff.hpp>