1
0

feat: finish option and variable in clap

This commit is contained in:
2025-09-23 16:13:52 +08:00
parent c85830902b
commit 776adb0c96
6 changed files with 398 additions and 56 deletions

View File

@ -1,11 +1,10 @@
#pragma once
#include "types.hpp"
#include "../../macro/class_copy_move.hpp"
#include "../../string/op.hpp"
#include <optional>
#include <stdexcept>
#include <format>
#include <string>
#include <vector>
#include <map>
#define NS_YYCC_CLAP_TYPES ::yycc::carton::clap::types
@ -16,62 +15,22 @@ namespace yycc::carton::clap::option {
Option(std::optional<std::u8string_view> short_name,
std::optional<std::u8string_view> long_name,
std::optional<std::u8string_view> value_hint,
const std::u8string& description) :
short_name(short_name), long_name(long_name), value_hint(value_hint), description(description) {
if (!short_name.has_value() && !long_name.has_value()) {
throw std::logic_error("must have at least one name, short or long name");
}
if (short_name.has_value()) {
const auto& short_name_value = short_name.value();
if (!legal_short_name(short_name_value)) {
throw std::logic_error(std::format("invalid short name {}", short_name_value));
}
}
if (long_name.has_value()) {
const auto& long_name_value = long_name.value();
if (!legal_long_name(long_name_value)) {
throw std::logic_error(std::format("invalid long name {}", long_name_value));
}
}
}
~Option() {}
const std::u8string& description);
~Option();
YYCC_DEFAULT_COPY_MOVE(Option)
public:
std::optional<std::u8string_view> get_short_name() const { return this->short_name; }
std::optional<std::u8string_view> get_long_name() const { return this->long_name; }
std::optional<std::u8string_view> get_value_hint() const { return this->value_hint; }
std::u8string_view get_description() const { return this->description; }
std::optional<std::u8string_view> get_short_name() const;
std::optional<std::u8string_view> get_long_name() const;
std::optional<std::u8string_view> get_value_hint() const;
std::u8string_view get_description() const;
std::u8string to_showcase_name() {
namespace op = ::yycc::string::op;
if (short_name.has_value()) {
if (long_name.has_value()) {
} else {
}
} else {
if (long_name.has_value()) {
op::printf
} else {
throw std::runtime_error("both long name and short name are empty");
}
}
}
std::u8string to_showcase_name() const;
std::u8string to_showcase_value() const;
private:
static bool legal_short_name(const std::u8string_view& name) {
if (name.empty()) return false;
if (name.starts_with(NS_YYCC_CLAP_TYPES::DASH)) return false;
return true;
}
static bool legal_long_name(const std::u8string_view& name) {
if (name.empty()) return false;
return true;
}
static bool legal_short_name(const std::u8string_view& name);
static bool legal_long_name(const std::u8string_view& name);
private:
std::optional<std::u8string> short_name;
@ -80,6 +39,43 @@ namespace yycc::carton::clap::option {
std::u8string description;
};
class RegisteredOption {
public:
RegisteredOption(NS_YYCC_CLAP_TYPES::Token token, Option&& option);
~RegisteredOption();
YYCC_DEFAULT_COPY_MOVE(RegisteredOption)
public:
NS_YYCC_CLAP_TYPES::Token get_token() const;
const Option& get_option() const;
private:
NS_YYCC_CLAP_TYPES::Token token;
Option option;
};
class OptionCollection {
public:
OptionCollection();
~OptionCollection();
YYCC_DEFAULT_COPY_MOVE(OptionCollection)
public:
NS_YYCC_CLAP_TYPES::Token add_option(Option&& opt);
std::optional<NS_YYCC_CLAP_TYPES::Token> find_long_name(const std::u8string_view& long_name) const;
std::optional<NS_YYCC_CLAP_TYPES::Token> find_short_name(const std::u8string_view& short_name) const;
bool has_option(NS_YYCC_CLAP_TYPES::Token token) const;
const Option& get_option(NS_YYCC_CLAP_TYPES::Token token) const;
const std::vector<RegisteredOption>& all_options() const;
size_t length() const;
bool empty() const;
private:
std::map<std::u8string, NS_YYCC_CLAP_TYPES::Token> short_names;
std::map<std::u8string, NS_YYCC_CLAP_TYPES::Token> long_names;
std::vector<RegisteredOption> options;
};
} // namespace yycc::carton::clap::option
#undef NS_YYCC_CLAP_TYPES