1
0

feat: finish summary and application for clap.

- finish summary and application for clap.
- add patch for utf8 string in std::format.
This commit is contained in:
2025-09-24 16:20:21 +08:00
parent 776adb0c96
commit a61955bb09
8 changed files with 203 additions and 2 deletions

View File

@ -33,6 +33,8 @@ PRIVATE
yycc/carton/csconsole.cpp
yycc/carton/clap/option.cpp
yycc/carton/clap/variable.cpp
yycc/carton/clap/summary.cpp
yycc/carton/clap/application.cpp
)
target_sources(YYCCommonplace
PUBLIC
@ -53,6 +55,7 @@ FILES
yycc/string/reinterpret.hpp
yycc/string/op.hpp
yycc/string/stream.hpp
yycc/string/format.hpp
yycc/patch/ptr_pad.hpp
yycc/patch/fopen.hpp
yycc/num/parse.hpp
@ -88,6 +91,8 @@ FILES
yycc/carton/clap/types.hpp
yycc/carton/clap/option.hpp
yycc/carton/clap/variable.hpp
yycc/carton/clap/summary.hpp
yycc/carton/clap/application.hpp
)
# Setup header infomations
target_include_directories(YYCCommonplace

View File

@ -0,0 +1,28 @@
#include "application.hpp"
#define NS_YYCC_CLAP ::yycc::carton::clap
namespace yycc::carton::clap::application {
using Summary = NS_YYCC_CLAP::summary::Summary;
using OptionCollection = NS_YYCC_CLAP::option::OptionCollection;
using VariableCollection = NS_YYCC_CLAP::variable::VariableCollection;
Application::Application(Summary &&summary, OptionCollection &&options, VariableCollection &&variables) :
summary(std::move(summary)), options(std::move(options)), variables(std::move(variables)) {}
Application::~Application() {}
const Summary &Application::get_summary() const {
return this->summary;
}
const OptionCollection &Application::get_options() const {
return this->options;
}
const VariableCollection &Application::get_variables() const {
return this->variables;
}
} // namespace yycc::carton::clap::application

View File

@ -0,0 +1,31 @@
#pragma once
#include "summary.hpp"
#include "option.hpp"
#include "variable.hpp"
#define NS_YYCC_CLAP ::yycc::carton::clap
namespace yycc::carton::clap::application {
class Application {
public:
Application(NS_YYCC_CLAP::summary::Summary&& summary,
NS_YYCC_CLAP::option::OptionCollection&& options,
NS_YYCC_CLAP::variable::VariableCollection&& variables);
~Application();
YYCC_DEFAULT_COPY_MOVE(Application);
public:
const NS_YYCC_CLAP::summary::Summary& get_summary() const;
const NS_YYCC_CLAP::option::OptionCollection& get_options() const;
const NS_YYCC_CLAP::variable::VariableCollection& get_variables() const;
private:
NS_YYCC_CLAP::summary::Summary summary;
NS_YYCC_CLAP::option::OptionCollection options;
NS_YYCC_CLAP::variable::VariableCollection variables;
};
}
#undef NS_YYCC_CLAP

View File

@ -1,5 +1,6 @@
#include "option.hpp"
#include "../../string/op.hpp"
#include "../../string/format.hpp"
#include <stdexcept>
#include <format>
@ -117,7 +118,8 @@ namespace yycc::carton::clap::option {
if (short_name.has_value()) {
std::u8string short_name_value(short_name.value());
if (this->long_names.contains(short_name_value)) {
throw std::logic_error(std::format("short name {} is duplicated with same long name", short_name_value));
throw std::logic_error(
std::format("short name {} is duplicated with same long name", short_name_value));
}
auto [_, ok] = this->short_names.try_emplace(short_name_value, token);
if (!ok) {
@ -128,7 +130,8 @@ namespace yycc::carton::clap::option {
if (long_name.has_value()) {
std::u8string long_name_value(long_name.value());
if (this->short_names.contains(long_name_value)) {
throw std::logic_error(std::format("long name {} is duplicated with same short name", long_name_value));
throw std::logic_error(
std::format("long name {} is duplicated with same short name", long_name_value));
}
auto [_, ok] = this->long_names.try_emplace(long_name_value, token);
if (!ok) {

View File

@ -0,0 +1,28 @@
#include "summary.hpp"
namespace yycc::carton::clap::summary {
Summary::Summary(const std::u8string_view &name,
const std::u8string_view &author,
const std::u8string_view &version,
const std::u8string_view &description) : name(name), author(author), version(version), description(description) {}
Summary::~Summary() {}
std::u8string_view Summary::get_name() const {
return this->name;
}
std::u8string_view Summary::get_author() const {
return this->author;
}
std::u8string_view Summary::get_version() const {
return this->version;
}
std::u8string_view Summary::get_description() const {
return this->description;
}
} // namespace yycc::carton::clap::summary

View File

@ -0,0 +1,27 @@
#pragma once
#include "../../macro/class_copy_move.hpp"
#include <string>
#include <string_view>
namespace yycc::carton::clap::summary {
class Summary {
public:
Summary(const std::u8string_view& name,
const std::u8string_view& author,
const std::u8string_view& version,
const std::u8string_view& description);
~Summary();
YYCC_DEFAULT_COPY_MOVE(Summary)
public:
std::u8string_view get_name() const;
std::u8string_view get_author() const;
std::u8string_view get_version() const;
std::u8string_view get_description() const;
private:
std::u8string name, author, version, description;
};
}

View File

@ -1,4 +1,5 @@
#include "variable.hpp"
#include "../../string/format.hpp"
#include <stdexcept>
#include <format>

View File

@ -0,0 +1,78 @@
/**
* @file
* @brief The patch for \c std::format to allow UTF8 string as arguments.
* @details
* As we known, \c std::format only allow \c char and \c wchar_t as its char type in C++ 23 currently.
* So it is impossible to use UTF8 string for std::format, both format string and argument.
* This namespace give a patch for this shortcoming.
* By including this file directly, you will have abilities that use UTF8 string as argument in \c std::format with \c char char type.
*/
#pragma once
#include "reinterpret.hpp"
#include <format>
#include <string>
#include <string_view>
#define NS_YYCC_STRING_REINTERPRET ::yycc::string::reinterpret
#pragma region Utf8 Formatter
// Add std::formatter specialization for "char8_t*"
template<>
struct std::formatter<char8_t*, char> {
constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); }
auto format(char8_t* str, auto& ctx) const { return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary(str), ctx); }
private:
std::formatter<const char*, char> underlying_formatter{};
};
// Add std::formatter specialization for "const char8_t*"
template<>
struct std::formatter<const char8_t*, char> {
constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); }
auto format(const char8_t* str, auto& ctx) const {
return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary(str), ctx);
}
private:
std::formatter<const char*, char> underlying_formatter{};
};
// Add std::formatter specialization for "char8_t[N]"
template<std::size_t N>
struct std::formatter<char8_t[N], char> {
constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); }
auto format(const char8_t (&str)[N], auto& ctx) const { return underlying_formatter.format(std::basic_string_view<char>(str, N), ctx); }
private:
std::formatter<std::basic_string_view<char>, char> underlying_formatter{};
};
// Add std::formatter specialization for "std::u8string"
template<class Traits, class Alloc>
struct std::formatter<std::basic_string<char8_t, Traits, Alloc>, char> {
constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); }
auto format(const std::u8string& str, auto& ctx) const {
return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary_view(str), ctx);
}
private:
std::formatter<std::basic_string_view<char>, char> underlying_formatter{};
};
// Add std::formatter specialization for "std::u8string_view"
template<class Traits>
struct std::formatter<std::basic_string_view<char8_t, Traits>, char> {
constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); }
auto format(std::u8string_view sv, auto& ctx) const {
return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary_view(sv), ctx);
}
private:
std::formatter<std::basic_string_view<char>, char> underlying_formatter{};
};
#pragma endregion
#undef NS_YYCC_STRING_REINTERPRET