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-13 17:47:27 -05:00
|
|
|
#include <functional>
|
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 argument_builder_t;
|
2025-02-13 17:47:27 -05:00
|
|
|
class argument_storage_t;
|
|
|
|
|
|
|
|
enum class action_t
|
|
|
|
{
|
|
|
|
STORE,
|
|
|
|
STORE_CONST,
|
|
|
|
STORE_TRUE,
|
|
|
|
STORE_FALSE,
|
|
|
|
APPEND,
|
|
|
|
APPEND_CONST,
|
|
|
|
COUNT,
|
|
|
|
HELP,
|
|
|
|
VERSION,
|
|
|
|
EXTEND,
|
|
|
|
SUBCOMMAND
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class nargs_t
|
|
|
|
{
|
|
|
|
IF_POSSIBLE,
|
|
|
|
ALL,
|
|
|
|
ALL_AT_LEAST_ONE
|
|
|
|
};
|
|
|
|
|
|
|
|
using nargs_v = std::variant<nargs_t, i32>;
|
2025-02-12 22:17:04 -05:00
|
|
|
|
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 17:47:27 -05:00
|
|
|
class missing_argument_error final : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit missing_argument_error(const std::string& message): std::runtime_error(message)
|
|
|
|
{
|
|
|
|
}
|
2025-02-16 23:22:00 -05:00
|
|
|
};
|
2025-02-13 17:47:27 -05:00
|
|
|
|
2025-02-13 13:59:59 -05:00
|
|
|
class subparse_error final : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
2025-02-13 17:47:27 -05:00
|
|
|
explicit subparse_error(const std::string_view found_string, std::vector<std::vector<std::string_view>> allowed_strings):
|
|
|
|
m_found_string(found_string),
|
2025-02-13 13:59:59 -05:00
|
|
|
m_allowed_strings(std::move(allowed_strings))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
[[nodiscard]] const std::vector<std::vector<std::string_view>>& get_allowed_strings() const
|
2025-02-13 13:59:59 -05:00
|
|
|
{
|
|
|
|
return m_allowed_strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view get_found_string() const
|
|
|
|
{
|
|
|
|
return m_found_string;
|
|
|
|
}
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
[[nodiscard]] std::string error_string() const;
|
2025-02-13 13:59:59 -05:00
|
|
|
|
|
|
|
[[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;
|
2025-02-13 17:47:27 -05:00
|
|
|
std::vector<std::vector<std::string_view>> m_allowed_strings;
|
2025-02-13 13:59:59 -05:00
|
|
|
};
|
|
|
|
|
2025-02-12 02:54:22 -05:00
|
|
|
template <typename... Args>
|
|
|
|
struct arg_data_helper_t
|
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
using variant_t = std::variant<Args..., std::vector<Args>...>;
|
2025-02-12 02:54:22 -05:00
|
|
|
};
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
using arg_data_t = arg_data_helper_t<i8, i16, i32, i64, u8, u16, u32, u64, float, double, std::string_view>::variant_t;
|
2025-02-12 02:54:22 -05:00
|
|
|
|
|
|
|
template <typename T>
|
2025-02-16 23:22:00 -05:00
|
|
|
struct arg_string_converter_t
|
2025-02-12 19:42:50 -05:00
|
|
|
{
|
|
|
|
static T convert(const std::string_view value)
|
|
|
|
{
|
2025-02-13 17:47:27 -05:00
|
|
|
static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, std::string_view> || std::is_same_v<T, std::string>,
|
|
|
|
"Type must be arithmetic, string_view or string!");
|
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
|
|
|
}
|
2025-02-13 17:47:27 -05:00
|
|
|
else if constexpr (std::is_same_v<T, std::string_view>)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, std::string>)
|
|
|
|
{
|
|
|
|
return std::string(value);
|
|
|
|
}
|
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),
|
2025-02-16 23:22:00 -05:00
|
|
|
m_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-16 23:22:00 -05:00
|
|
|
/**
|
|
|
|
* This function takes the command line argument represented by this class,
|
|
|
|
* stored in m_argument and converts into flag side and name side arguments.
|
|
|
|
*
|
|
|
|
* What this means is in the case of a flag provided, for example passing --foo or --bar, this function will split the argument into
|
|
|
|
*
|
|
|
|
* m_flag_section = "--"
|
|
|
|
* m_name_section = "foo" || "bar"
|
|
|
|
*
|
|
|
|
* If the argument is purely positional, meaning there is no flag prefix,
|
|
|
|
* this function will do nothing and m_flag_section will be true for .empty()
|
|
|
|
*
|
|
|
|
* For example, if you provide res/some/folder/or/file as a command line positional argument,
|
|
|
|
* this function will create the following internal state:
|
|
|
|
*
|
|
|
|
* m_flag_section = ""
|
|
|
|
* m_flag_section.empty() == true
|
|
|
|
* m_name_section = "res/some/folder/or/file"
|
|
|
|
*
|
|
|
|
* m_argument is not modified by this function
|
|
|
|
*/
|
2025-02-13 01:53:21 -05:00
|
|
|
void process_argument()
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
// user provides a list of allowed prefix characters to argument_parser_t, which is then provided to this class at construction time
|
|
|
|
// it is not the job of this class to validate flag prefixes beyond this. // TODO: requiring this pointer is a code smell.
|
2025-02-13 01:53:21 -05:00
|
|
|
size_t start = 0;
|
2025-02-16 23:22:00 -05:00
|
|
|
for (; start < m_argument.size() && m_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;
|
2025-02-16 23:22:00 -05:00
|
|
|
const hashset_t<char>* m_allowed_flag_prefix;
|
2025-02-12 02:54:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class argument_consumer_t
|
|
|
|
{
|
|
|
|
public:
|
2025-02-16 23:22:00 -05:00
|
|
|
explicit argument_consumer_t(const span<argument_string_t>& args): m_begin(args.data()), m_end(args.data() + args.size())
|
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-16 23:22:00 -05:00
|
|
|
return *(m_begin + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] argument_string_t r_peek(const i32 offset = 0) const
|
|
|
|
{
|
|
|
|
return *(m_end - 1 - 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-16 23:22:00 -05:00
|
|
|
return *(m_begin++);
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
argument_string_t r_consume()
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
return *(--m_end);
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] i32 remaining() const
|
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
return static_cast<i32>(size());
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
[[nodiscard]] bool can_consume(const i32 amount = 0) const
|
2025-02-12 02:54:22 -05:00
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
return amount < remaining();
|
2025-02-12 22:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2025-02-16 23:22:00 -05:00
|
|
|
[[nodiscard]] ptrdiff_t size() const
|
|
|
|
{
|
|
|
|
return m_end - m_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
argument_string_t* m_begin;
|
|
|
|
argument_string_t* m_end;
|
2025-02-12 22:17:04 -05:00
|
|
|
};
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
class argument_storage_t
|
2025-02-12 22:17:04 -05:00
|
|
|
{
|
2025-02-13 17:47:27 -05:00
|
|
|
friend argument_parser_t;
|
|
|
|
friend argument_subparser_t;
|
|
|
|
friend argument_builder_t;
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
public:
|
2025-02-13 17:47:27 -05:00
|
|
|
template <typename T>
|
|
|
|
const T& get(const std::string_view key)
|
|
|
|
{
|
|
|
|
return std::get<T>(m_data[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view get(const std::string_view key)
|
|
|
|
{
|
|
|
|
return std::get<std::string_view>(m_data[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const std::string_view key)
|
|
|
|
{
|
|
|
|
return m_data.find(key) != m_data.end();
|
|
|
|
}
|
2025-02-12 22:17:04 -05:00
|
|
|
|
|
|
|
private:
|
2025-02-16 23:22:00 -05:00
|
|
|
void add(const argument_storage_t& values)
|
|
|
|
{
|
|
|
|
for (const auto value : values)
|
|
|
|
m_data.insert(value);
|
|
|
|
}
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
hashmap_t<std::string_view, detail::arg_data_t> m_data;
|
2025-02-12 22:17:04 -05:00
|
|
|
};
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
class argument_builder_t
|
2025-02-12 22:17:04 -05:00
|
|
|
{
|
2025-02-13 17:47:27 -05:00
|
|
|
friend argument_parser_t;
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
public:
|
2025-02-13 17:47:27 -05:00
|
|
|
argument_builder_t()
|
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
m_dest_func = [](const std::string_view dest, argument_storage_t& storage, std::string_view value)
|
2025-02-13 17:47:27 -05:00
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
storage.m_data.insert({dest, value});
|
|
|
|
};
|
|
|
|
m_dest_vec_func = [](const std::string_view dest, argument_storage_t& storage, const std::vector<std::string_view>& values)
|
|
|
|
{
|
|
|
|
storage.m_data.insert({dest, values});
|
2025-02-13 17:47:27 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
argument_builder_t& as_type()
|
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
m_dest_func = [](const std::string_view dest, argument_storage_t& storage, std::string_view value)
|
|
|
|
{
|
|
|
|
storage.m_data.insert({dest, detail::arg_string_converter_t<T>::convert(value)});
|
|
|
|
};
|
|
|
|
m_dest_vec_func = [](const std::string_view dest, argument_storage_t& storage, const std::vector<std::string_view>& values)
|
2025-02-13 17:47:27 -05:00
|
|
|
{
|
2025-02-16 23:22:00 -05:00
|
|
|
std::vector<T> converted_values;
|
|
|
|
for (const auto& value : values)
|
|
|
|
converted_values.push_back(detail::arg_string_converter_t<T>::convert(value));
|
|
|
|
storage.m_data.insert({dest, converted_values});
|
2025-02-13 17:47:27 -05:00
|
|
|
};
|
|
|
|
return *this;
|
|
|
|
}
|
2025-02-12 22:17:04 -05:00
|
|
|
|
|
|
|
private:
|
2025-02-16 23:22:00 -05:00
|
|
|
action_t m_action = action_t::STORE;
|
|
|
|
bool m_required = false; // do we require this argument to be provided as an argument?
|
|
|
|
nargs_v m_nargs = 1; // number of arguments to consume
|
|
|
|
std::optional<std::string> m_metavar; // variable name to be used in the help string
|
|
|
|
std::optional<std::string> m_help; // help string to be used in the help string
|
|
|
|
std::optional<std::vector<std::string>> m_choices; // optional allowed choices for this argument
|
|
|
|
std::optional<detail::arg_data_t> m_default_value;
|
|
|
|
std::optional<detail::arg_data_t> m_const_value;
|
|
|
|
std::optional<std::string> m_dest;
|
2025-02-13 17:47:27 -05:00
|
|
|
// dest, storage, value input
|
2025-02-16 23:22:00 -05:00
|
|
|
std::function<void(std::string_view, argument_storage_t&, std::string_view)> m_dest_func;
|
|
|
|
// dest, storage, value input
|
|
|
|
std::function<void(std::string_view, argument_storage_t&, const std::vector<std::string_view>& values)> m_dest_vec_func;
|
2025-02-12 22:17:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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 14:04:39 -05:00
|
|
|
explicit argument_parser_t(const std::optional<std::string_view> name = {}, const std::optional<std::string_view> usage = {},
|
|
|
|
const std::optional<std::string_view> description = {}, const std::optional<std::string_view> epilogue = {}):
|
|
|
|
m_name(name), m_usage(usage), m_description(description), m_epilogue(epilogue)
|
2025-02-12 22:17:04 -05:00
|
|
|
{
|
2025-02-12 02:54:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
template <typename... Aliases>
|
|
|
|
argument_builder_t& add_flag(const std::string_view arg, Aliases... aliases)
|
|
|
|
{
|
|
|
|
static_assert(
|
|
|
|
std::conjunction_v<std::disjunction<std::is_convertible<Aliases, std::string_view>, std::is_constructible<
|
|
|
|
std::string_view, Aliases>>...>,
|
|
|
|
"Arguments must be of type string_view, convertible to string_view or be string_view constructable");
|
|
|
|
m_argument_builders.emplace_back();
|
2025-02-16 23:22:00 -05:00
|
|
|
m_flag_arguments.insert({arg, &m_argument_builders.back()});
|
2025-02-13 17:47:27 -05:00
|
|
|
((m_flag_arguments[std::string_view{aliases}] = &m_argument_builders.back()), ...);
|
|
|
|
return m_argument_builders.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
argument_builder_t& add_positional(const std::string_view arg)
|
|
|
|
{
|
|
|
|
m_argument_builders.emplace_back();
|
2025-02-16 23:22:00 -05:00
|
|
|
m_positional_arguments.insert({arg, &m_argument_builders.back()});
|
2025-02-13 17:47:27 -05:00
|
|
|
return m_argument_builders.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
argument_subparser_t& add_subparser(std::string_view dest);
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
argument_storage_t parse(argument_consumer_t& consumer); // NOLINT
|
2025-02-13 17:47:27 -05:00
|
|
|
|
|
|
|
void print_help();
|
|
|
|
|
2025-02-16 23:22:00 -05:00
|
|
|
void print_usage();
|
|
|
|
|
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 17:47:27 -05:00
|
|
|
argument_parser_t& set_usage(const std::string_view usage)
|
|
|
|
{
|
|
|
|
m_usage = usage;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::optional<std::string>& get_usage() const
|
|
|
|
{
|
|
|
|
return m_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
argument_parser_t& set_description(const std::string_view description)
|
|
|
|
{
|
|
|
|
m_description = description;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::optional<std::string>& get_description() const
|
|
|
|
{
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
argument_parser_t& set_epilogue(const std::string_view epilogue)
|
|
|
|
{
|
|
|
|
m_epilogue = epilogue;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::optional<std::string>& get_epilogue() const
|
2025-02-13 13:59:59 -05:00
|
|
|
{
|
2025-02-13 17:47:27 -05:00
|
|
|
return m_epilogue;
|
2025-02-13 13:59:59 -05:00
|
|
|
}
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
private:
|
2025-02-16 23:22:00 -05:00
|
|
|
void parse_flag(argument_storage_t& parsed_args, argument_consumer_t& consumer, std::string_view arg);
|
|
|
|
void parse_positional(argument_storage_t& parsed_args, argument_consumer_t& consumer, std::string_view arg);
|
|
|
|
static void handle_missing_and_default_args(hashmap_t<std::string_view, argument_builder_t*>& arguments, const hashset_t<std::string_view>& found,
|
|
|
|
argument_storage_t& parsed_args, std::string_view type);
|
|
|
|
|
2025-02-12 22:17:04 -05:00
|
|
|
std::optional<std::string> m_name;
|
|
|
|
std::optional<std::string> m_usage;
|
|
|
|
std::optional<std::string> m_description;
|
|
|
|
std::optional<std::string> m_epilogue;
|
2025-02-13 17:47:27 -05:00
|
|
|
std::vector<std::pair<std::string_view, argument_subparser_t>> m_subparsers;
|
|
|
|
std::vector<argument_builder_t> m_argument_builders;
|
|
|
|
hashmap_t<std::string_view, argument_builder_t*> m_flag_arguments;
|
|
|
|
hashmap_t<std::string_view, argument_builder_t*> m_positional_arguments;
|
2025-02-12 22:17:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class argument_subparser_t
|
|
|
|
{
|
|
|
|
public:
|
2025-02-13 17:47:27 -05:00
|
|
|
explicit argument_subparser_t(const argument_parser_t& parent): m_parent(&parent)
|
2025-02-13 14:04:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-13 17:47:27 -05:00
|
|
|
template <typename... Aliases>
|
|
|
|
argument_parser_t& add_parser(const std::string_view name, Aliases... aliases)
|
2025-02-13 01:53:21 -05:00
|
|
|
{
|
2025-02-13 17:47:27 -05:00
|
|
|
static_assert(
|
|
|
|
std::conjunction_v<std::disjunction<std::is_convertible<Aliases, std::string_view>, std::is_constructible<
|
|
|
|
std::string_view, Aliases>>...>,
|
|
|
|
"Arguments must be of type string_view, convertible to string_view or be string_view constructable");
|
|
|
|
m_parsers.emplace(name);
|
|
|
|
((m_aliases[std::string_view{aliases}] = &m_parsers[name]), ...);
|
2025-02-13 13:59:59 -05:00
|
|
|
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.
|
|
|
|
*/
|
2025-02-16 23:22:00 -05:00
|
|
|
std::pair<argument_string_t, argument_storage_t> parse(argument_consumer_t& consumer); // NOLINT
|
2025-02-13 13:59:59 -05:00
|
|
|
|
2025-02-13 01:53:21 -05:00
|
|
|
private:
|
2025-02-13 17:47:27 -05:00
|
|
|
[[nodiscard]] std::vector<std::vector<std::string_view>> get_allowed_strings() const;
|
2025-02-13 13:59:59 -05:00
|
|
|
|
|
|
|
const argument_parser_t* m_parent;
|
|
|
|
hashmap_t<std::string_view, argument_parser_t> m_parsers;
|
2025-02-13 17:47:27 -05:00
|
|
|
hashmap_t<std::string_view, argument_parser_t*> m_aliases;
|
2025-02-12 02:54:22 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_PARSE_ARGPARSE_V2_H
|