1 #ifndef SIMPT_MODE_MANAGER_H_
2 #define SIMPT_MODE_MANAGER_H_
29 #include <tclap/CmdLine.h>
30 #include <boost/functional/value_factory.hpp>
41 using Function = std::function<int(int argc, char** argv)>;
46 template<
typename executable_type>
56 const std::string msg = std::string(
"Invalid selection:\n");
57 throw std::logic_error(msg + application);
59 return executable_type::modes.Get(application)();
65 static bool IsValid(
const std::string& selection)
67 return (selection !=
"") && executable_type::modes.IsValid(selection);
73 static std::vector<std::string>
List()
75 auto list = executable_type::modes.List();
76 return std::vector<std::string> (std::begin(list),std::end(list) );
84 return executable_type::default_mode;
87 static int main(
int argc,
char** argv)
90 using namespace TCLAP;
92 int exit_status = EXIT_SUCCESS;
95 CmdLine cmd(executable_type::application_name,
' ',
"",
false);
96 vector<string> apps(
List());
97 ValuesConstraint<string> allowedApps(apps);
98 ValueArg<string> application_Arg(
"m",
"mode",
"The application mode",
false,
"compound", &allowedApps, cmd);
99 SwitchArg list_applications(
"l",
"list-modes",
"List the available modes", cmd,
false);
100 SwitchArg help(
"h",
"help",
"Displays usage information and exits.", cmd,
false);
103 UnlabeledMultiArg<string> multi(
"params",
"application arguments",
false,
"");
105 cmd.parse(argc, argv);
107 if (list_applications.getValue()) {
108 std::cout <<
"Available modes:"<<std::endl;
109 for (std::string app: apps ) {
110 cout <<
" -"<<app<<std::endl;
112 }
else if (help.getValue() && !application_Arg.isSet()) {
113 cmd.getOutput()->usage(cmd);
116 vector<string> params = multi.getValue();
119 std::string appArg =
" -m " + (application_Arg.isSet() ? application_Arg.getValue() : default_app);
120 params.insert(params.begin(), std::string(argv[0])+appArg);
123 if (help.getValue()) {
124 params.insert(params.begin() + 1,
"--help");
128 std::vector<char *> argv_new(params.size() + 1);
129 for(std::size_t i=0; i < params.size(); ++i) {
130 argv_new[i] =
new char[params[i].length() + 1];
131 std::strcpy(argv_new[i], params[i].c_str());
133 int argc_new = params.size();
136 if (application_Arg.isSet()) {
137 exit_status =
Create(application_Arg.getValue())(argc_new, argv_new.data());
139 exit_status =
Create(default_app)(argc_new, argv_new.data());
142 for(
int i = 0; i < argc_new; ++i) {
143 if (argv_new[i]!=0) {
144 delete [] argv_new[i];
149 catch (exception& e) {
150 exit_status = EXIT_FAILURE;
151 cerr << e.what() << endl;
154 exit_status = EXIT_FAILURE;
155 cerr <<
"Unknown exception." << endl;
170 #endif // end_of_include_guard
Interface/Implementation for the exec modes.
Interface/Implementation for the exec modes.
Namespace for startup modes for simPT tools.
Interface/Implementation for the exec modes.
static Function Create(const std::string &application)
Create a function.
static std::vector< std::string > List()
Lists all the registered applications.
std::function< int(int argc, char **argv)> Function
Function representing a certain mode.
static bool IsValid(const std::string &selection)
Check whether selection is valid i.e.
A map to hold std::functions.
Interface/Implementation for the exec modes.
Interface/Implementation FunctionMap.
static std::string DefaultApplication()
Returns the default application.
Factory produces functions that start the different modes of the various executables.
Interface/Implementation for the exec modes.