Add Color and Design Disable

Add Long args support
Set Long Arg as default for GetArg
This commit is contained in:
tobid7 2024-12-16 12:03:45 +00:00
parent eb4cd221b0
commit 44905e449f

View File

@ -33,6 +33,8 @@ SOFTWARE.
// CLI FANCY // CLI FANCY
namespace cf7 { namespace cf7 {
static bool fancy_print = true;
static bool colors_supported = true;
namespace sym { namespace sym {
const std::string arrow = ""; const std::string arrow = "";
} }
@ -88,6 +90,7 @@ class col {
unsigned char m_b = 0; unsigned char m_b = 0;
}; };
void PrintFancy(const std::vector<std::pair<std::string, col>> &e) { void PrintFancy(const std::vector<std::pair<std::string, col>> &e) {
if (fancy_print && colors_supported) {
std::cout << col(); std::cout << col();
for (int i = 0; i < e.size(); i++) { for (int i = 0; i < e.size(); i++) {
std::cout << e[i].second.GetAs(false) << col(true, 255, 255, 255); std::cout << e[i].second.GetAs(false) << col(true, 255, 255, 255);
@ -100,6 +103,25 @@ void PrintFancy(const std::vector<std::pair<std::string, col>>& e) {
} }
} }
std::cout << col() << std::endl; 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;
}
} }
class command { class command {
public: public:
@ -176,13 +198,14 @@ class arg_mgr {
app_version = ver; app_version = ver;
} }
bool FindString(const std::vector<std::string>& l, std::string w) { bool FindShort(const std::vector<std::string> &l, std::string w) {
auto tf = "-" + w; auto tf = "-" + w;
return (std::find(l.begin(), l.end(), tf) != l.end()); return (std::find(l.begin(), l.end(), tf) != l.end());
} }
std::string GetArg(std::string w, std::string def = "") { 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++) { for (size_t i = 0; i < m_args.size() - 1; i++) {
if (m_args[i] == std::string("-" + w)) { if (m_args[i] == std::string("-" + w)) {
return m_args[i + 1]; return m_args[i + 1];
@ -213,13 +236,22 @@ class arg_mgr {
PrintFancy({std::make_pair(c.GetName(), col(10, 10, 10)), PrintFancy({std::make_pair(c.GetName(), col(10, 10, 10)),
std::make_pair("Help", col(30, 30, 30))}); std::make_pair("Help", col(30, 30, 30))});
for (const auto &it : c.GetArgs()) { for (const auto &it : c.GetArgs()) {
if (!it.GetShort().empty()) {
PrintFancy({ PrintFancy({
std::make_pair("-" + it.GetShort(), col(30, 30, 30)), std::make_pair("--" + it.GetLong(), col(30, 30, 30)),
std::make_pair("--" + it.GetLong(), col(50, 50, 50)), std::make_pair("-" + it.GetShort(), col(50, 50, 50)),
std::make_pair(it.GetDesc(), col(70, 70, 70)), std::make_pair(it.GetDesc(), col(70, 70, 70)),
std::make_pair(it.IsRequired() ? "true" : "false", std::make_pair(it.IsRequired() ? "true" : "false",
col(90, 90, 90)), 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; return;
} }
@ -239,14 +271,20 @@ class arg_mgr {
for (const auto &c : m_commands) { for (const auto &c : m_commands) {
if (c.GetName() == m_args[1]) { if (c.GetName() == m_args[1]) {
for (const auto &j : c.GetArgs()) { for (const auto &j : c.GetArgs()) {
if (!FindString(m_args, j.GetShort())) { bool ishort = FindShort(m_args, j.GetShort());
bool ilong = FindShort(m_args, "-" + j.GetLong());
if (!ishort && !ilong) {
if (j.IsRequired()) { if (j.IsRequired()) {
PrintHelp(c.GetName()); PrintHelp(c.GetName());
return; return;
} }
} else if (ishort && ilong) {
PrintHelp(c.GetName());
return;
} else { } else {
arglist.push_back( arglist.push_back(std::make_pair(
std::make_pair(j.GetShort(), GetArg(j.GetShort()))); j.GetLong(),
GetArg((ishort ? j.GetShort() : ("-" + j.GetLong())))));
} }
} }
c.Func()(arglist); c.Func()(arglist);