Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Parse a string option.
- *
- * The default value is returned if the option was not passed.
- */
- std::string parseOption(
- const std::string name,
- const std::string defaultValue,
- const std::map<std::string,std::string> &config);
- /** Parse a flag (i.e. boolean) option.
- *
- * Default value is 'false'. It is returned if the option was not passed. If
- * the option was passed, but the value passed is maleformed (i.e. not "true"
- * or "false"), print error and return default value.
- */
- bool parseFlagOption(
- const std::string name,
- const std::map<std::string,std::string> &config);
- /** Parse an option and perform a lexical cast.
- *
- * The default value is returned if the option was not passed. If the option
- * was passed, but the lexical cast throws an exception, print error and
- * return default value.
- */
- template<class T>
- T parseOptionLexicalCast(
- const std::string name,
- const T defaultValue,
- const std::map<std::string,std::string> &config);
- /** Parse a path option and resolve the path.
- *
- * If the option was not passed, the default value is resolved and
- * returned. If in that case the default value can not be resolved, print an
- * error and return an empty string. If the option was passed, but the value
- * could not be resolved as a path, print error and return default value, else
- * return the resolved path.
- */
- std::string parsePathOption(
- const std::string name,
- const std::string defaultValue,
- const std::map<std::string,std::string> &config);
- // ------------------------------------------------------
- string PlacePropertySaver::parseOption(
- const string name,
- const string defaultValue,
- const map<string,string> &config)
- {
- map<string, string>::const_iterator it = config.find(name);
- if (it != config.end())
- return it->second;
- return defaultValue; // default if not given
- }
- // ------------------------------------------------------
- bool PlacePropertySaver::parseFlagOption(
- const string name,
- const map<string,string> &config)
- {
- map<string, string>::const_iterator it = config.find(name);
- if (it != config.end())
- {
- if (it->second == "true")
- {
- return true;
- }
- else if (it->second == "false")
- {
- return false;
- }
- else
- {
- error("Invalid value '%s' for flag option '%s'. "
- "Should be one of {'true', 'false'}",
- it->second.c_str(), name.c_str());
- return false; // default if malformed
- }
- }
- return false; // default if not given
- }
- // ------------------------------------------------------
- template<class T>
- T PlacePropertySaver::parseOptionLexicalCast(
- const std::string name,
- const T defaultValue,
- const std::map<std::string,std::string> &config)
- {
- map<string, string>::const_iterator it = config.find(name);
- if (it != config.end())
- {
- try
- {
- return boost::lexical_cast<T>(it->second);
- }
- catch(boost::bad_lexical_cast &)
- {
- error("Value '%s' of option '%s' is not of type '%s'",
- it->second.c_str(), name.c_str(), typeid(T).name());
- return defaultValue; // default if malformed
- }
- }
- return defaultValue; // default if not given
- }
- // ------------------------------------------------------
- string PlacePropertySaver::parsePathOption(
- const string name,
- const string defaultValue,
- const map<string,string> &config)
- {
- string tmp = parseOption("--save-file-name", defaultValue, config);
- shared_array<char> path(realpath(tmp.c_str(), 0));
- if (path)
- {
- return path.get();
- }
- else
- {
- error("Value '%s' of option '%s' could not be resolved as a valid path",
- tmp.c_str(), name.c_str());
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement