66 lines
2.4 KiB
C++
66 lines
2.4 KiB
C++
#pragma once
|
|
#include "../../macro/class_copy_move.hpp"
|
|
#include "application.hpp"
|
|
#include "validator.hpp"
|
|
#include <vector>
|
|
#include <utility>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#define NS_YYCC_CLAP_TYPES ::yycc::carton::clap::types
|
|
#define NS_YYCC_CLAP_APPLICATION ::yycc::carton::clap::application
|
|
#define NS_YYCC_CLAP_VALIDATOR ::yycc::carton::clap::validator
|
|
|
|
namespace yycc::carton::clap::resolver {
|
|
|
|
class Resolver {
|
|
private:
|
|
/**
|
|
* @brief All captured environment variable.
|
|
* @details Key is the token to already registered variable.
|
|
* Value is the associated value for key token.
|
|
* If it is no-value variable, the value will be \c std::nullopt.
|
|
*/
|
|
std::map<NS_YYCC_CLAP_TYPES::Token, std::optional<std::u8string>> values;
|
|
|
|
public:
|
|
static NS_YYCC_CLAP_TYPES::ClapResult<Resolver> from_user(
|
|
const NS_YYCC_CLAP_APPLICATION::Application& app, const std::vector<std::pair<std::u8string_view, std::u8string_view>>& vars);
|
|
static NS_YYCC_CLAP_TYPES::ClapResult<Resolver> from_system(const NS_YYCC_CLAP_APPLICATION::Application& app);
|
|
|
|
private:
|
|
Resolver(decltype(Resolver::values)&& values);
|
|
|
|
public:
|
|
~Resolver();
|
|
YYCC_DEFAULT_COPY_MOVE(Resolver)
|
|
|
|
private:
|
|
NS_YYCC_CLAP_TYPES::ClapResult<std::u8string_view> get_raw_value_variable(NS_YYCC_CLAP_TYPES::Token token) const;
|
|
|
|
public:
|
|
bool has_variable(NS_YYCC_CLAP_TYPES::Token token) const;
|
|
NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_variable(NS_YYCC_CLAP_TYPES::Token token) const;
|
|
template<NS_YYCC_CLAP_VALIDATOR::Validator T>
|
|
NS_YYCC_CLAP_TYPES::ClapResult<NS_YYCC_CLAP_VALIDATOR::ValidatorReturnType<T>> get_value_variable(
|
|
NS_YYCC_CLAP_TYPES::Token token) const {
|
|
auto raw_value = this->get_raw_value_variable(token);
|
|
if (raw_value.has_value()) {
|
|
T validator{};
|
|
auto value = validator.validate(raw_value.value());
|
|
if (value.has_value()) return value.value();
|
|
else return std::unexpected(NS_YYCC_CLAP_TYPES::ClapError::BadCast);
|
|
} else {
|
|
return std::unexpected(raw_value.error());
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace yycc::carton::clap::resolver
|
|
|
|
#undef NS_YYCC_CLAP_VALIDATOR
|
|
#undef NS_YYCC_CLAP_APPLICATION
|
|
#undef NS_YYCC_CLAP_TYPES
|