37 int &i,
int argc,
char* argv[],
38 DefaultCallback callback
46 std::string optname = argv[i++];
49 while (optname[0] ==
'-')
50 optname.erase(optname.begin());
53 std::string optarg =
"";
56 auto iter = std::find (optname.begin(), optname.end(),
'=');
57 if (iter != optname.end())
59 optarg = optname.substr(iter-optname.begin()+1);
60 optname = optname.substr(0, iter-optname.begin());
65 if (i < argc and argv[i][0] !=
'-')
69 return callback (optname, optarg);
92 template <
class Callback,
class ...Params>
bool HandleSwitch
94 int &i,
int argc,
char* argv[],
95 std::string longoptname, std::string shortoptname,
unsigned noptarg, Callback callback,
104 std::string optname = argv[i];
107 while (optname[0] ==
'-')
108 optname.erase(optname.begin());
111 std::string optarg =
"";
114 auto iter = std::find (optname.begin(), optname.end(),
'=');
115 if (iter != optname.end())
117 optarg = optname.substr(iter-optname.begin()+1);
118 optname = optname.substr(0, iter-optname.begin());
122 if (optname == longoptname or optname == shortoptname)
130 return callback (
"");
132 else if (noptarg == 1)
135 if (optarg.size() > 0)
136 return callback (optarg);
140 throw exception (
"Missing an argument for the option \"%s\".", optname.c_str());
143 return callback (argv[i++]);
147 throw exception (
"An option cannot have more than one arguments, but \"%s\" has %d.", optname.c_str(), noptarg);
195 int argc,
char* argv[], Params ...params
class NodeIntegrator exception
void ParseCommandLine(int argc, char *argv[], Params...params)
Parse command line.
Definition: cmdline.h:194
bool HandleSwitch(int &i, int argc, char *argv[], DefaultCallback callback)
Command line option default hanndler.
Definition: cmdline.h:36