Add Color and Design Disable
Add Long args support Set Long Arg as default for GetArg
This commit is contained in:
parent
eb4cd221b0
commit
44905e449f
@ -33,6 +33,8 @@ SOFTWARE.
|
||||
|
||||
// CLI FANCY
|
||||
namespace cf7 {
|
||||
static bool fancy_print = true;
|
||||
static bool colors_supported = true;
|
||||
namespace sym {
|
||||
const std::string arrow = "";
|
||||
}
|
||||
@ -88,6 +90,7 @@ class col {
|
||||
unsigned char m_b = 0;
|
||||
};
|
||||
void PrintFancy(const std::vector<std::pair<std::string, col>> &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);
|
||||
@ -100,6 +103,25 @@ void PrintFancy(const std::vector<std::pair<std::string, col>>& e) {
|
||||
}
|
||||
}
|
||||
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 {
|
||||
public:
|
||||
@ -176,13 +198,14 @@ class arg_mgr {
|
||||
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;
|
||||
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];
|
||||
@ -213,13 +236,22 @@ class arg_mgr {
|
||||
PrintFancy({std::make_pair(c.GetName(), col(10, 10, 10)),
|
||||
std::make_pair("Help", col(30, 30, 30))});
|
||||
for (const auto &it : c.GetArgs()) {
|
||||
if (!it.GetShort().empty()) {
|
||||
PrintFancy({
|
||||
std::make_pair("-" + it.GetShort(), col(30, 30, 30)),
|
||||
std::make_pair("--" + it.GetLong(), col(50, 50, 50)),
|
||||
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;
|
||||
}
|
||||
@ -239,14 +271,20 @@ class arg_mgr {
|
||||
for (const auto &c : m_commands) {
|
||||
if (c.GetName() == m_args[1]) {
|
||||
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()) {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user