parse groups
This commit is contained in:
parent
239525bcf4
commit
1d9ae57a92
@ -363,7 +363,7 @@ Options::add_option
|
|||||||
|
|
||||||
//add the help details
|
//add the help details
|
||||||
auto& options = m_help[group];
|
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
|
void
|
||||||
@ -382,11 +382,11 @@ Options::add_one_option
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
Options::help() const
|
Options::help_one_group(const std::string& g) const
|
||||||
{
|
{
|
||||||
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
|
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())
|
if (group == m_help.end())
|
||||||
{
|
{
|
||||||
return "";
|
return "";
|
||||||
@ -396,10 +396,14 @@ Options::help() const
|
|||||||
|
|
||||||
size_t longest = 0;
|
size_t longest = 0;
|
||||||
|
|
||||||
std::string result = "Usage:\n " + m_program + " [OPTION...] "
|
std::string result;
|
||||||
+ m_help_string + "\n\n";
|
|
||||||
|
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);
|
auto s = format_option(o.s, o.l, o.has_arg);
|
||||||
longest = std::max(longest, s.size());
|
longest = std::max(longest, s.size());
|
||||||
@ -412,7 +416,7 @@ Options::help() const
|
|||||||
int allowed = 76 - longest - OPTION_DESC_GAP;
|
int allowed = 76 - longest - OPTION_DESC_GAP;
|
||||||
|
|
||||||
auto fiter = format.begin();
|
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);
|
auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed);
|
||||||
|
|
||||||
@ -436,4 +440,18 @@ Options::help() const
|
|||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -326,7 +326,7 @@ namespace cxxopts
|
|||||||
int m_count;
|
int m_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HelpDetails
|
struct HelpOptionDetails
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
std::string l;
|
std::string l;
|
||||||
@ -334,6 +334,13 @@ namespace cxxopts
|
|||||||
bool has_arg;
|
bool has_arg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct HelpGroupDetails
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
std::string description;
|
||||||
|
std::vector<HelpOptionDetails> options;
|
||||||
|
};
|
||||||
|
|
||||||
class Options
|
class Options
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -390,7 +397,7 @@ namespace cxxopts
|
|||||||
parse_positional(std::string option);
|
parse_positional(std::string option);
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
help() const;
|
help(const std::vector<std::string>& groups = {""}) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -425,6 +432,9 @@ namespace cxxopts
|
|||||||
const std::string& name
|
const std::string& name
|
||||||
);
|
);
|
||||||
|
|
||||||
|
std::string
|
||||||
|
help_one_group(const std::string& group) const;
|
||||||
|
|
||||||
std::string m_program;
|
std::string m_program;
|
||||||
std::string m_help_string;
|
std::string m_help_string;
|
||||||
|
|
||||||
@ -432,7 +442,7 @@ namespace cxxopts
|
|||||||
std::string m_positional;
|
std::string m_positional;
|
||||||
|
|
||||||
//mapping from groups to help options
|
//mapping from groups to help options
|
||||||
std::map<std::string, std::vector<HelpDetails>> m_help;
|
std::map<std::string, HelpGroupDetails> m_help;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionAdder
|
class OptionAdder
|
||||||
|
|||||||
@ -48,10 +48,20 @@ int main(int argc, char* argv[])
|
|||||||
("option_that_is_too_long_for_the_help", "A very long option")
|
("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_positional("positional");
|
||||||
|
|
||||||
options.parse(argc, argv);
|
options.parse(argc, argv);
|
||||||
|
|
||||||
|
if (options.count("help"))
|
||||||
|
{
|
||||||
|
std::cout << options.help({"", "Group"}) << std::endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (apple)
|
if (apple)
|
||||||
{
|
{
|
||||||
std::cout << "Saw option ‘a’" << std::endl;
|
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"))
|
if (options.count("positional"))
|
||||||
{
|
{
|
||||||
std::cout << "Positional = " << options["positional"].as<std::string>()
|
std::cout << "Positional = " << options["positional"].as<std::string>()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user