You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 07:44:14 UTC
svn commit: r1131881 - in /incubator/mesos/trunk/src: configuration.cpp
configuration.hpp master.cpp master_main.cpp option.hpp
tests/test_configuration.cpp
Author: benh
Date: Sun Jun 5 05:44:13 2011
New Revision: 1131881
URL: http://svn.apache.org/viewvc?rev=1131881&view=rev
Log:
Made addOption a little nicer so that default value does not need to be specified as a string all the time
Modified:
incubator/mesos/trunk/src/configuration.cpp
incubator/mesos/trunk/src/configuration.hpp
incubator/mesos/trunk/src/master.cpp
incubator/mesos/trunk/src/master_main.cpp
incubator/mesos/trunk/src/option.hpp
incubator/mesos/trunk/src/tests/test_configuration.cpp
Modified: incubator/mesos/trunk/src/configuration.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configuration.cpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configuration.cpp (original)
+++ incubator/mesos/trunk/src/configuration.cpp Sun Jun 5 05:44:13 2011
@@ -182,7 +182,7 @@ string Configuration::getUsage() const
int maxLen = 0;
foreachpair (const string& key, const Option& opt, options) {
int len = key.length() + LONG_PAD;
- len += opt.hasShort ? SHORT_PAD : 0;
+ len += opt.hasShortName ? SHORT_PAD : 0;
maxLen = len > maxLen ? len : maxLen;
}
@@ -195,7 +195,7 @@ string Configuration::getUsage() const
}
line += "--" + key + "=VAL";
- line += opt.hasShort ? string(" (or -") + opt.shortName + " VAL)" : "";
+ line += opt.hasShortName ? string(" (or -") + opt.shortName + " VAL)" : "";
string pad(PAD + maxLen - line.size(), ' ');
line += pad;
size_t pos1 = 0, pos2 = 0;
@@ -236,7 +236,7 @@ Params& Configuration::getParams()
string Configuration::getLongName(char shortName) const
{
foreachpair (const string& key, const Option& opt, options) {
- if (opt.hasShort && opt.shortName == shortName)
+ if (opt.hasShortName && opt.shortName == shortName)
return key;
}
return "";
Modified: incubator/mesos/trunk/src/configuration.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configuration.hpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configuration.hpp (original)
+++ incubator/mesos/trunk/src/configuration.hpp Sun Jun 5 05:44:13 2011
@@ -80,61 +80,105 @@ public:
string getUsage() const;
+private:
/**
- * Adds a registered option together with a default value and a help string.
+ * Private method for adding an option with, potentially, a default
+ * value and a short name.
* @param optName name of the option, e.g. "home"
* @param helpString description of the option, may contain line breaks
- * @param defaultValue default value of the option.
+ * @param hasShortName whether the option has a short name
+ * @param shortName character representing short name of option, e.g. 'h'
+ * @param hasDefault whether the option has a default value
+ * @param defaultValue default value of the option, as a string.
* The default option is put in the internal params,
* unless the option already has a value in params.
- * Its type must support operator<<(ostream,...)
- * @param shortName character representing short name of option, e.g. 'I'
- * @return 0 on success, -1 if option already exists,
- * -2 if defaultValue cannot be converted to templated type T
**/
template <class T>
- int addOption(string optName, const string& helpString,
- const string& defaultValue, char shortName = '\0')
+ void addOption(string optName,
+ const string& helpString,
+ bool hasShortName,
+ char shortName,
+ bool hasDefault,
+ const string& defaultValue)
{
std::transform(optName.begin(), optName.end(), optName.begin(), ::tolower);
- if (options.find(optName) != options.end())
- return -1;
- try {
- lexical_cast<T>(defaultValue);
- } catch(const bad_lexical_cast& ex) { return -2; }
-
- options[optName] = Option(helpString, Validator<T>(),
- defaultValue, shortName);
-
- if (!params.contains(optName)) // insert default value
+ if (options.find(optName) != options.end()) {
+ string message = "Duplicate option registration: " + optName;
+ throw ConfigurationException(message.c_str());
+ }
+ options[optName] = Option(helpString,
+ Validator<T>(),
+ hasShortName,
+ shortName,
+ hasDefault,
+ defaultValue);
+ if (hasDefault && !params.contains(optName)) {
+ // insert default value into params
params[optName] = defaultValue;
+ }
+ }
- return 0;
+public:
+ /**
+ * Adds a registered option together with a help string.
+ * @param optName name of the option, e.g. "home"
+ * @param helpString description of the option, may contain line breaks
+ **/
+ template <class T>
+ void addOption(string optName, const string& helpString)
+ {
+ addOption<T>(optName, helpString, false, '\0', false, "");
}
-
/**
- * Adds a registered option together with a default value and a help string.
+ * Adds a registered option with a short name and help string.
* @param optName name of the option, e.g. "home"
+ * @param shortName character representing short name of option, e.g. 'h'
* @param helpString description of the option, may contain line breaks
- * @param shortName character representing short name of option, e.g. 'I'
- * @return 0 on success, -1 if option already exists
**/
template <class T>
- int addOption(string optName, const string& helpString,
- char shortName = '\0')
+ void addOption(string optName, char shortName, const string& helpString)
{
- std::transform(optName.begin(), optName.end(), optName.begin(), ::tolower);
- if (options.find(optName) != options.end())
- return -1;
- options[optName] = Option(helpString, Validator<T>(), shortName);
+ addOption<T>(optName, helpString, true, shortName, false, "");
+ }
- return 0;
+ /**
+ * Adds a registered option with a default value and a help string.
+ * @param optName name of the option, e.g. "home"
+ * @param helpString description of the option, may contain line breaks
+ * @param defaultVal default value of option.
+ * The default option is put in the internal params,
+ * unless the option already has a value in params.
+ * Its type must support operator<<(ostream,...)
+ **/
+ template <class T>
+ void addOption(string optName, const string& helpString, const T& defaultVal)
+ {
+ string defaultStr = lexical_cast<string>(defaultVal);
+ addOption<T>(optName, helpString, false, '\0', true, defaultStr);
}
+ /**
+ * Adds a registered option with a default value, short name and help string.
+ * @param optName name of the option, e.g. "home"
+ * @param helpString description of the option, may contain line breaks
+ * @param defaultVal default value of option.
+ * The default option is put in the internal params,
+ * unless the option already has a value in params.
+ * Its type must support operator<<(ostream,...)
+ **/
+ template <class T>
+ void addOption(string optName,
+ char shortName,
+ const string& helpString,
+ const T& defaultVal)
+ {
+ string defaultStr = lexical_cast<string>(defaultVal);
+ addOption<T>(optName, helpString, true, shortName, true, defaultStr);
+ }
/**
- * Returns the name of all options.
+ * Returns the names of all registered options.
* @return name of every registered option
**/
vector<string> getOptions() const;
Modified: incubator/mesos/trunk/src/master.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/master.cpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/master.cpp (original)
+++ incubator/mesos/trunk/src/master.cpp Sun Jun 5 05:44:13 2011
@@ -151,7 +151,7 @@ Master::~Master()
void Master::registerOptions(Configuration* conf)
{
- conf->addOption<string>("allocator", "Allocation module to use", "simple");
+ conf->addOption<string>("allocator", 'a', "Allocation module name", "simple");
}
Modified: incubator/mesos/trunk/src/master_main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/master_main.cpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/master_main.cpp (original)
+++ incubator/mesos/trunk/src/master_main.cpp Sun Jun 5 05:44:13 2011
@@ -31,9 +31,9 @@ void usage(const char* programName, cons
int main(int argc, char **argv)
{
Configuration conf;
- conf.addOption<string>("url", "URL used for contending to a master.");
- conf.addOption<int>("port", "Port to listen on", 'p');
- conf.addOption<bool>("quiet", "Do not log to stderr", "0", 'q');
+ conf.addOption<string>("url", 'u', "URL used for contending to a master.");
+ conf.addOption<int>("port", 'p', "Port to listen on");
+ conf.addOption<bool>("quiet", 'q', "Do not log to stderr", false);
conf.addOption<string>("log_dir", "Where to place logs", "/tmp");
Master::registerOptions(&conf);
Modified: incubator/mesos/trunk/src/option.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/option.hpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/option.hpp (original)
+++ incubator/mesos/trunk/src/option.hpp Sun Jun 5 05:44:13 2011
@@ -76,49 +76,40 @@ struct BadOptionValueException : std::ex
/**
* Registered option with help string and default value
**/
-struct Option {
-
+class Option {
+public:
Option() : hasDefault(false), validator(NULL) {}
Option(const string& _helpString,
const ValidatorBase& _validator,
- const string& _defaultValue = "",
- char _shortName = '\0')
+ bool _hasShortName,
+ char _shortName,
+ bool _hasDefault,
+ const string& _defaultValue)
: helpString(_helpString),
+ hasDefault(_hasDefault),
defaultValue(_defaultValue),
+ hasShortName(_hasShortName),
shortName(_shortName)
{
- hasDefault = _defaultValue == "" ? false : true;
- hasShort = _shortName == '\0' ? false : true;
- validator = _validator.clone();
- }
-
- Option(const string& _helpString,
- const ValidatorBase& _validator,
- char _shortName)
- : helpString(_helpString),
- hasDefault(false),
- shortName(_shortName)
- {
- hasShort = _shortName == '\0' ? false : true;
validator = _validator.clone();
}
Option(const Option& opt)
: helpString(opt.helpString),
hasDefault(opt.hasDefault),
- hasShort(opt.hasShort),
+ hasShortName(opt.hasShortName),
shortName(opt.shortName),
defaultValue(opt.defaultValue)
{
- validator = opt.validator == NULL ? NULL : opt.validator->clone();
+ validator = (opt.validator == NULL) ? NULL : opt.validator->clone();
}
Option &operator=(const Option& opt)
{
helpString = opt.helpString;
hasDefault = opt.hasDefault;
- hasShort = opt.hasShort;
+ hasShortName = opt.hasShortName;
shortName = opt.shortName;
defaultValue = opt.defaultValue;
validator = opt.validator == NULL ? NULL : opt.validator->clone();
@@ -133,7 +124,7 @@ struct Option {
string helpString;
bool hasDefault;
string defaultValue;
- bool hasShort;
+ bool hasShortName;
char shortName;
ValidatorBase *validator;
};
Modified: incubator/mesos/trunk/src/tests/test_configuration.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_configuration.cpp?rev=1131881&r1=1131880&r2=1131881&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/test_configuration.cpp (original)
+++ incubator/mesos/trunk/src/tests/test_configuration.cpp Sun Jun 5 05:44:13 2011
@@ -41,20 +41,16 @@ TEST(ConfigurationTest, DefaultOptions)
Configuration conf;
- int ret = 0;
EXPECT_NO_THROW( {
- ret += conf.addOption<int>("test1", "Testing option", "500");
- ret += conf.addOption<short>("test2", "Another tester", "0");
- ret += conf.addOption<long>("test3", "Tests the default", "2010");
- ret += conf.addOption<string>("test4", "Option without default");
- ret += conf.addOption<string>("test5", "Option with a default", "arb");
+ conf.addOption<int>("test1", "Testing option", 500);
+ conf.addOption<short>("test2", "Another tester", 0);
+ conf.addOption<long>("test3", "Tests the default", 2010);
+ conf.addOption<string>("test4", "Option without default");
+ conf.addOption<string>("test5", "Option with a default", "arb");
conf.load(ARGC, argv, false);
} );
- int addOptionError = ret;
- EXPECT_EQ(addOptionError, 0);
-
- conf.addOption<int>("excp", "Exception tester.", "50");
+ conf.addOption<int>("excp", "Exception tester.", 50);
EXPECT_THROW(conf.validate(), BadOptionValueException);
conf.getParams()["excp"] = "27";
EXPECT_NO_THROW(conf.validate());
@@ -82,11 +78,11 @@ TEST(ConfigurationTest, CommandLine)
argv[7] = (char*) "--space=Long String";
Configuration conf;
- int addOptionError = 0;
- addOptionError += conf.addOption<int>("negative", "some val", "-30", 'N');
- addOptionError += conf.addOption<string>("test1", "textual value", "text2");
+ EXPECT_NO_THROW( {
+ conf.addOption<int>("negative", 'N', "some val", -30);
+ conf.addOption<string>("test1", "textual value", "text2");
+ } );
- EXPECT_EQ(addOptionError, 0);
EXPECT_NO_THROW( conf.load(ARGC, argv, false) );
EXPECT_EQ("text1", conf.getParams()["test1"]);