Showing list of available commands in help message.

This commit is contained in:
Patrick Lühne 2017-10-14 01:20:26 +02:00
parent d20b30d34d
commit 0d5245b48b
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
12 changed files with 216 additions and 108 deletions

View File

@ -14,12 +14,30 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class... OptionGroups>
template<class Derived, class... OptionGroups>
class Command
{
public:
void printHelp()
{
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 << m_options.help(optionGroupNames) << std::endl;
}
protected:
Command(cxxopts::Options &&options)
: m_options{options}
Command()
: m_options(std::string("plasp ") + Derived::Name, std::string(Derived::Description) + ".")
{
forEach(m_optionGroups,
[&](auto &optionGroup)
@ -39,24 +57,7 @@ class Command
});
}
void printHelp()
{
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 << m_options.help(optionGroupNames) << std::endl;
}
void printVersion()
static void printVersion()
{
std::cout << Version << std::endl;
}

View File

@ -0,0 +1,65 @@
#ifndef __PLASP_APP__COMMAND_TYPE_H
#define __PLASP_APP__COMMAND_TYPE_H
#include <map>
#include <string>
#include <plasp-app/commands/CommandHelp.h>
#include <plasp-app/commands/CommandNormalize.h>
#include <plasp-app/commands/CommandTranslate.h>
#include <plasp-app/commands/CommandVersion.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command Type
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class CommandType
{
Help,
Version,
CheckSyntax,
Requirements,
PrettyPrint,
Normalize,
Translate
};
////////////////////////////////////////////////////////////////////////////////////////////////////
static const std::map<std::string, CommandType> commandNames =
{
{"help", CommandType::Help},
{"-h", CommandType::Help},
{"--help", CommandType::Help},
{"version", CommandType::Version},
{"-v", CommandType::Version},
{"--version", CommandType::Version},
{"check-syntax", CommandType::CheckSyntax},
{"requirements", CommandType::Requirements},
{"pretty-print", CommandType::PrettyPrint},
{"normalize", CommandType::Normalize},
{"translate", CommandType::Translate},
};
////////////////////////////////////////////////////////////////////////////////////////////////////
const auto parseCommandType =
[](const std::string &commandString)
{
const auto matchingCommand = commandNames.find(commandString);
if (matchingCommand == commandNames.cend())
throw std::runtime_error(std::string("") + commandString + "” is not a plasp command");
return matchingCommand->second;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
using AvailableCommands = std::tuple<CommandTranslate, CommandNormalize, CommandHelp, CommandVersion>;
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@ -0,0 +1,25 @@
#ifndef __PLASP_APP__COMMANDS__COMMAND_HELP_H
#define __PLASP_APP__COMMANDS__COMMAND_HELP_H
#include <plasp-app/Command.h>
#include <plasp-app/OptionGroups.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command Help
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CommandHelp : public Command<CommandHelp>
{
public:
static constexpr auto Name = "help";
static constexpr auto Description = "Display this help message";
public:
int run(int argc, char **argv);
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@ -10,11 +10,13 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CommandNormalize : public Command<OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
class CommandNormalize : public Command<CommandNormalize, OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
{
public:
CommandNormalize();
static constexpr auto Name = "normalize";
static constexpr auto Description = "Normalize PDDL to plasps custom PDDL format";
public:
int run(int argc, char **argv);
};

View File

@ -10,11 +10,13 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CommandTranslate : public Command<OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
class CommandTranslate : public Command<CommandTranslate, OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
{
public:
CommandTranslate();
static constexpr auto Name = "translate";
static constexpr auto Description = "Translate PDDL and SAS to ASP";
public:
int run(int argc, char **argv);
};

View File

@ -10,11 +10,13 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CommandVersion : public Command<>
class CommandVersion : public Command<CommandVersion>
{
public:
CommandVersion();
static constexpr auto Name = "version";
static constexpr auto Description = "Display version information";
public:
int run(int argc, char **argv);
};

View File

@ -83,6 +83,7 @@ void OptionGroupParser::addTo(cxxopts::Options &options)
("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"));
options.parse_positional("input");
options.positional_help("[<input_file...>]");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,79 @@
#include <plasp-app/commands/CommandHelp.h>
#include <colorlog/Logger.h>
#include <plasp-app/CommandType.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command Help
//
////////////////////////////////////////////////////////////////////////////////////////////////////
int CommandHelp::run(int argc, char **argv)
{
colorlog::Logger logger;
if (argc > 1)
{
try
{
switch (parseCommandType(argv[1]))
{
case CommandType::Help:
case CommandType::Version:
break;
case CommandType::Normalize:
CommandNormalize().printHelp();
return EXIT_SUCCESS;
case CommandType::Translate:
CommandTranslate().printHelp();
return EXIT_SUCCESS;
default:
logger.log(colorlog::Priority::Error, std::string("command “") + argv[1] + "” not yet implemented");
exit(EXIT_FAILURE);
}
}
catch (std::exception &exception)
{
}
}
std::cout
<< "ASP planning tools for PDDL." << std::endl
<< "Usage:" << std::endl
<< " plasp <command> [<arguments>]" << std::endl << std::endl
<< " available commands:" << std::endl;
AvailableCommands availableCommands;
size_t lengthOfLongestCommandName = 0;
forEach(availableCommands,
[&](const auto &command)
{
lengthOfLongestCommandName = std::max(lengthOfLongestCommandName, std::strlen(command.Name));
});
const auto printSpaces =
[](const auto numberOfSpaces)
{
for (auto i = static_cast<decltype(numberOfSpaces)>(0); i < numberOfSpaces; i++)
std::cout << " ";
};
forEach(availableCommands,
[&](const auto &command)
{
const auto lengthOfCommandName = std::strlen(command.Name);
std::cout << " " << command.Name;
printSpaces(lengthOfLongestCommandName - lengthOfCommandName + 2);
std::cout << command.Description << std::endl;
});
return EXIT_SUCCESS;
}

View File

@ -27,13 +27,6 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
CommandNormalize::CommandNormalize()
: Command(cxxopts::Options("plasp normalize", "Normalize PDDL to plasps custom PDDL format."))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int CommandNormalize::run(int argc, char **argv)
{
parseOptions(argc, argv);

View File

@ -30,13 +30,6 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
CommandTranslate::CommandTranslate()
: Command(cxxopts::Options("plasp translate", "Translate PDDL to ASP."))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int CommandTranslate::run(int argc, char **argv)
{
parseOptions(argc, argv);

View File

@ -6,13 +6,6 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
CommandVersion::CommandVersion()
: Command(cxxopts::Options("plasp version", "Display version information."))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int CommandVersion::run(int, char **)
{
printVersion();

View File

@ -8,10 +8,8 @@
#include <colorlog/Priority.h>
#include <plasp-app/Command.h>
#include <plasp-app/CommandType.h>
#include <plasp-app/Version.h>
#include <plasp-app/commands/CommandNormalize.h>
#include <plasp-app/commands/CommandTranslate.h>
#include <plasp-app/commands/CommandVersion.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -19,65 +17,14 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class CommandType
{
Help,
Version,
CheckSyntax,
Requirements,
PrettyPrint,
Normalize,
Translate
};
////////////////////////////////////////////////////////////////////////////////////////////////////
static const std::map<std::string, CommandType> commandNames =
{
{"help", CommandType::Help},
{"-h", CommandType::Help},
{"--help", CommandType::Help},
{"version", CommandType::Version},
{"-v", CommandType::Version},
{"--version", CommandType::Version},
{"check-syntax", CommandType::CheckSyntax},
{"requirements", CommandType::Requirements},
{"pretty-print", CommandType::PrettyPrint},
{"normalize", CommandType::Normalize},
{"translate", CommandType::Translate},
};
////////////////////////////////////////////////////////////////////////////////////////////////////
const auto parseCommandType =
[](const std::string &commandString)
{
const auto matchingCommand = commandNames.find(commandString);
if (matchingCommand == commandNames.cend())
throw std::runtime_error(std::string("") + commandString + "” is not a plasp command");
return matchingCommand->second;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
const auto printHelp =
[&]()
{
// TODO: add list of available commands
std::cout
<< "ASP planning tools for PDDL." << std::endl
<< "Usage: plasp <command> [<arguments>]" << std::endl;
};
colorlog::Logger logger;
if (argc < 2)
{
printHelp();
CommandHelp().run(argc - 1, &argv[1]);
return EXIT_FAILURE;
}
@ -86,16 +33,19 @@ int main(int argc, char **argv)
switch (parseCommandType(argv[1]))
{
case CommandType::Help:
printHelp();
return EXIT_SUCCESS;
return CommandHelp().run(argc - 1, &argv[1]);
case CommandType::Version:
return CommandVersion().run(argc - 1, &argv[1]);
return EXIT_SUCCESS;
case CommandType::Normalize:
return CommandNormalize().run(argc - 1, &argv[1]);
case CommandType::Translate:
return CommandTranslate().run(argc - 1, &argv[1]);
default:
logger.log(colorlog::Priority::Error, std::string("command “") + argv[1] + "” not yet implemented");
exit(EXIT_FAILURE);
}
}
@ -103,7 +53,9 @@ int main(int argc, char **argv)
{
logger.log(colorlog::Priority::Error, exception.what());
std::cout << std::endl;
printHelp();
CommandHelp().run(argc - 1, &argv[1]);
return EXIT_FAILURE;
}