Refactored printing help messages in commands.

This commit is contained in:
Patrick Lühne 2017-10-13 18:09:15 +02:00
parent db7ea6f2fe
commit abdc4e31dc
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
4 changed files with 28 additions and 13 deletions

View File

@ -35,6 +35,23 @@ class Command
});
}
void printHelp(cxxopts::Options &options)
{
const auto numberOfOptionGroups = std::tuple_size<std::decay_t<decltype(m_optionGroups)>>();
std::vector<std::string> optionGroupNames;
optionGroupNames.reserve(numberOfOptionGroups + 1);
optionGroupNames.emplace_back("");
forEach(m_optionGroups,
[&](auto &optionGroup)
{
optionGroupNames.emplace_back(optionGroup.Name);
});
std::cout << options.help(optionGroupNames) << std::endl;
}
std::tuple<OptionGroups...> m_optionGroups;
};

View File

@ -27,6 +27,8 @@ class OptionException : public pddl::Exception
struct OptionGroupBasic
{
static constexpr const auto Name = "basic";
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);
@ -39,6 +41,8 @@ struct OptionGroupBasic
struct OptionGroupOutput
{
static constexpr const auto Name = "output";
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);
@ -50,6 +54,8 @@ struct OptionGroupOutput
struct OptionGroupParser
{
static constexpr const auto Name = "parser";
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);

View File

@ -8,7 +8,7 @@
void OptionGroupBasic::addTo(cxxopts::Options &options)
{
options.add_options("basic")
options.add_options(Name)
("h,help", "Display this help message")
("v,version", "Display version information")
("warnings-as-errors", "Treat warnings as errors");
@ -27,7 +27,7 @@ void OptionGroupBasic::parse(cxxopts::Options &options)
void OptionGroupOutput::addTo(cxxopts::Options &options)
{
options.add_options("output")
options.add_options(Name)
("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
("p,log-priority", "Log messages starting from this priority (debug, info, warning, error)", cxxopts::value<std::string>()->default_value("info"));
}
@ -63,7 +63,7 @@ void OptionGroupOutput::parse(cxxopts::Options &options)
void OptionGroupParser::addTo(cxxopts::Options &options)
{
options.add_options("parser")
options.add_options(Name)
("i,input", "Input files (in PDDL or SAS format)", cxxopts::value<std::vector<std::string>>())
("parsing-mode", "Parsing mode (strict, compatibility)", cxxopts::value<std::string>()->default_value("strict"))
("l,language", "Input language (pddl, sas, auto)", cxxopts::value<std::string>()->default_value("auto"));

View File

@ -37,15 +37,7 @@ int CommandTranslate::run(int argc, char **argv)
cxxopts::Options options("plasp translate", "Translate PDDL to ASP.");
addOptionGroupsTo(options);
const auto printHelp =
[&]()
{
std::cout << options.help({"", "basic", "output", "parser"});
};
options.parse(argc, argv);
parseOptionGroups(options);
const auto &basicOptions = std::get<OptionGroupBasic>(m_optionGroups);
@ -54,7 +46,7 @@ int CommandTranslate::run(int argc, char **argv)
if (basicOptions.help)
{
printHelp();
printHelp(options);
return EXIT_SUCCESS;
}
@ -110,7 +102,7 @@ int CommandTranslate::run(int argc, char **argv)
{
logger.log(colorlog::Priority::Error, "unknown input language");
std::cout << std::endl;
printHelp();
printHelp(options);
return EXIT_FAILURE;
}