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,11 +33,13 @@ 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 = "";
|
||||||
}
|
}
|
||||||
class col {
|
class col {
|
||||||
public:
|
public:
|
||||||
col() { m_isres = true; }
|
col() { m_isres = true; }
|
||||||
col(bool fg, unsigned char r, unsigned char g, unsigned char b) {
|
col(bool fg, unsigned char r, unsigned char g, unsigned char b) {
|
||||||
m_isres = false;
|
m_isres = false;
|
||||||
@ -72,7 +74,7 @@ class col {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator std::string() const { return Get(); }
|
operator std::string() const { return Get(); }
|
||||||
operator const char*() const {
|
operator const char *() const {
|
||||||
/// CREATE DYNAMIC BUFFER TO ALWAYS RETURN STABLE CONST CHAR
|
/// CREATE DYNAMIC BUFFER TO ALWAYS RETURN STABLE CONST CHAR
|
||||||
static char buf[32];
|
static char buf[32];
|
||||||
strcpy(buf, Get().c_str());
|
strcpy(buf, Get().c_str());
|
||||||
@ -80,14 +82,15 @@ class col {
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_isres = true;
|
bool m_isres = true;
|
||||||
bool m_fg = false;
|
bool m_fg = false;
|
||||||
unsigned char m_r = 0;
|
unsigned char m_r = 0;
|
||||||
unsigned char m_g = 0;
|
unsigned char m_g = 0;
|
||||||
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,14 +103,33 @@ 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:
|
||||||
using ArgumentList = std::vector<std::pair<std::string, std::string>>;
|
using ArgumentList = std::vector<std::pair<std::string, std::string>>;
|
||||||
using Function = std::function<void(const ArgumentList&)>;
|
using Function = std::function<void(const ArgumentList &)>;
|
||||||
class sub {
|
class sub {
|
||||||
public:
|
public:
|
||||||
sub(const std::string& _short, const std::string& _long,
|
sub(const std::string &_short, const std::string &_long,
|
||||||
const std::string desc, bool req) {
|
const std::string desc, bool req) {
|
||||||
m_isrequired = req;
|
m_isrequired = req;
|
||||||
m_long = _long;
|
m_long = _long;
|
||||||
@ -117,9 +139,9 @@ class command {
|
|||||||
~sub() = default;
|
~sub() = default;
|
||||||
|
|
||||||
bool IsRequired() const { return m_isrequired; }
|
bool IsRequired() const { return m_isrequired; }
|
||||||
const std::string& GetShort() const { return m_short; }
|
const std::string &GetShort() const { return m_short; }
|
||||||
const std::string& GetLong() const { return m_long; }
|
const std::string &GetLong() const { return m_long; }
|
||||||
const std::string& GetDesc() const { return m_desc; }
|
const std::string &GetDesc() const { return m_desc; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_isrequired;
|
bool m_isrequired;
|
||||||
@ -133,12 +155,12 @@ class command {
|
|||||||
}
|
}
|
||||||
~command() = default;
|
~command() = default;
|
||||||
|
|
||||||
command& AddSubEntry(const sub& sub) {
|
command &AddSubEntry(const sub &sub) {
|
||||||
m_sub.push_back(sub);
|
m_sub.push_back(sub);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
command& SetFunction(Function fun) {
|
command &SetFunction(Function fun) {
|
||||||
m_fun = fun;
|
m_fun = fun;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -146,7 +168,7 @@ class command {
|
|||||||
Function Func() const { return m_fun; }
|
Function Func() const { return m_fun; }
|
||||||
|
|
||||||
static std::string GetArg(ArgumentList list, std::string arg) {
|
static std::string GetArg(ArgumentList list, std::string arg) {
|
||||||
for (const auto& it : list) {
|
for (const auto &it : list) {
|
||||||
if (it.first == arg) {
|
if (it.first == arg) {
|
||||||
return it.second;
|
return it.second;
|
||||||
}
|
}
|
||||||
@ -154,35 +176,36 @@ class command {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetName() const { return m_name; }
|
const std::string &GetName() const { return m_name; }
|
||||||
const std::string& GetDesc() const { return m_desc; }
|
const std::string &GetDesc() const { return m_desc; }
|
||||||
const std::vector<sub>& GetArgs() const { return m_sub; }
|
const std::vector<sub> &GetArgs() const { return m_sub; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::string m_desc;
|
std::string m_desc;
|
||||||
std::vector<sub> m_sub;
|
std::vector<sub> m_sub;
|
||||||
Function m_fun;
|
Function m_fun;
|
||||||
};
|
};
|
||||||
class arg_mgr {
|
class arg_mgr {
|
||||||
public:
|
public:
|
||||||
arg_mgr() {}
|
arg_mgr() {}
|
||||||
arg_mgr(char** args, int argc) { Parse(args, argc); }
|
arg_mgr(char **args, int argc) { Parse(args, argc); }
|
||||||
arg_mgr(int argc, char** args) { Parse(args, argc); }
|
arg_mgr(int argc, char **args) { Parse(args, argc); }
|
||||||
~arg_mgr() = default;
|
~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_name = name;
|
||||||
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];
|
||||||
@ -190,13 +213,13 @@ class arg_mgr {
|
|||||||
}
|
}
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
void Parse(char** args, int argc) {
|
void Parse(char **args, int argc) {
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
m_args.push_back(args[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()) {
|
if (!app_name.empty() && !app_version.empty()) {
|
||||||
PrintFancy({
|
PrintFancy({
|
||||||
std::make_pair(app_name, col(20, 20, 20)),
|
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)),
|
std::make_pair(std::string(__TIMESTAMP__), col(70, 70, 70)),
|
||||||
});
|
});
|
||||||
if (!cmd.empty()) {
|
if (!cmd.empty()) {
|
||||||
for (const auto& c : m_commands) {
|
for (const auto &c : m_commands) {
|
||||||
if (c.GetName() == cmd) {
|
if (c.GetName() == cmd) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -236,17 +268,23 @@ class arg_mgr {
|
|||||||
|
|
||||||
void Execute() {
|
void Execute() {
|
||||||
command::ArgumentList arglist;
|
command::ArgumentList arglist;
|
||||||
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);
|
||||||
@ -258,7 +296,7 @@ class arg_mgr {
|
|||||||
|
|
||||||
void AddCommand(command cmd) { m_commands.push_back(cmd); }
|
void AddCommand(command cmd) { m_commands.push_back(cmd); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string app_name;
|
std::string app_name;
|
||||||
std::string app_version;
|
std::string app_version;
|
||||||
std::vector<std::string> m_args;
|
std::vector<std::string> m_args;
|
||||||
|
Loading…
Reference in New Issue
Block a user