9 #ifndef KTCOMMANDLINEHANDLER_H_ 10 #define KTCOMMANDLINEHANDLER_H_ 15 #include "singleton.hh" 17 #include <boost/program_options.hpp> 18 namespace po = boost::program_options;
28 KTLOGGER(utillog_clh,
"KTCommandLineHandler.hh");
60 typedef std::map< std::string, po::options_description* >
OptDescMap;
79 const std::string& GetExecutableName()
const;
80 const std::string& GetPackageString()
const;
91 bool ProcessCommandLine(
int argC,
char** argV);
93 bool TakeArguments(
int argC,
char** argV);
94 bool GetArgumentsTaken();
109 OptDescMapIt CreateNewOptionGroup(
const std::string& aTitle);
112 bool AddOption(
const std::string& aTitle,
const std::string& aHelpMsg,
const std::string& aLongOpt,
char aShortOpt,
bool aWarnOnDuplicate=
true);
114 bool AddOption(
const std::string& aTitle,
const std::string& aHelpMsg,
const std::string& aLongOpt,
bool aWarnOnDuplicate=
true);
117 template<
class XValueType >
118 bool AddOption(
const std::string& aTitle,
const std::string& aHelpMsg,
const std::string& aLongOpt,
char aShortOpt,
bool aWarnOnDuplicate=
true);
120 template<
class XValueType >
121 bool AddOption(
const std::string& aTitle,
const std::string& aHelpMsg,
const std::string& aLongOpt,
bool aWarnOnDuplicate=
true);
124 po::options_description* GetOptionsDescription(
const std::string& aKey);
127 bool FinalizeNewOptionGroups();
140 bool AddCommandLineOptions(
const po::options_description& aSetOfOpts);
148 void InitialCommandLineProcessing();
151 bool DelayedCommandLineProcessing();
162 bool IsCommandLineOptSet(
const std::string& aCLOption);
165 template<
class XReturnType >
166 XReturnType GetCommandLineValue(
const std::string& aCLOption);
169 template<
class XReturnType >
170 XReturnType GetCommandLineValue(
const std::string& aCLOption, XReturnType defaultValue);
172 bool GetPrintHelpMessageFlag()
const;
173 bool GetPrintHelpMessageAfterConfigFlag()
const;
174 bool GetPrintVersionMessage()
const;
177 const std::string& GetConfigFilename()
const;
180 const std::string& GetCommandLineJSON()
const;
182 const po::parsed_options* GetParsedOptions()
const;
183 const po::variables_map* GetVariablesMap()
const;
185 const scarab::param_node* GetConfigOverride()
const;
192 static const char fDash =
'-';
193 static const char fSeparator =
'=';
194 static const char fNodeSeparator =
'.';
208 virtual void PrintHelpMessage();
209 virtual void PrintVersionMessage();
214 return fExecutableName;
218 return fPackageString;
221 template<
class XValueType >
224 if (fAllOptionsLong.find(aLongOpt) != fAllOptionsLong.end())
226 if (aWarnOnDuplicate)
227 KTWARN(utillog_clh,
"There is already an option called <" << aLongOpt <<
">");
230 if (fAllOptionsShort.find(aShortOpt) != fAllOptionsShort.end())
232 if (aWarnOnDuplicate)
233 KTWARN(utillog_clh,
"There is already a short option called <" << aShortOpt <<
">");
240 if (tIter == fProposedGroups.end())
242 tIter = CreateNewOptionGroup(aTitle);
245 std::string tOptionName = aLongOpt;
246 fAllOptionsLong.insert(aLongOpt);
247 tOptionName +=
"," + std::string(&aShortOpt);
248 fAllOptionsShort.insert(aShortOpt);
250 tIter->second->add_options()(tOptionName.c_str(), po::value< XValueType >(), aHelpMsg.c_str());
256 template<
class XValueType >
259 if (fAllOptionsLong.find(aLongOpt) != fAllOptionsLong.end())
261 if (aWarnOnDuplicate)
262 KTWARN(utillog_clh,
"There is already an option called <" << aLongOpt <<
">");
269 if (tIter == fProposedGroups.end())
271 tIter = CreateNewOptionGroup(aTitle);
274 fAllOptionsLong.insert(aLongOpt);
276 tIter->second->add_options()(aLongOpt.c_str(), po::value< XValueType >(), aHelpMsg.c_str());
283 template<
class XReturnType >
286 if (! IsCommandLineOptSet(aCLOption))
288 KTWARN(utillog_clh,
"Command line option <" << aCLOption <<
"> was not set!\n" 289 "Next time check whether it's set before calling this function.");
292 return fCommandLineVarMap[aCLOption].as< XReturnType >();
295 template<
class XReturnType >
298 if (! IsCommandLineOptSet(aCLOption))
302 return fCommandLineVarMap[aCLOption].as< XReturnType >();
307 return fConfigFilename;
312 return fCommandLineJSON;
317 return fPrintVersionMessage;
322 return fPrintHelpMessage;
327 return fPrintHelpMessageAfterConfig;
332 return &fParsedOptions;
337 return &fCommandLineVarMap;
342 return &fConfigOverrideValues;
std::string fExecutableName
bool GetPrintVersionMessage() const
std::set< char > fAllOptionsShort
CommandLineHandlerException(std::string const &why)
const scarab::param_node * GetConfigOverride() const
const std::string & GetPackageString() const
std::vector< std::string > fCommandLineParseLater
OptDescMap::iterator OptDescMapIt
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)
const std::string & GetConfigFilename() const
Return the file name provided by the user on the command line for the config file.
bool GetPrintHelpMessageAfterConfigFlag() const
bool fPrintHelpMessageAfterConfig
scarab::param_node fConfigOverrideValues
const po::variables_map * GetVariablesMap() const
po::options_description fCommandLineOptions
std::set< std::string > fAllOptionsLong
std::string fPackageString
const std::string & GetCommandLineJSON() const
Return the string of json provided by the user on the command line.
XReturnType GetCommandLineValue(const std::string &aCLOption)
Return the value of a command line option; throws an exception if the value was not set...
Parses and stores command-line options.
bool fPrintVersionMessage
KTLOGGER(applog, "KTApplication")
std::map< std::string, po::options_description *> OptDescMap
po::parsed_options fParsedOptions
OptDescMap fProposedGroups
std::string fConfigFilename
const std::string & GetExecutableName() const
po::variables_map fCommandLineVarMap
bool GetPrintHelpMessageFlag() const
const po::parsed_options * GetParsedOptions() const
OptDescMap::const_iterator OptDescMapCIt
Contains the logger class and macros, based on Kasper's KLogger class.
std::set< std::string > fAllGroupKeys
std::string fCommandLineJSON