fix very stupid out of range error
This commit is contained in:
@@ -39,7 +39,7 @@ 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;
|
||||||
@@ -82,7 +82,7 @@ public:
|
|||||||
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;
|
||||||
@@ -124,11 +124,11 @@ void PrintFancy(const std::vector<std::pair<std::string, col>> &e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
@@ -143,7 +143,7 @@ public:
|
|||||||
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;
|
||||||
std::string m_long;
|
std::string m_long;
|
||||||
std::string m_short;
|
std::string m_short;
|
||||||
@@ -180,14 +180,14 @@ public:
|
|||||||
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); }
|
||||||
@@ -204,8 +204,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string GetArg(std::string w, std::string def = "") {
|
std::string GetArg(std::string w, std::string def = "") {
|
||||||
if (!FindShort(m_args, w))
|
if (!FindShort(m_args, w)) return def;
|
||||||
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];
|
||||||
@@ -268,27 +267,29 @@ public:
|
|||||||
|
|
||||||
void Execute() {
|
void Execute() {
|
||||||
command::ArgumentList arglist;
|
command::ArgumentList arglist;
|
||||||
for (const auto &c : m_commands) {
|
if (m_args.size() > 1) {
|
||||||
if (c.GetName() == m_args[1]) {
|
for (const auto &c : m_commands) {
|
||||||
for (const auto &j : c.GetArgs()) {
|
if (c.GetName() == m_args[1]) {
|
||||||
bool ishort = FindShort(m_args, j.GetShort());
|
for (const auto &j : c.GetArgs()) {
|
||||||
bool ilong = FindShort(m_args, "-" + j.GetLong());
|
bool ishort = FindShort(m_args, j.GetShort());
|
||||||
if (!ishort && !ilong) {
|
bool ilong = FindShort(m_args, "-" + j.GetLong());
|
||||||
if (j.IsRequired()) {
|
if (!ishort && !ilong) {
|
||||||
|
if (j.IsRequired()) {
|
||||||
|
PrintHelp(c.GetName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (ishort && ilong) {
|
||||||
PrintHelp(c.GetName());
|
PrintHelp(c.GetName());
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
arglist.push_back(std::make_pair(
|
||||||
|
j.GetLong(),
|
||||||
|
GetArg((ishort ? j.GetShort() : ("-" + j.GetLong())))));
|
||||||
}
|
}
|
||||||
} else if (ishort && ilong) {
|
|
||||||
PrintHelp(c.GetName());
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
arglist.push_back(std::make_pair(
|
|
||||||
j.GetLong(),
|
|
||||||
GetArg((ishort ? j.GetShort() : ("-" + j.GetLong())))));
|
|
||||||
}
|
}
|
||||||
|
c.Func()(arglist);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
c.Func()(arglist);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PrintHelp("");
|
PrintHelp("");
|
||||||
@@ -296,10 +297,10 @@ public:
|
|||||||
|
|
||||||
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;
|
||||||
std::vector<command> m_commands;
|
std::vector<command> m_commands;
|
||||||
};
|
};
|
||||||
} // namespace cf7
|
} // namespace cf7
|
||||||
Reference in New Issue
Block a user