Core: Add CommandLineParameters
This commit is contained in:
parent
651261d28a
commit
218b75558a
|
|
@ -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
|
||||
|
|
@ -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>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#include <Nazara/Core/CommandLineParameters.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <array>
|
||||
|
||||
SCENARIO("CommandLineParameters", "[CORE][CommandLineParameters]")
|
||||
{
|
||||
WHEN("Parsing no parameters")
|
||||
{
|
||||
Nz::CommandLineParameters emptyParams;
|
||||
|
||||
const char* appName = "UnitTests";
|
||||
Nz::CommandLineParameters commandLineParameters1 = Nz::CommandLineParameters::Parse(0, static_cast<char**>(nullptr));
|
||||
CHECK(commandLineParameters1 == emptyParams);
|
||||
CHECK_FALSE(commandLineParameters1 != emptyParams);
|
||||
Nz::CommandLineParameters commandLineParameters2 = Nz::CommandLineParameters::Parse(1, &appName);
|
||||
CHECK(commandLineParameters2 == emptyParams);
|
||||
CHECK_FALSE(commandLineParameters2 != emptyParams);
|
||||
}
|
||||
|
||||
WHEN("Parsing simple parameters")
|
||||
{
|
||||
std::array<const char*, 5> params = {"exec", "--flag", "--param1=value", "--param2 value2", "ignored"};
|
||||
Nz::CommandLineParameters commandLineParameters = Nz::CommandLineParameters::Parse(Nz::SafeCast<int>(params.size()), params.data());
|
||||
|
||||
std::string_view value;
|
||||
|
||||
CHECK(commandLineParameters.HasFlag("flag"));
|
||||
CHECK_FALSE(commandLineParameters.HasFlag("ignored"));
|
||||
CHECK_FALSE(commandLineParameters.GetParameter("param0", nullptr));
|
||||
CHECK(commandLineParameters.GetParameter("param1", nullptr));
|
||||
CHECK(commandLineParameters.GetParameter("param1", &value));
|
||||
CHECK(value == "value");
|
||||
CHECK(commandLineParameters.GetParameter("param2", &value));
|
||||
CHECK(value == "value2");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue