fix very stupid out of range error
This commit is contained in:
@@ -39,7 +39,7 @@ 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;
|
||||
@@ -82,7 +82,7 @@ public:
|
||||
return buf;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
bool m_isres = true;
|
||||
bool m_fg = false;
|
||||
unsigned char m_r = 0;
|
||||
@@ -124,11 +124,11 @@ void PrintFancy(const std::vector<std::pair<std::string, col>> &e) {
|
||||
}
|
||||
}
|
||||
class command {
|
||||
public:
|
||||
public:
|
||||
using ArgumentList = std::vector<std::pair<std::string, std::string>>;
|
||||
using Function = std::function<void(const ArgumentList &)>;
|
||||
class sub {
|
||||
public:
|
||||
public:
|
||||
sub(const std::string &_short, const std::string &_long,
|
||||
const std::string desc, bool req) {
|
||||
m_isrequired = req;
|
||||
@@ -143,7 +143,7 @@ public:
|
||||
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;
|
||||
@@ -180,14 +180,14 @@ public:
|
||||
const std::string &GetDesc() const { return m_desc; }
|
||||
const std::vector<sub> &GetArgs() const { return m_sub; }
|
||||
|
||||
private:
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_desc;
|
||||
std::vector<sub> 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); }
|
||||
@@ -204,8 +204,7 @@ public:
|
||||
}
|
||||
|
||||
std::string GetArg(std::string w, std::string def = "") {
|
||||
if (!FindShort(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];
|
||||
@@ -268,27 +267,29 @@ public:
|
||||
|
||||
void Execute() {
|
||||
command::ArgumentList arglist;
|
||||
for (const auto &c : m_commands) {
|
||||
if (c.GetName() == m_args[1]) {
|
||||
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()) {
|
||||
if (m_args.size() > 1) {
|
||||
for (const auto &c : m_commands) {
|
||||
if (c.GetName() == m_args[1]) {
|
||||
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.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("");
|
||||
@@ -296,10 +297,10 @@ public:
|
||||
|
||||
void AddCommand(command cmd) { m_commands.push_back(cmd); }
|
||||
|
||||
private:
|
||||
private:
|
||||
std::string app_name;
|
||||
std::string app_version;
|
||||
std::vector<std::string> m_args;
|
||||
std::vector<command> m_commands;
|
||||
};
|
||||
} // namespace cf7
|
||||
} // namespace cf7
|
||||
Reference in New Issue
Block a user