- added a way to have options like '-h, --help' by having the user of the class specify a function that is called when such options are seen

This commit is contained in:
gimmedonutnow 2006-08-14 22:35:14 +00:00
parent 4102348817
commit ea9b2a33a1
2 changed files with 70 additions and 17 deletions

View File

@ -36,6 +36,9 @@ Config::_addOption(char shortArg,
case(STRING):
_strOptMap[name] = "";
break;
case(FUNCTION):
_fnOptMap[name] = NULL;
break;
default:
break;
}
@ -55,13 +58,12 @@ int
Config::addOption(char shortArg,
const std::string &longArg,
const std::string &name,
int type,
int defaultValue)
{
int error;
// add the option to the config system
error = _addOption(shortArg, longArg, name, type);
error = _addOption(shortArg, longArg, name, INTEGER);
if(error) {
return error;
}
@ -85,13 +87,12 @@ int
Config::addOption(char shortArg,
const std::string &longArg,
const std::string &name,
int type,
const std::string &defaultValue)
{
int error;
// add the option to the config system
error = _addOption(shortArg, longArg, name, type);
error = _addOption(shortArg, longArg, name, STRING);
if(error) {
return error;
}
@ -105,6 +106,30 @@ Config::addOption(char shortArg,
return 0;
}
int
Config::addOption(char shortArg,
const std::string &longArg,
const std::string &name,
void (*defaultFn)(void))
{
int error;
// add the option to the config system
error = _addOption(shortArg, longArg, name, FUNCTION);
if(error) {
return error;
}
// set the option to the default value
error = setOption(name, defaultFn);
if(error) {
return error;
}
return 0;
}
/**
* Sets the specified option to the given integer value.
*/
@ -145,6 +170,27 @@ Config::setOption(const std::string &name,
return 0;
}
/**
* Sets the specified option to the given function.
*/
int
Config::setOption(const std::string &name,
void (*value)(void))
{
std::map<std::string, void (*)(void)>::iterator opt_i;
// confirm that the option exists
opt_i = _fnOptMap.find(name);
if(opt_i == _fnOptMap.end()) {
return -1;
}
// set the option
opt_i->second = value;
return 0;
}
int
Config::getOption(const std::string &name,
std::string *value)
@ -196,7 +242,6 @@ Config::getOption(const std::string &name,
return 0;
}
/**
* Parses the command line arguments. Short args are of the form -f
* <opt>, long args are of the form --foo <opt>. Returns < 0 on error,
@ -210,6 +255,7 @@ Config::_parseArgs(int argc,
std::map<std::string, std::string>::iterator long_i, str_i;
std::map<char, std::string>::iterator short_i;
std::map<std::string, int>::iterator int_i;
std::map<std::string, void (*)(void)>::iterator fn_i;
std::string arg, opt, value;
for(int i = 1; i < argc; i++) {
@ -246,6 +292,13 @@ Config::_parseArgs(int argc,
opt = short_i->second;
}
// check if its a function, and if so, call the function
fn_i = _fnOptMap.find(opt);
if(fn_i != _fnOptMap.end()) {
(*(fn_i->second))();
continue;
}
// make sure we've got a value
if(i + 1 >= argc) {
// XXX missing value
@ -288,12 +341,7 @@ Config::parse(int argc,
}
// parse the arguments
error = _parseArgs(argc, argv);
if(error) {
return error;
}
return 0;
return _parseArgs(argc, argv);
}

View File

@ -8,8 +8,9 @@ class Config {
private:
std::string _dir;
std::map<std::string, std::string> _strOptMap;
std::map<std::string, int> _intOptMap;
std::map<std::string, std::string> _strOptMap;
std::map<std::string, int> _intOptMap;
std::map<std::string, void (*)(void)> _fnOptMap;
std::map<char, std::string> _shortArgMap;
std::map<std::string, std::string> _longArgMap;
@ -20,8 +21,9 @@ private:
int _parseArgs(int, char **);
public:
const static int STRING = 1;
const static int INTEGER = 2;
const static int STRING = 1;
const static int INTEGER = 2;
const static int FUNCTION = 3;
public:
Config(std::string d) : _dir(d) { }
@ -32,15 +34,18 @@ public:
* parse().
*/
int addOption(char, const std::string &,
const std::string &, int, int);
const std::string &, int);
int addOption(char, const std::string &,
const std::string &, int, const std::string &);
const std::string &, const std::string &);
int addOption(char, const std::string &,
const std::string &, void (*)(void));
/**
* Sets a configuration option. Can be called at any time.
*/
int setOption(const std::string &, int);
int setOption(const std::string &, const std::string &);
int setOption(const std::string &, void (*)(void));
int getOption(const std::string &, std::string *);
int getOption(const std::string &, const char **);