1
0

feat: finish basic of manual in clap

This commit is contained in:
2025-09-25 15:52:28 +08:00
parent c8d763bdcf
commit ce3d5b9556
9 changed files with 65 additions and 31 deletions

View File

@ -31,11 +31,11 @@ PRIVATE
yycc/carton/tabulate.cpp yycc/carton/tabulate.cpp
yycc/carton/ironpad.cpp yycc/carton/ironpad.cpp
yycc/carton/csconsole.cpp yycc/carton/csconsole.cpp
#yycc/carton/clap/option.cpp yycc/carton/clap/option.cpp
#yycc/carton/clap/variable.cpp yycc/carton/clap/variable.cpp
#yycc/carton/clap/summary.cpp yycc/carton/clap/summary.cpp
#yycc/carton/clap/application.cpp yycc/carton/clap/application.cpp
#yycc/carton/clap/manual.cpp yycc/carton/clap/manual.cpp
) )
target_sources(YYCCommonplace target_sources(YYCCommonplace
PUBLIC PUBLIC

View File

@ -1,16 +1,18 @@
#include "manual.hpp" #include "manual.hpp"
#include "../termcolor.hpp" #include "../termcolor.hpp"
#include "../../patch/stream.hpp" #include "../../patch/stream.hpp"
#include "../../patch/format.hpp"
#include "../../string/op.hpp" #include "../../string/op.hpp"
#define CLAP ::yycc::carton::clap #define CLAP ::yycc::carton::clap
#define TABULATE ::yycc::carton::tabulate #define TABULATE ::yycc::carton::tabulate
#define TERMCOLOR ::yycc::carton::termcolor #define TERMCOLOR ::yycc::carton::termcolor
#define FORMAT ::yycc::patch::format
#define OP ::yycc::carton::op #define OP ::yycc::carton::op
using namespace ::yycc::patch::stream; using namespace ::yycc::patch::stream;
namespace yycc::carton::clap::manual::Manual { namespace yycc::carton::clap::manual {
#pragma region Manual Translation #pragma region Manual Translation
@ -27,6 +29,8 @@ namespace yycc::carton::clap::manual::Manual {
using Application = CLAP::application::Application; using Application = CLAP::application::Application;
using Tabulate = TABULATE::Tabulate; using Tabulate = TABULATE::Tabulate;
static constexpr char8_t INDENT[] = u8" ";
Manual::Manual(const Application &app, ManualTr &&trctx) : trctx(std::move(trctx)), app(app), opt_printer(3), var_printer(2) { Manual::Manual(const Application &app, ManualTr &&trctx) : trctx(std::move(trctx)), app(app), opt_printer(3), var_printer(2) {
this->setup_table(); this->setup_table();
this->fill_opt_table(); this->fill_opt_table();
@ -38,11 +42,11 @@ namespace yycc::carton::clap::manual::Manual {
void Manual::setup_table() { void Manual::setup_table() {
this->opt_printer.show_header(false); this->opt_printer.show_header(false);
this->opt_printer.show_bar(false); this->opt_printer.show_bar(false);
this->opt_printer.set_prefix(u8" "); this->opt_printer.set_prefix(INDENT);
this->var_printer.show_header(false); this->var_printer.show_header(false);
this->var_printer.show_bar(false); this->var_printer.show_bar(false);
this->var_printer.set_prefix(u8" "); this->var_printer.set_prefix(INDENT);
} }
void Manual::fill_opt_table() { void Manual::fill_opt_table() {
@ -57,13 +61,34 @@ namespace yycc::carton::clap::manual::Manual {
void Manual::print_version(std::ostream &dst) const { void Manual::print_version(std::ostream &dst) const {
const auto &summary = this->app.get_summary(); const auto &summary = this->app.get_summary();
dst << std::format TERMCOLOR::cprintln(summary.get_name(), TERMCOLOR::Color::Yellow, TERMCOLOR::Color::Default, TERMCOLOR::Attribute::Default, dst);
dst << summary.get_name() << std::endl;
dst << FORMAT::format(trctx.author_and_version, summary.get_author(), summary.get_version()) << std::endl;
dst << summary.get_description() << std::endl; dst << summary.get_description() << std::endl;
dst << std::endl; dst << std::endl;
} }
void Manual::print_help(std::ostream &dst) const {} void Manual::print_help(std::ostream &dst) const {
this->print_version();
TERMCOLOR::cprintln(trctx.usage_title, TERMCOLOR::Color::Yellow, TERMCOLOR::Color::Default, TERMCOLOR::Attribute::Default, dst);
dst << INDENT << FORMAT::format(trctx.usage_body, app.get_summary().get_bin_name()) << std::endl;
const auto &variables = app.get_variables();
if (!variables.empty()) {
TERMCOLOR::cprintln(trctx.avail_var, TERMCOLOR::Color::Yellow, TERMCOLOR::Color::Default, TERMCOLOR::Attribute::Default, dst);
this->var_printer.print(dst);
dst << std::endl;
}
const auto &options = app.get_options();
if (!options.empty()) {
TERMCOLOR::cprintln(trctx.avail_opt, TERMCOLOR::Color::Yellow, TERMCOLOR::Color::Default, TERMCOLOR::Attribute::Default, dst);
this->opt_printer.print(dst);
dst << std::endl;
}
}
#pragma endregion #pragma endregion
} // namespace yycc::carton::clap::manual::Manual } // namespace yycc::carton::clap::manual

View File

@ -7,7 +7,7 @@
#define NS_YYCC_CLAP ::yycc::carton::clap #define NS_YYCC_CLAP ::yycc::carton::clap
#define NS_YYCC_TABULATE ::yycc::carton::tabulate #define NS_YYCC_TABULATE ::yycc::carton::tabulate
namespace yycc::carton::clap::manual::Manual { namespace yycc::carton::clap::manual {
struct ManualTr { struct ManualTr {
public: public:
@ -20,7 +20,7 @@ namespace yycc::carton::clap::manual::Manual {
std::u8string usage_title, usage_body; std::u8string usage_title, usage_body;
std::u8string avail_opt, avail_var; std::u8string avail_opt, avail_var;
}; };
class Manual { class Manual {
public: public:
Manual(const NS_YYCC_CLAP::application::Application& app, ManualTr&& trctx = ManualTr()); Manual(const NS_YYCC_CLAP::application::Application& app, ManualTr&& trctx = ManualTr());
@ -43,7 +43,7 @@ namespace yycc::carton::clap::manual::Manual {
NS_YYCC_TABULATE::Tabulate var_printer; NS_YYCC_TABULATE::Tabulate var_printer;
}; };
} } // namespace yycc::carton::clap::manual
#undef NS_YYCC_TABULATE #undef NS_YYCC_TABULATE
#undef NS_YYCC_CLAP #undef NS_YYCC_CLAP

View File

@ -1,11 +1,9 @@
#include "option.hpp" #include "option.hpp"
#include "../../string/op.hpp"
#include "../../patch/format.hpp" #include "../../patch/format.hpp"
#include <stdexcept> #include <stdexcept>
#include <format>
#define TYPES ::yycc::carton::clap::types #define TYPES ::yycc::carton::clap::types
#define OP ::yycc::string::op #define FORMAT ::yycc::patch::format
namespace yycc::carton::clap::option { namespace yycc::carton::clap::option {
@ -23,13 +21,13 @@ namespace yycc::carton::clap::option {
if (short_name.has_value()) { if (short_name.has_value()) {
const auto& short_name_value = short_name.value(); const auto& short_name_value = short_name.value();
if (!legal_short_name(short_name_value)) { if (!legal_short_name(short_name_value)) {
throw std::logic_error(std::format("invalid short name {}", short_name_value)); throw std::logic_error(FORMAT::format("invalid short name {}", short_name_value));
} }
} }
if (long_name.has_value()) { if (long_name.has_value()) {
const auto& long_name_value = long_name.value(); const auto& long_name_value = long_name.value();
if (!legal_long_name(long_name_value)) { if (!legal_long_name(long_name_value)) {
throw std::logic_error(std::format("invalid long name {}", long_name_value)); throw std::logic_error(FORMAT::format("invalid long name {}", long_name_value));
} }
} }
} }
@ -55,13 +53,13 @@ namespace yycc::carton::clap::option {
std::u8string Option::to_showcase_name() const { std::u8string Option::to_showcase_name() const {
if (short_name.has_value()) { if (short_name.has_value()) {
if (long_name.has_value()) { if (long_name.has_value()) {
return OP::printf(u8"%s%s %s%s", TYPES::DASH, short_name.value().c_str(), TYPES::DOUBLE_DASH, long_name.value().c_str()); return FORMAT::format(u8"{}{} {}{}", TYPES::DASH, short_name.value(), TYPES::DOUBLE_DASH, long_name.value());
} else { } else {
return OP::printf(u8"%s%s", TYPES::DASH, short_name.value().c_str()); return FORMAT::format(u8"{}{}", TYPES::DASH, short_name.value());
} }
} else { } else {
if (long_name.has_value()) { if (long_name.has_value()) {
return OP::printf(u8"%s%s", TYPES::DOUBLE_DASH, long_name.value().c_str()); return FORMAT::format(u8"{}{}", TYPES::DOUBLE_DASH, long_name.value());
} else { } else {
throw std::runtime_error("both long name and short name are empty"); throw std::runtime_error("both long name and short name are empty");
} }
@ -70,7 +68,7 @@ namespace yycc::carton::clap::option {
std::u8string Option::to_showcase_value() const { std::u8string Option::to_showcase_value() const {
if (value_hint.has_value()) { if (value_hint.has_value()) {
return OP::printf(u8"<%s>", value_hint.value().c_str()); return FORMAT::format(u8"<{}>", value_hint.value());
} else { } else {
return {}; return {};
} }
@ -119,11 +117,11 @@ namespace yycc::carton::clap::option {
std::u8string short_name_value(short_name.value()); std::u8string short_name_value(short_name.value());
if (this->long_names.contains(short_name_value)) { if (this->long_names.contains(short_name_value)) {
throw std::logic_error( throw std::logic_error(
std::format("short name {} is duplicated with same long name", short_name_value)); FORMAT::format("short name {} is duplicated with same long name", short_name_value));
} }
auto [_, ok] = this->short_names.try_emplace(short_name_value, token); auto [_, ok] = this->short_names.try_emplace(short_name_value, token);
if (!ok) { if (!ok) {
throw std::logic_error(std::format("duplicate short name {}", short_name_value)); throw std::logic_error(FORMAT::format("duplicate short name {}", short_name_value));
} }
} }
const auto& long_name = opt.get_long_name(); const auto& long_name = opt.get_long_name();
@ -131,11 +129,11 @@ namespace yycc::carton::clap::option {
std::u8string long_name_value(long_name.value()); std::u8string long_name_value(long_name.value());
if (this->short_names.contains(long_name_value)) { if (this->short_names.contains(long_name_value)) {
throw std::logic_error( throw std::logic_error(
std::format("long name {} is duplicated with same short name", long_name_value)); FORMAT::format("long name {} is duplicated with same short name", long_name_value));
} }
auto [_, ok] = this->long_names.try_emplace(long_name_value, token); auto [_, ok] = this->long_names.try_emplace(long_name_value, token);
if (!ok) { if (!ok) {
throw std::logic_error(std::format("duplicate long name {}", long_name_value)); throw std::logic_error(FORMAT::format("duplicate long name {}", long_name_value));
} }
} }

View File

@ -3,9 +3,11 @@
namespace yycc::carton::clap::summary { namespace yycc::carton::clap::summary {
Summary::Summary(const std::u8string_view &name, Summary::Summary(const std::u8string_view &name,
const std::u8string_view &bin_name,
const std::u8string_view &author, const std::u8string_view &author,
const std::u8string_view &version, const std::u8string_view &version,
const std::u8string_view &description) : name(name), author(author), version(version), description(description) {} const std::u8string_view &description) :
name(name), bin_name(bin_name), author(author), version(version), description(description) {}
Summary::~Summary() {} Summary::~Summary() {}
@ -13,6 +15,10 @@ namespace yycc::carton::clap::summary {
return this->name; return this->name;
} }
std::u8string_view Summary::get_bin_name() const {
return this->bin_name;
}
std::u8string_view Summary::get_author() const { std::u8string_view Summary::get_author() const {
return this->author; return this->author;
} }

View File

@ -8,6 +8,7 @@ namespace yycc::carton::clap::summary {
class Summary { class Summary {
public: public:
Summary(const std::u8string_view& name, Summary(const std::u8string_view& name,
const std::u8string_view& bin_name,
const std::u8string_view& author, const std::u8string_view& author,
const std::u8string_view& version, const std::u8string_view& version,
const std::u8string_view& description); const std::u8string_view& description);
@ -16,12 +17,13 @@ namespace yycc::carton::clap::summary {
public: public:
std::u8string_view get_name() const; std::u8string_view get_name() const;
std::u8string_view get_bin_name() const;
std::u8string_view get_author() const; std::u8string_view get_author() const;
std::u8string_view get_version() const; std::u8string_view get_version() const;
std::u8string_view get_description() const; std::u8string_view get_description() const;
private: private:
std::u8string name, author, version, description; std::u8string name, bin_name, author, version, description;
}; };
} }

View File

@ -1,9 +1,9 @@
#include "variable.hpp" #include "variable.hpp"
#include "../../patch/format.hpp" #include "../../patch/format.hpp"
#include <stdexcept> #include <stdexcept>
#include <format>
#define TYPES ::yycc::carton::clap::types #define TYPES ::yycc::carton::clap::types
#define FORMAT ::yycc::patch::format
namespace yycc::carton::clap::variable { namespace yycc::carton::clap::variable {
@ -55,7 +55,7 @@ namespace yycc::carton::clap::variable {
std::u8string name(var.get_name()); std::u8string name(var.get_name());
auto [_, ok] = this->names.try_emplace(name, token); auto [_, ok] = this->names.try_emplace(name, token);
if (!ok) { if (!ok) {
throw std::logic_error(std::format("duplicated variable name {}", name)); throw std::logic_error(FORMAT::format("duplicated variable name {}", name));
} }
this->variables.emplace_back(RegisteredVariable(token, std::move(var))); this->variables.emplace_back(RegisteredVariable(token, std::move(var)));

View File

@ -20,6 +20,7 @@
namespace yycc::patch::format { namespace yycc::patch::format {
// TODO: order all use of std::format redirect to this function. // TODO: order all use of std::format redirect to this function.
// TODO: all use of OP::printf should also switch to this function if possible.
template<class... Args> template<class... Args>
std::string format(std::format_string<Args...> fmt, Args&&... args) { std::string format(std::format_string<Args...> fmt, Args&&... args) {

View File

@ -10,6 +10,8 @@
*/ */
namespace yycc::patch::stream { namespace yycc::patch::stream {
// TODO: replace all old way of outputing utf8 string with this new way.
std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str); std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str);
std::ostream& operator<<(std::ostream& os, const char8_t* u8str); std::ostream& operator<<(std::ostream& os, const char8_t* u8str);
std::ostream& operator<<(std::ostream& os, char8_t u8chr); std::ostream& operator<<(std::ostream& os, char8_t u8chr);