From 2ba8720b0919e760ea6842c514834cda8e61615d Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 29 Dec 2025 14:08:59 +0100 Subject: [PATCH] fix very stupid out of range error --- include/cli-fancy.hpp | 55 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/include/cli-fancy.hpp b/include/cli-fancy.hpp index e3ae1ab..cc9af4f 100644 --- a/include/cli-fancy.hpp +++ b/include/cli-fancy.hpp @@ -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> &e) { } } class command { -public: + public: using ArgumentList = std::vector>; using Function = std::function; 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 &GetArgs() const { return m_sub; } -private: + private: std::string m_name; std::string m_desc; std::vector 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 m_args; std::vector m_commands; }; -} // namespace cf7 \ No newline at end of file +} // namespace cf7 \ No newline at end of file