2025-02-12 02:54:22 -05:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_PARSE_ARGPARSE_V2_H
|
|
|
|
#define BLT_PARSE_ARGPARSE_V2_H
|
|
|
|
|
|
|
|
#include <blt/std/types.h>
|
|
|
|
#include <blt/std/hashmap.h>
|
2025-02-12 22:17:04 -05:00
|
|
|
#include <blt/fs/path_helper.h>
|
2025-02-12 02:54:22 -05:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
#include <variant>
|
|
|
|
#include <optional>
|
2025-02-12 22:17:04 -05:00
|
|
|
#include <memory>
|
2025-02-12 19:42:50 -05:00
|
|
|
#include <type_traits>
|
2025-02-12 02:54:22 -05:00
|
|
|
#include <blt/iterator/enumerate.h>
|
2025-02-12 19:42:50 -05:00
|
|
|
#include <blt/std/ranges.h>
|
|
|
|
#include <blt/std/utility.h>
|
2025-02-12 02:54:22 -05:00
|
|
|
|
|
|
|
namespace blt::argparse
|
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
class argument_string_t;
|
|
|
|
class argument_consumer_t;
|
|
|
|
class argument_parser_t;
|
|
|
|
class argument_subparser_t;
|
|
|
|
class parsed_argset_t;
|
|
|
|
class argument_builder_t;
|
|
|
|
|
2025-02-12 02:54:22 -05:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
class bad_flag final : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit bad_flag(const std::string& message): std::runtime_error(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit bad_flag(const char* message): std::runtime_error(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-02-13 13:59:59 -05:00
|
|
|
class subparse_error final : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit subparse_error(const std::string_view found_string, std::vector<std::string_view> allowed_strings): m_found_string(found_string),
|
|
|
|
m_allowed_strings(std::move(allowed_strings))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::vector<std::string_view>& get_allowed_strings() const
|
|
|
|
{
|
|
|
|
return m_allowed_strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view get_found_string() const
|
|
|
|
{
|
|
|
|
return m_found_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string error_string() const
|
|
|
|
{
|
|
|
|
std::string message = "Subparser Error: ";
|
|
|
|
message += m_found_string;
|
|
|
|
message += " is not a valid command. Allowed commands are: {";
|
|
|
|
for (const auto [i, allowed_string] : enumerate(m_allowed_strings))
|
|
|
|
{
|
|
|
|
message += allowed_string;
|
|
|
|
if (i != m_allowed_strings.size() - 1)
|
|
|
|
message += ' ';
|
|
|
|
}
|
|
|
|
message += "}";
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const char* what() const override
|
|
|
|
{
|
|
|
|
return "Please use error_string() method instead of what(). This exception should *always* be caught!";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string_view m_found_string;
|
|
|
|
std::vector<std::string_view> m_allowed_strings;
|
|
|
|
};
|
|
|
|
|
2025-02-12 02:54:22 -05:00
|
|
|
template <typename... Args>
|
|
|
|
struct arg_data_helper_t
|
|
|
|
{
|
|
|
|
using arg_primitive_data_t = std::variant<Args...>;
|
|
|
|
using arg_list_data_t = std::variant<std::vector<Args>...>;
|
|
|
|
};
|
|
|
|
|
|
|
|
using data_helper_t = arg_data_helper_t<i8, i16, i32, i64, u8, u16, u32, u64, float, double>;
|
|
|
|
|
|
|
|
using arg_primitive_data_t = data_helper_t::arg_primitive_data_t;
|
|
|
|
using arg_list_data_t = data_helper_t::arg_list_data_t;
|
|
|
|
using arg_data_t = std::variant<arg_primitive_data_t, arg_list_data_t>;
|
|
|
|
|
|
|
|
template <typename T>
|
2025-02-12 19:42:50 -05:00
|
|
|
struct arg_type_t
|
|
|
|
{
|
|
|
|
static T convert(const std::string_view value)
|
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
static_assert(std::is_arithmetic_v<T>, "Type must be arithmetic!");
|
2025-02-12 19:42:50 -05:00
|
|
|
const std::string temp{value};
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
if constexpr (std::is_same_v<T, float>)
|
2025-02-12 19:42:50 -05:00
|
|
|
{
|
|
|
|
return std::stof(temp);
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, double>)
|
|
|
|
{
|
|
|
|
return std::stod(temp);
|
2025-02-12 22:17:04 -05:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_unsigned_v<T>)
|
2025-02-12 19:42:50 -05:00
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return static_cast<T>(std::stoull(temp));
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_signed_v<T>)
|
|
|
|
{
|
|
|
|
return static_cast<T>(std::stoll(temp));
|
2025-02-12 19:42:50 -05:00
|
|
|
}
|
|
|
|
BLT_UNREACHABLE;
|
|
|
|
}
|
|
|
|
};
|
2025-02-12 02:54:22 -05:00
|
|
|
|
|
|
|
void test();
|
|
|
|
}
|
|
|
|
|
|
|
|
class argument_string_t
|
|
|
|
{
|
|
|
|
public:
|
2025-02-13 01:53:21 -05:00
|
|
|
explicit argument_string_t(const char* input, const hashset_t<char>& allowed_flag_prefix): m_argument(input),
|
|
|
|
allowed_flag_prefix(&allowed_flag_prefix)
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
|
|
|
if (input == nullptr)
|
|
|
|
throw detail::bad_flag("Argument cannot be null!");
|
2025-02-13 01:53:21 -05:00
|
|
|
process_argument();
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view get_flag() const
|
|
|
|
{
|
2025-02-13 01:53:21 -05:00
|
|
|
return m_flag_section;
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view get_name() const
|
|
|
|
{
|
2025-02-13 01:53:21 -05:00
|
|
|
return m_name_section;
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view value() const
|
|
|
|
{
|
|
|
|
return get_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool is_flag() const
|
|
|
|
{
|
2025-02-13 01:53:21 -05:00
|
|
|
return !m_flag_section.empty();
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view get_argument() const
|
|
|
|
{
|
|
|
|
return m_argument;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2025-02-13 01:53:21 -05:00
|
|
|
void process_argument()
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-13 01:53:21 -05:00
|
|
|
size_t start = 0;
|
|
|
|
for (; start < m_argument.size() && allowed_flag_prefix->contains(m_argument[start]); start++)
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
|
|
|
}
|
2025-02-13 01:53:21 -05:00
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
m_flag_section = {m_argument.data(), start};
|
|
|
|
m_name_section = {m_argument.data() + start, m_argument.size() - start};
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view m_argument;
|
2025-02-13 01:53:21 -05:00
|
|
|
std::string_view m_flag_section;
|
|
|
|
std::string_view m_name_section;
|
|
|
|
const hashset_t<char>* allowed_flag_prefix;
|
2025-02-12 02:54:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class argument_consumer_t
|
|
|
|
{
|
|
|
|
public:
|
2025-02-12 22:17:04 -05:00
|
|
|
explicit argument_consumer_t(const span<argument_string_t>& args): m_args(args)
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-12 19:42:50 -05:00
|
|
|
[[nodiscard]] argument_string_t peek(const i32 offset = 0) const
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return m_args[m_forward_index + offset];
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-12 19:42:50 -05:00
|
|
|
argument_string_t consume()
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return m_args[m_forward_index++];
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] i32 position() const
|
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return m_forward_index;
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] i32 remaining() const
|
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return static_cast<i32>(m_args.size()) - m_forward_index;
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool has_next(const i32 offset = 0) const
|
|
|
|
{
|
2025-02-12 22:17:04 -05:00
|
|
|
return (offset + m_forward_index) < m_args.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
span<argument_string_t> m_args;
|
|
|
|
i32 m_forward_index = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class argument_builder_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
class parsed_argset_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
class argument_parser_t
|
|
|
|
{
|
2025-02-13 13:59:59 -05:00
|
|
|
friend argument_subparser_t;
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
public:
|
2025-02-13 01:53:21 -05:00
|
|
|
explicit argument_parser_t(const std::optional<std::string_view> name = {}, const std::optional<std::string_view> usage = {}):
|
|
|
|
m_name(name), m_usage(usage)
|
2025-02-12 22:17:04 -05:00
|
|
|
{
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
argument_parser_t& set_name(const std::string_view name)
|
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2025-02-13 13:59:59 -05:00
|
|
|
void parse(argument_consumer_t& consumer) // NOLINT
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
private:
|
|
|
|
std::optional<std::string> m_name;
|
|
|
|
std::optional<std::string> m_usage;
|
|
|
|
std::optional<std::string> m_description;
|
|
|
|
std::optional<std::string> m_epilogue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class argument_subparser_t
|
|
|
|
{
|
|
|
|
public:
|
2025-02-13 13:59:59 -05:00
|
|
|
explicit argument_subparser_t(const argument_parser_t& parent): m_parent(&parent)
|
2025-02-13 01:53:21 -05:00
|
|
|
{
|
|
|
|
}
|
2025-02-12 22:17:04 -05:00
|
|
|
|
2025-02-13 13:59:59 -05:00
|
|
|
argument_parser_t& add_parser(const std::string_view name)
|
2025-02-13 01:53:21 -05:00
|
|
|
{
|
2025-02-13 13:59:59 -05:00
|
|
|
m_parsers.emplace(name);
|
|
|
|
return m_parsers[name];
|
|
|
|
}
|
2025-02-13 01:53:21 -05:00
|
|
|
|
2025-02-13 13:59:59 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the next argument using the provided argument consumer.
|
|
|
|
*
|
|
|
|
* This function uses an argument consumer to extract and process the next argument.
|
|
|
|
* If the argument is a flag or if it cannot be matched against the available parsers,
|
|
|
|
* an exception is thrown.
|
|
|
|
*
|
|
|
|
* @param consumer Reference to an argument_consumer_t object, which handles argument parsing.
|
|
|
|
* The consumer provides the next argument to be parsed.
|
|
|
|
*
|
|
|
|
* @throws detail::subparse_error If the argument is a flag or does not match any known parser.
|
|
|
|
*/
|
|
|
|
void parse(argument_consumer_t& consumer) // NOLINT
|
|
|
|
{
|
|
|
|
const auto key = consumer.consume();
|
|
|
|
if (key.is_flag())
|
|
|
|
throw detail::subparse_error(key.get_argument(), get_allowed_strings());
|
|
|
|
const auto it = m_parsers.find(key.get_name());
|
|
|
|
if (it == m_parsers.end())
|
|
|
|
throw detail::subparse_error(key.get_argument(), get_allowed_strings());
|
|
|
|
it->second.parse(consumer);
|
2025-02-13 01:53:21 -05:00
|
|
|
}
|
2025-02-13 13:59:59 -05:00
|
|
|
|
2025-02-13 01:53:21 -05:00
|
|
|
private:
|
2025-02-13 13:59:59 -05:00
|
|
|
[[nodiscard]] std::vector<std::string_view> get_allowed_strings() const
|
|
|
|
{
|
|
|
|
std::vector<std::string_view> vec;
|
|
|
|
for (const auto& [key, value] : m_parsers)
|
|
|
|
vec.push_back(key);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
const argument_parser_t* m_parent;
|
|
|
|
hashmap_t<std::string_view, argument_parser_t> m_parsers;
|
2025-02-12 02:54:22 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_PARSE_ARGPARSE_V2_H
|