From 44905e449f8f071af5a9e96ec834559d5257b1d2 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 16 Dec 2024 12:03:45 +0000 Subject: [PATCH] Add Color and Design Disable Add Long args support Set Long Arg as default for GetArg --- include/cli-fancy.hpp | 144 ++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 53 deletions(-) diff --git a/include/cli-fancy.hpp b/include/cli-fancy.hpp index c946cbb..e3ae1ab 100644 --- a/include/cli-fancy.hpp +++ b/include/cli-fancy.hpp @@ -33,11 +33,13 @@ SOFTWARE. // CLI FANCY namespace cf7 { +static bool fancy_print = true; +static bool colors_supported = true; namespace sym { const std::string arrow = ""; } class col { - public: +public: col() { m_isres = true; } col(bool fg, unsigned char r, unsigned char g, unsigned char b) { m_isres = false; @@ -72,7 +74,7 @@ class col { } operator std::string() const { return Get(); } - operator const char*() const { + operator const char *() const { /// CREATE DYNAMIC BUFFER TO ALWAYS RETURN STABLE CONST CHAR static char buf[32]; strcpy(buf, Get().c_str()); @@ -80,34 +82,54 @@ class col { return buf; } - private: +private: bool m_isres = true; bool m_fg = false; unsigned char m_r = 0; unsigned char m_g = 0; unsigned char m_b = 0; }; -void PrintFancy(const std::vector>& e) { - std::cout << col(); - for (int i = 0; i < e.size(); i++) { - std::cout << e[i].second.GetAs(false) << col(true, 255, 255, 255); - std::cout << " " << e[i].first << " "; - if (i == e.size() - 1) { - std::cout << col() << e[i].second.GetAs(true) << sym::arrow; - } else { - std::cout << e[i + 1].second.GetAs(false) << e[i].second.GetAs(true) - << sym::arrow; +void PrintFancy(const std::vector> &e) { + if (fancy_print && colors_supported) { + std::cout << col(); + for (int i = 0; i < e.size(); i++) { + std::cout << e[i].second.GetAs(false) << col(true, 255, 255, 255); + std::cout << " " << e[i].first << " "; + if (i == e.size() - 1) { + std::cout << col() << e[i].second.GetAs(true) << sym::arrow; + } else { + std::cout << e[i + 1].second.GetAs(false) << e[i].second.GetAs(true) + << sym::arrow; + } } + std::cout << col() << std::endl; + } else { + for (int i = 0; i < e.size(); i++) { + if (colors_supported) { + std::cout << e[i].second.GetAs(true); + } + std::cout << e[i].first; + if (i == e.size() - 1) { + if (colors_supported) { + std::cout << col(); + } + } else { + if (colors_supported) { + std::cout << col(); + } + std::cout << " -> "; + } + } + std::cout << std::endl; } - std::cout << col() << std::endl; } class command { - public: +public: using ArgumentList = std::vector>; - using Function = std::function; + using Function = std::function; class sub { - public: - sub(const std::string& _short, const std::string& _long, + public: + sub(const std::string &_short, const std::string &_long, const std::string desc, bool req) { m_isrequired = req; m_long = _long; @@ -117,11 +139,11 @@ class command { ~sub() = default; bool IsRequired() const { return m_isrequired; } - const std::string& GetShort() const { return m_short; } - const std::string& GetLong() const { return m_long; } - const std::string& GetDesc() const { return m_desc; } + const std::string &GetShort() const { return m_short; } + const std::string &GetLong() const { return m_long; } + const std::string &GetDesc() const { return m_desc; } - private: + private: bool m_isrequired; std::string m_long; std::string m_short; @@ -133,12 +155,12 @@ class command { } ~command() = default; - command& AddSubEntry(const sub& sub) { + command &AddSubEntry(const sub &sub) { m_sub.push_back(sub); return *this; } - command& SetFunction(Function fun) { + command &SetFunction(Function fun) { m_fun = fun; return *this; } @@ -146,7 +168,7 @@ class command { Function Func() const { return m_fun; } static std::string GetArg(ArgumentList list, std::string arg) { - for (const auto& it : list) { + for (const auto &it : list) { if (it.first == arg) { return it.second; } @@ -154,35 +176,36 @@ class command { return ""; } - const std::string& GetName() const { return m_name; } - const std::string& GetDesc() const { return m_desc; } - const std::vector& GetArgs() const { return m_sub; } + const std::string &GetName() const { return m_name; } + const std::string &GetDesc() const { return m_desc; } + const std::vector &GetArgs() const { return m_sub; } - private: +private: std::string m_name; std::string m_desc; std::vector m_sub; Function m_fun; }; class arg_mgr { - public: +public: arg_mgr() {} - arg_mgr(char** args, int argc) { Parse(args, argc); } - arg_mgr(int argc, char** args) { Parse(args, argc); } + arg_mgr(char **args, int argc) { Parse(args, argc); } + arg_mgr(int argc, char **args) { Parse(args, argc); } ~arg_mgr() = default; - void SetAppInfo(const std::string& name, const std::string& ver) { + void SetAppInfo(const std::string &name, const std::string &ver) { app_name = name; app_version = ver; } - bool FindString(const std::vector& l, std::string w) { + bool FindShort(const std::vector &l, std::string w) { auto tf = "-" + w; return (std::find(l.begin(), l.end(), tf) != l.end()); } std::string GetArg(std::string w, std::string def = "") { - if (!FindString(m_args, w)) return def; + if (!FindShort(m_args, w)) + return def; for (size_t i = 0; i < m_args.size() - 1; i++) { if (m_args[i] == std::string("-" + w)) { return m_args[i + 1]; @@ -190,13 +213,13 @@ class arg_mgr { } return def; } - void Parse(char** args, int argc) { + void Parse(char **args, int argc) { for (int i = 0; i < argc; i++) { m_args.push_back(args[i]); } } - void PrintHelp(const std::string& cmd = "") { + void PrintHelp(const std::string &cmd = "") { if (!app_name.empty() && !app_version.empty()) { PrintFancy({ std::make_pair(app_name, col(20, 20, 20)), @@ -208,18 +231,27 @@ class arg_mgr { std::make_pair(std::string(__TIMESTAMP__), col(70, 70, 70)), }); if (!cmd.empty()) { - for (const auto& c : m_commands) { + for (const auto &c : m_commands) { if (c.GetName() == cmd) { PrintFancy({std::make_pair(c.GetName(), col(10, 10, 10)), std::make_pair("Help", col(30, 30, 30))}); - for (const auto& it : c.GetArgs()) { - PrintFancy({ - std::make_pair("-" + it.GetShort(), col(30, 30, 30)), - std::make_pair("--" + it.GetLong(), col(50, 50, 50)), - std::make_pair(it.GetDesc(), col(70, 70, 70)), - std::make_pair(it.IsRequired() ? "true" : "false", - col(90, 90, 90)), - }); + for (const auto &it : c.GetArgs()) { + if (!it.GetShort().empty()) { + PrintFancy({ + std::make_pair("--" + it.GetLong(), col(30, 30, 30)), + std::make_pair("-" + it.GetShort(), col(50, 50, 50)), + std::make_pair(it.GetDesc(), col(70, 70, 70)), + std::make_pair(it.IsRequired() ? "true" : "false", + col(90, 90, 90)), + }); + } else { + PrintFancy({ + std::make_pair("--" + it.GetLong(), col(30, 30, 30)), + std::make_pair(it.GetDesc(), col(50, 50, 50)), + std::make_pair(it.IsRequired() ? "true" : "false", + col(70, 70, 70)), + }); + } } return; } @@ -236,17 +268,23 @@ class arg_mgr { void Execute() { command::ArgumentList arglist; - for (const auto& c : m_commands) { + for (const auto &c : m_commands) { if (c.GetName() == m_args[1]) { - for (const auto& j : c.GetArgs()) { - if (!FindString(m_args, j.GetShort())) { + for (const auto &j : c.GetArgs()) { + bool ishort = FindShort(m_args, j.GetShort()); + bool ilong = FindShort(m_args, "-" + j.GetLong()); + if (!ishort && !ilong) { if (j.IsRequired()) { PrintHelp(c.GetName()); return; } + } else if (ishort && ilong) { + PrintHelp(c.GetName()); + return; } else { - arglist.push_back( - std::make_pair(j.GetShort(), GetArg(j.GetShort()))); + arglist.push_back(std::make_pair( + j.GetLong(), + GetArg((ishort ? j.GetShort() : ("-" + j.GetLong()))))); } } c.Func()(arglist); @@ -258,10 +296,10 @@ class arg_mgr { void AddCommand(command cmd) { m_commands.push_back(cmd); } - private: +private: std::string app_name; std::string app_version; std::vector m_args; std::vector m_commands; }; -} // namespace cf7 \ No newline at end of file +} // namespace cf7 \ No newline at end of file