Argparse: colored help output by default (NO_COLOR aware)
This commit is contained in:
1 parent
b8e6e924a8
commit
5e70ad2bc4
2 files changed
+49
-11
No files matched your search
@@ -140,6 +140,10 @@ namespace Void::Argparse
|
|||||||
// Omit an option or flag from the generated help.
|
// Omit an option or flag from the generated help.
|
||||||
Parser& hide(std::string_view long_name);
|
Parser& hide(std::string_view long_name);
|
||||||
|
|
||||||
|
// Enable or disable ANSI styling in help text (default: enabled,
|
||||||
|
// unless the NO_COLOR environment variable is set).
|
||||||
|
Parser& color(bool enabled) noexcept;
|
||||||
|
|
||||||
// Add a required positional argument.
|
// Add a required positional argument.
|
||||||
Parser& positional(std::string_view name,
|
Parser& positional(std::string_view name,
|
||||||
std::string_view description, std::string& out);
|
std::string_view description, std::string& out);
|
||||||
@@ -214,6 +218,7 @@ namespace Void::Argparse
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::string program_, description_, version_;
|
std::string program_, description_, version_;
|
||||||
|
bool m_color{true};
|
||||||
std::vector<std::pair<std::string, std::string>> conflicts_;
|
std::vector<std::pair<std::string, std::string>> conflicts_;
|
||||||
std::vector<FlagEntry> flags_;
|
std::vector<FlagEntry> flags_;
|
||||||
std::vector<OptionEntry> options_;
|
std::vector<OptionEntry> options_;
|
||||||
|
|||||||
@@ -9,6 +9,22 @@ namespace Void::Argparse
|
|||||||
{
|
{
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
// ANSI styling is on by default; NO_COLOR (any non-empty value) disables it.
|
||||||
|
[[nodiscard]] bool terminal_color_enabled() noexcept
|
||||||
|
{
|
||||||
|
const char* no_color = std::getenv("NO_COLOR");
|
||||||
|
return !(no_color != nullptr && *no_color != '\0');
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::string paint(bool enabled, std::string_view code,
|
||||||
|
std::string_view text)
|
||||||
|
{
|
||||||
|
if (!enabled) {
|
||||||
|
return std::string{text};
|
||||||
|
}
|
||||||
|
return std::string{code} + std::string{text} + "\033[0m";
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::size_t edit_distance(std::string_view lhs,
|
[[nodiscard]] std::size_t edit_distance(std::string_view lhs,
|
||||||
std::string_view rhs) noexcept
|
std::string_view rhs) noexcept
|
||||||
{
|
{
|
||||||
@@ -205,6 +221,12 @@ Parser& Parser::hide(std::string_view lng)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Parser& Parser::color(bool enabled) noexcept
|
||||||
|
{
|
||||||
|
m_color = enabled;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Parser& Parser::positional(std::string_view name,
|
Parser& Parser::positional(std::string_view name,
|
||||||
std::string_view desc,
|
std::string_view desc,
|
||||||
std::string& out)
|
std::string& out)
|
||||||
@@ -606,21 +628,32 @@ std::expected<void, std::string> Parser::parse(int argc,
|
|||||||
|
|
||||||
std::string Parser::help() const
|
std::string Parser::help() const
|
||||||
{
|
{
|
||||||
|
const bool color = m_color && terminal_color_enabled();
|
||||||
|
const auto style = [color](std::string_view code, std::string_view text) {
|
||||||
|
return paint(color, code, text);
|
||||||
|
};
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << "Usage: " << program_;
|
os << style("\033[1m", "Usage:") << " " << style("\033[1;36m", program_);
|
||||||
if (!subcommands_.empty()) {
|
if (!subcommands_.empty()) {
|
||||||
os << " <command>";
|
os << " " << style("\033[33m", "<command>");
|
||||||
|
}
|
||||||
|
os << " " << style("\033[2m", "[OPTIONS]");
|
||||||
|
for (const auto& p : positionals_) {
|
||||||
|
os << " " << style("\033[33m", "<" + p.name + ">");
|
||||||
}
|
}
|
||||||
os << " [OPTIONS]";
|
|
||||||
for (const auto& p : positionals_) os << " <" << p.name << ">";
|
|
||||||
if (positional_many_.has_value()) {
|
if (positional_many_.has_value()) {
|
||||||
os << (positional_many_->required ? " <" : " [")
|
os << (positional_many_->required ? " " : " ")
|
||||||
<< positional_many_->name
|
<< style("\033[33m",
|
||||||
<< (positional_many_->required ? "...>" : "...]");
|
(positional_many_->required ? "<" : "[") +
|
||||||
|
positional_many_->name +
|
||||||
|
(positional_many_->required ? "...>" : "...]"));
|
||||||
}
|
}
|
||||||
os << "\n";
|
os << "\n";
|
||||||
if (!description_.empty()) os << "\n" << description_ << "\n";
|
if (!description_.empty()) os << "\n" << description_ << "\n";
|
||||||
if (!version_.empty()) os << "\n" << program_ << " " << version_ << "\n";
|
if (!version_.empty()) {
|
||||||
|
os << "\n" << style("\033[2m", program_ + " " + version_) << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
using Row = std::pair<std::string, std::string>;
|
using Row = std::pair<std::string, std::string>;
|
||||||
auto section = [&](std::string_view title, std::vector<Row> rows) {
|
auto section = [&](std::string_view title, std::vector<Row> rows) {
|
||||||
@@ -628,16 +661,16 @@ std::string Parser::help() const
|
|||||||
std::size_t width = 0;
|
std::size_t width = 0;
|
||||||
for (const auto& [left, _] : rows) width = std::max(width, left.size());
|
for (const auto& [left, _] : rows) width = std::max(width, left.size());
|
||||||
width = std::min<std::size_t>(width, 30);
|
width = std::min<std::size_t>(width, 30);
|
||||||
os << "\n" << title << ":\n";
|
os << "\n" << style("\033[1;34m", std::string{title} + ":") << "\n";
|
||||||
for (const auto& [left, desc] : rows) {
|
for (const auto& [left, desc] : rows) {
|
||||||
os << " " << left;
|
os << " " << style("\033[36m", left);
|
||||||
if (!desc.empty()) {
|
if (!desc.empty()) {
|
||||||
if (left.size() >= width) {
|
if (left.size() >= width) {
|
||||||
os << "\n" << std::string(width + 4, ' ');
|
os << "\n" << std::string(width + 4, ' ');
|
||||||
} else {
|
} else {
|
||||||
os << std::string(width - left.size() + 2, ' ');
|
os << std::string(width - left.size() + 2, ' ');
|
||||||
}
|
}
|
||||||
os << desc;
|
os << style("\033[2m", desc);
|
||||||
}
|
}
|
||||||
os << "\n";
|
os << "\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user