1
0

feat: finish clap parser basic layout

This commit is contained in:
2025-12-08 15:16:28 +08:00
parent 8b7ab2c870
commit eb9e576d33
7 changed files with 160 additions and 3 deletions

View File

@ -0,0 +1,53 @@
#pragma once
#include "../../macro/class_copy_move.hpp"
#include "application.hpp"
#include "validator.hpp"
#include <map>
#include <optional>
#include <string>
#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 Parser {
public:
Parser(const NS_YYCC_CLAP_APPLICATION::Application& app);
~Parser();
YYCC_DEFAULT_COPY_MOVE(Parser)
private:
NS_YYCC_CLAP_TYPES::ClapResult<std::u8string_view> get_raw_value_option(NS_YYCC_CLAP_TYPES::Token token) const;
public:
bool has_option(NS_YYCC_CLAP_TYPES::Token token) const;
NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_option(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_option(
NS_YYCC_CLAP_TYPES::Token token) const {
auto raw_value = this->get_raw_value_option(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())
}
}
private:
/**
* @brief All captured commandline argument.
* @details Key is the token to already registered option.
* Value is the associated value for key token.
* If it is no-value option, the value will be \c std::nullopt.
*/
std::map<NS_YYCC_CLAP_TYPES::Token, std::optional<std::u8string>> values;
};
} // namespace yycc::carton::clap::resolver
#undef NS_YYCC_CLAP_TYPES