16 #ifndef PACKAGE_STRING 17 #define PACKAGE_STRING Nymph (unknown version) 19 #define STRINGIFY_1(x) #x 20 #define STRINGIFY_2(x) STRINGIFY_1(x) 27 KTLOGGER(utillog,
"KTCommandLineHandler");
30 :
std::logic_error(why)
34 fExecutableName(
"NONE"),
38 fArgumentsTaken(false),
39 fCommandLineOptions(),
41 fCommandLineParseLater(),
44 fConfigOverrideValues(),
45 fPrintHelpMessage(false),
46 fPrintVersionMessage(false),
47 fPrintHelpMessageAfterConfig(false),
104 po::options_description* tNewOpts =
new po::options_description(aTitle);
105 std::pair< OptDescMapIt, bool > result =
fProposedGroups.insert(OptDescMap::value_type(aTitle, tNewOpts));
108 KTWARN(utillog,
"There is already an option group with title <" << aTitle <<
">");
119 if (aWarnOnDuplicate)
120 KTWARN(utillog,
"There is already an option called <" << aLongOpt <<
">");
123 if (aShortOpt !=
'#')
127 if (aWarnOnDuplicate)
128 KTWARN(utillog,
"There is already a short option called <" << aShortOpt <<
">");
141 string tOptionName = aLongOpt;
143 if (aShortOpt !=
'#')
145 tOptionName +=
"," + string(&aShortOpt);
148 tIter->second->add_options()(tOptionName.c_str(), aHelpMsg.c_str());
157 if (aWarnOnDuplicate)
158 KTWARN(utillog,
"There is already an option called <" << aLongOpt <<
">");
171 tIter->second->add_options()(aLongOpt.c_str(), aHelpMsg.c_str());
181 KTWARN(utillog,
"There no proposed option group with key <" << aKey <<
">");
184 return tIter->second;
197 delete tIter->second;
211 catch (std::exception& e)
213 KTERROR(utillog,
"Exception thrown while adding options: " << e.what());
218 KTERROR(utillog,
"Exception was thrown, but caught in a generic way!");
239 KTERROR(utillog,
"An error occurred while adding the proposed option groups\n" <<
240 "Command-line options were not parsed");
250 catch (std::exception& e)
252 KTERROR(utillog,
"An error occurred while boost was parsing the command line options:\n" << e.what());
257 vector< string > tRemainingToParse = po::collect_unrecognized(
fParsedOptions.options, po::include_positional);
264 for (vector< string >::const_iterator tokenIt = tRemainingToParse.begin(); tokenIt != tRemainingToParse.end(); ++tokenIt)
266 string argument(*tokenIt);
267 size_t t_name_pos = argument.find_first_not_of(
fDash );
268 size_t t_val_pos = argument.find_first_of(
fSeparator );
270 if( t_name_pos == 2 && t_val_pos != string::npos )
272 string t_full_name(argument.substr( t_name_pos, t_val_pos-2 ));
274 size_t t_node_start_pos = 0;
275 size_t t_node_sep_pos = t_full_name.find_first_of(
fNodeSeparator );
277 while (t_node_sep_pos != string::npos)
279 string nodeName(t_full_name.substr(t_node_start_pos, t_node_sep_pos));
280 if (parentNode.has(nodeName))
282 parentNode = parentNode[nodeName].as_node();
286 parentNode.add(nodeName, scarab::param_ptr_t(
new scarab::param_node()));
287 parentNode = parentNode[nodeName].as_node();
289 t_node_start_pos = t_node_sep_pos + 1;
290 t_node_sep_pos = t_full_name.find_first_of(
fNodeSeparator, t_node_start_pos);
293 string valueName(t_full_name.substr(t_node_start_pos, t_val_pos));
295 scarab::param_value* new_value =
new scarab::param_value();
296 new_value->set(argument.substr( t_val_pos + 1 ));
300 parentNode.replace( valueName, new_value );
305 KTERROR(utillog,
"Argument <" << argument <<
"> does not match --<name>=<value> pattern");
328 po::options_description tGeneralOpts(
"General options");
329 tGeneralOpts.add_options()(
"help,h",
"Print help message")(
"help-config",
"Print help message after reading config file")(
"version,v",
"Print version information");
331 tGeneralOpts.add_options()(
"config,c", po::value< string >(),
"Configuration file");
332 tGeneralOpts.add_options()(
"json,j", po::value< string >(),
"Command-Line JSON");
346 po::options_description tHiddenOpts(
"Hidden options");
351 po::options_description tInitialOptions(
"Initial options");
352 tInitialOptions.add(tGeneralOpts).add(tHiddenOpts);
364 po::command_line_style::style_t pstyle = po::command_line_style::unix_style;
378 po::parsed_options tParsedOpts(NULL);
382 tParsedOpts = po::command_line_parser(
fNArgs,
fArgV).style(pstyle).options(tInitialOptions).allow_unregistered().run();
387 catch (std::exception& e)
389 KTERROR(utillog,
"Exception caught while performing initial CL parsing:\n" 390 <<
'\t' << e.what());
391 throw std::logic_error(e.what());
404 po::variables_map tGeneralOptsVarMap;
405 po::store(tParsedOpts, tGeneralOptsVarMap);
406 po::notify(tGeneralOptsVarMap);
409 if (tGeneralOptsVarMap.count(
"help"))
413 if (tGeneralOptsVarMap.count(
"help-config"))
417 if (tGeneralOptsVarMap.count(
"version"))
421 if (tGeneralOptsVarMap.count(
"config"))
425 if (tGeneralOptsVarMap.count(
"json"))
438 " If using a config file, it should be specified as: -c config_file.json\n" <<
439 " Config file options can be modified using: --address.of.option=\"value\"\n" <<
std::string fExecutableName
std::set< char > fAllOptionsShort
CommandLineHandlerException(std::string const &why)
std::vector< std::string > fCommandLineParseLater
void InitialCommandLineProcessing()
Parses the general options and stores the remaining options available for later parsing.
OptDescMap::iterator OptDescMapIt
bool IsCommandLineOptSet(const std::string &aCLOption)
Check if a command line option was set.
po::options_description fPrintHelpOptions
bool AddOption(const std::string &aTitle, const std::string &aHelpMsg, const std::string &aLongOpt, char aShortOpt, bool aWarnOnDuplicate=true)
Simple option adding function, with short option (flag only; no values allowed)
bool ProcessCommandLine(int argC, char **argV)
bool fPrintHelpMessageAfterConfig
bool TakeArguments(int argC, char **argV)
scarab::param_node fConfigOverrideValues
po::options_description fCommandLineOptions
std::set< std::string > fAllOptionsLong
std::string fPackageString
virtual ~KTCommandLineHandler()
po::options_description * GetOptionsDescription(const std::string &aKey)
Request access to the options description object for more freedom (and responsibility!) in adding opt...
virtual void PrintVersionMessage()
OptDescMapIt CreateNewOptionGroup(const std::string &aTitle)
Makes a new option group available for command line options.
virtual void PrintHelpMessage()
bool DelayedCommandLineProcessing()
Parses the remaining command line options (those that weren't parsed during the InitialCommandLinePro...
bool fPrintVersionMessage
KTLOGGER(applog, "KTApplication")
po::parsed_options fParsedOptions
OptDescMap fProposedGroups
static const char fNodeSeparator
Contains KTCommandLineHandler.
std::string fConfigFilename
po::variables_map fCommandLineVarMap
bool FinalizeNewOptionGroups()
Adds the groups of options to the set of usable options groups (note: this must be called to make the...
static const char fSeparator
bool AddCommandLineOptions(const po::options_description &aSetOfOpts)
Adds a set of command line options.
std::set< std::string > fAllGroupKeys
std::string fCommandLineJSON