diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index 9ad20a3..0e5d704 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -35,6 +35,38 @@ namespace cxxopts std::basic_regex option_specifier ("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)"); + + std::string + format_option + ( + const std::string& s, + const std::string& l, + bool has_arg + ) + { + std::string result = " "; + + if (s.size() > 0) + { + result += "-" + s + ","; + } + else + { + result += " "; + } + + if (l.size() > 0) + { + result += " --" + l; + } + + if (has_arg) + { + result += " arg"; + } + + return result; + } } OptionAdder @@ -270,6 +302,8 @@ Options::add_option } //add the help details + auto& options = m_help[""]; + options.push_back(HelpDetails{s, l, desc, value->has_arg()}); } void @@ -290,13 +324,29 @@ Options::add_one_option std::string Options::help() const { + typedef std::vector> OptionHelp; + auto group = m_help.find(""); if (group == m_help.end()) { return ""; } - return ""; + OptionHelp format; + + size_t longest = 0; + + std::string result; + + for (const auto& o : group->second) + { + auto s = format_option(o.s, o.l, o.has_arg); + longest = std::max(longest, s.size()); + format.push_back(std::make_pair(s, std::string())); + result += s + "\n"; + } + + return result; } } diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index 0db994e..6a00950 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -286,16 +286,12 @@ namespace cxxopts int m_count; }; - class HelpDetails + struct HelpDetails { - public: - HelpDetails(); - - private: - std::string m_short; - std::string m_long; - std::string m_description; - bool m_arg; + std::string s; + std::string l; + std::string desc; + bool has_arg; }; class Options diff --git a/src/main.cpp b/src/main.cpp index 45b840a..1ab28ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,7 @@ int main(int argc, char* argv[]) ("b,bob", "Bob") ("f,file", "File", cxxopts::value>()) ("positional", "Positional arguments", cxxopts::value()) + ("help", "Print help") ; options.parse_positional("positional"); @@ -66,7 +67,7 @@ int main(int argc, char* argv[]) if (options.count("help")) { - //std::cout << options.print_help(); + std::cout << options.help() << std::endl; } if (options.count("positional"))