From 5e70ad2bc4c64f16433359ed5964295741cea63b Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Fri, 4 Dec 2026 08:01:19 +0300 Subject: [PATCH] Argparse: colored help output by default (NO_COLOR aware) --- include/Void/Configuration/Argparse.hpp | 5 +++ src/Configuration/Argparse.cpp | 55 ++++++++++++++++++++----- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/include/Void/Configuration/Argparse.hpp b/include/Void/Configuration/Argparse.hpp index 6149678..dd7d3bf 100644 --- a/include/Void/Configuration/Argparse.hpp +++ b/include/Void/Configuration/Argparse.hpp @@ -140,6 +140,10 @@ namespace Void::Argparse // Omit an option or flag from the generated help. 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. Parser& positional(std::string_view name, std::string_view description, std::string& out); @@ -214,6 +218,7 @@ namespace Void::Argparse }; std::string program_, description_, version_; + bool m_color{true}; std::vector> conflicts_; std::vector flags_; std::vector options_; diff --git a/src/Configuration/Argparse.cpp b/src/Configuration/Argparse.cpp index bf0194c..96e7f40 100644 --- a/src/Configuration/Argparse.cpp +++ b/src/Configuration/Argparse.cpp @@ -9,6 +9,22 @@ namespace Void::Argparse { 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, std::string_view rhs) noexcept { @@ -205,6 +221,12 @@ Parser& Parser::hide(std::string_view lng) return *this; } +Parser& Parser::color(bool enabled) noexcept +{ + m_color = enabled; + return *this; +} + Parser& Parser::positional(std::string_view name, std::string_view desc, std::string& out) @@ -606,21 +628,32 @@ std::expected Parser::parse(int argc, 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; - os << "Usage: " << program_; + os << style("\033[1m", "Usage:") << " " << style("\033[1;36m", program_); if (!subcommands_.empty()) { - os << " "; + os << " " << style("\033[33m", ""); + } + 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()) { - os << (positional_many_->required ? " <" : " [") - << positional_many_->name - << (positional_many_->required ? "...>" : "...]"); + os << (positional_many_->required ? " " : " ") + << style("\033[33m", + (positional_many_->required ? "<" : "[") + + positional_many_->name + + (positional_many_->required ? "...>" : "...]")); } os << "\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; auto section = [&](std::string_view title, std::vector rows) { @@ -628,16 +661,16 @@ std::string Parser::help() const std::size_t width = 0; for (const auto& [left, _] : rows) width = std::max(width, left.size()); width = std::min(width, 30); - os << "\n" << title << ":\n"; + os << "\n" << style("\033[1;34m", std::string{title} + ":") << "\n"; for (const auto& [left, desc] : rows) { - os << " " << left; + os << " " << style("\033[36m", left); if (!desc.empty()) { if (left.size() >= width) { os << "\n" << std::string(width + 4, ' '); } else { os << std::string(width - left.size() + 2, ' '); } - os << desc; + os << style("\033[2m", desc); } os << "\n"; }