- 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:
parent
4102348817
commit
ea9b2a33a1
|
@ -36,6 +36,9 @@ Config::_addOption(char shortArg,
|
||||||
case(STRING):
|
case(STRING):
|
||||||
_strOptMap[name] = "";
|
_strOptMap[name] = "";
|
||||||
break;
|
break;
|
||||||
|
case(FUNCTION):
|
||||||
|
_fnOptMap[name] = NULL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -55,13 +58,12 @@ int
|
||||||
Config::addOption(char shortArg,
|
Config::addOption(char shortArg,
|
||||||
const std::string &longArg,
|
const std::string &longArg,
|
||||||
const std::string &name,
|
const std::string &name,
|
||||||
int type,
|
|
||||||
int defaultValue)
|
int defaultValue)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
// add the option to the config system
|
// add the option to the config system
|
||||||
error = _addOption(shortArg, longArg, name, type);
|
error = _addOption(shortArg, longArg, name, INTEGER);
|
||||||
if(error) {
|
if(error) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
@ -85,13 +87,12 @@ int
|
||||||
Config::addOption(char shortArg,
|
Config::addOption(char shortArg,
|
||||||
const std::string &longArg,
|
const std::string &longArg,
|
||||||
const std::string &name,
|
const std::string &name,
|
||||||
int type,
|
|
||||||
const std::string &defaultValue)
|
const std::string &defaultValue)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
// add the option to the config system
|
// add the option to the config system
|
||||||
error = _addOption(shortArg, longArg, name, type);
|
error = _addOption(shortArg, longArg, name, STRING);
|
||||||
if(error) {
|
if(error) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
@ -105,6 +106,30 @@ Config::addOption(char shortArg,
|
||||||
return 0;
|
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.
|
* Sets the specified option to the given integer value.
|
||||||
*/
|
*/
|
||||||
|
@ -145,6 +170,27 @@ Config::setOption(const std::string &name,
|
||||||
return 0;
|
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
|
int
|
||||||
Config::getOption(const std::string &name,
|
Config::getOption(const std::string &name,
|
||||||
std::string *value)
|
std::string *value)
|
||||||
|
@ -196,7 +242,6 @@ Config::getOption(const std::string &name,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the command line arguments. Short args are of the form -f
|
* 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,
|
* <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<std::string, std::string>::iterator long_i, str_i;
|
||||||
std::map<char, std::string>::iterator short_i;
|
std::map<char, std::string>::iterator short_i;
|
||||||
std::map<std::string, int>::iterator int_i;
|
std::map<std::string, int>::iterator int_i;
|
||||||
|
std::map<std::string, void (*)(void)>::iterator fn_i;
|
||||||
std::string arg, opt, value;
|
std::string arg, opt, value;
|
||||||
|
|
||||||
for(int i = 1; i < argc; i++) {
|
for(int i = 1; i < argc; i++) {
|
||||||
|
@ -246,6 +292,13 @@ Config::_parseArgs(int argc,
|
||||||
opt = short_i->second;
|
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
|
// make sure we've got a value
|
||||||
if(i + 1 >= argc) {
|
if(i + 1 >= argc) {
|
||||||
// XXX missing value
|
// XXX missing value
|
||||||
|
@ -288,12 +341,7 @@ Config::parse(int argc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse the arguments
|
// parse the arguments
|
||||||
error = _parseArgs(argc, argv);
|
return _parseArgs(argc, argv);
|
||||||
if(error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,9 @@ class Config {
|
||||||
private:
|
private:
|
||||||
std::string _dir;
|
std::string _dir;
|
||||||
|
|
||||||
std::map<std::string, std::string> _strOptMap;
|
std::map<std::string, std::string> _strOptMap;
|
||||||
std::map<std::string, int> _intOptMap;
|
std::map<std::string, int> _intOptMap;
|
||||||
|
std::map<std::string, void (*)(void)> _fnOptMap;
|
||||||
|
|
||||||
std::map<char, std::string> _shortArgMap;
|
std::map<char, std::string> _shortArgMap;
|
||||||
std::map<std::string, std::string> _longArgMap;
|
std::map<std::string, std::string> _longArgMap;
|
||||||
|
@ -20,8 +21,9 @@ private:
|
||||||
int _parseArgs(int, char **);
|
int _parseArgs(int, char **);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const static int STRING = 1;
|
const static int STRING = 1;
|
||||||
const static int INTEGER = 2;
|
const static int INTEGER = 2;
|
||||||
|
const static int FUNCTION = 3;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Config(std::string d) : _dir(d) { }
|
Config(std::string d) : _dir(d) { }
|
||||||
|
@ -32,15 +34,18 @@ public:
|
||||||
* parse().
|
* parse().
|
||||||
*/
|
*/
|
||||||
int addOption(char, const std::string &,
|
int addOption(char, const std::string &,
|
||||||
const std::string &, int, int);
|
const std::string &, int);
|
||||||
int addOption(char, const std::string &,
|
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.
|
* Sets a configuration option. Can be called at any time.
|
||||||
*/
|
*/
|
||||||
int setOption(const std::string &, int);
|
int setOption(const std::string &, int);
|
||||||
int setOption(const std::string &, const std::string &);
|
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 &, std::string *);
|
||||||
int getOption(const std::string &, const char **);
|
int getOption(const std::string &, const char **);
|
||||||
|
|
Loading…
Reference in New Issue