parse groups

This commit is contained in:
Jarryd Beck 2014-10-14 16:59:54 +11:00
parent 239525bcf4
commit 1d9ae57a92
3 changed files with 48 additions and 15 deletions

View File

@ -363,7 +363,7 @@ Options::add_option
//add the help details
auto& options = m_help[group];
options.push_back(HelpDetails{s, l, desc, value->has_arg()});
options.options.push_back(HelpOptionDetails{s, l, desc, value->has_arg()});
}
void
@ -382,11 +382,11 @@ Options::add_one_option
}
std::string
Options::help() const
Options::help_one_group(const std::string& g) const
{
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
auto group = m_help.find("");
auto group = m_help.find(g);
if (group == m_help.end())
{
return "";
@ -396,10 +396,14 @@ Options::help() const
size_t longest = 0;
std::string result = "Usage:\n " + m_program + " [OPTION...] "
+ m_help_string + "\n\n";
std::string result;
if (!g.empty())
{
result += " " + g + " options:\n\n";
}
for (const auto& o : group->second)
for (const auto& o : group->second.options)
{
auto s = format_option(o.s, o.l, o.has_arg);
longest = std::max(longest, s.size());
@ -412,7 +416,7 @@ Options::help() const
int allowed = 76 - longest - OPTION_DESC_GAP;
auto fiter = format.begin();
for (const auto& o : group->second)
for (const auto& o : group->second.options)
{
auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed);
@ -436,4 +440,18 @@ Options::help() const
return result;
}
std::string
Options::help(const std::vector<std::string>& groups) const
{
std::string result = "Usage:\n " + m_program + " [OPTION...] "
+ m_help_string + "\n\n";
for (const auto& g : groups)
{
result += help_one_group(g);
}
return result;
}
}

View File

@ -326,7 +326,7 @@ namespace cxxopts
int m_count;
};
struct HelpDetails
struct HelpOptionDetails
{
std::string s;
std::string l;
@ -334,6 +334,13 @@ namespace cxxopts
bool has_arg;
};
struct HelpGroupDetails
{
std::string name;
std::string description;
std::vector<HelpOptionDetails> options;
};
class Options
{
public:
@ -390,7 +397,7 @@ namespace cxxopts
parse_positional(std::string option);
std::string
help() const;
help(const std::vector<std::string>& groups = {""}) const;
private:
@ -425,6 +432,9 @@ namespace cxxopts
const std::string& name
);
std::string
help_one_group(const std::string& group) const;
std::string m_program;
std::string m_help_string;
@ -432,7 +442,7 @@ namespace cxxopts
std::string m_positional;
//mapping from groups to help options
std::map<std::string, std::vector<HelpDetails>> m_help;
std::map<std::string, HelpGroupDetails> m_help;
};
class OptionAdder

View File

@ -48,10 +48,20 @@ int main(int argc, char* argv[])
("option_that_is_too_long_for_the_help", "A very long option")
;
options.add_options("Group")
("c,compile", "compile")
("d,drop", "drop", cxxopts::value<std::vector<std::string>>());
options.parse_positional("positional");
options.parse(argc, argv);
if (options.count("help"))
{
std::cout << options.help({"", "Group"}) << std::endl;
exit(0);
}
if (apple)
{
std::cout << "Saw option a" << std::endl;
@ -72,11 +82,6 @@ int main(int argc, char* argv[])
}
}
if (options.count("help"))
{
std::cout << options.help() << std::endl;
}
if (options.count("positional"))
{
std::cout << "Positional = " << options["positional"].as<std::string>()