class Appl reworked.

This commit is contained in:
Roland Jax
2014-11-15 23:03:33 +01:00
parent 4cc9055e71
commit 3273300893
7 changed files with 416 additions and 349 deletions
+172 -159
View File
@@ -22,46 +22,162 @@
#include <iomanip>
#include <cstdlib>
Appl& Appl::Instance()
Appl& Appl::Instance(const bool command)
{
static Appl instance;
static Appl instance(command);
return instance;
}
Appl::~Appl()
{
m_args.clear();
m_params.clear();
m_opts.clear();
m_optvals.clear();
}
void Appl::addArgs(const std::string argTxt, const int argNum)
void Appl::addText(const char* text)
{
m_argTxt = argTxt;
m_argNum = argNum;
opt_t opt;
opt.name = "__text_only__";
opt.shortname = "";
opt.datatype = dt_none;
opt.optiontype = ot_none;
opt.description = text;
m_opts.push_back(opt);
}
void Appl::addItem(const char* name, Param param, const char* shortname,
const char* longname, const char* description,
Datatype datatype, Optiontype optiontype)
void Appl::addOption(const char* name, const char* shortname, OptVal optval,
DataType datatype, OptionType optiontype, const char* description)
{
if (strlen(name) != 0)
m_params[name] = param;
if (strlen(name) != 0) {
if (strlen(longname) != 0) {
Arg arg;
arg.name = name;
arg.shortname = shortname;
arg.longname = longname;
arg.description = description;
arg.datatype = datatype;
arg.optiontype = optiontype;
m_args.push_back(arg);
m_optvals[name] = optval;
opt_t opt;
opt.name = name;
opt.shortname = shortname;
opt.datatype = datatype;
opt.optiontype = optiontype;
opt.description = description;
m_opts.push_back(opt);
}
}
void Appl::addVersion(const char* version)
void Appl::parseArgs(int argc, char* argv[])
{
m_version = version;
std::vector<std::string> _argv(argv, argv + argc);
m_argv = _argv;
// walk through all arguments
for (int i = 1; i < argc; i++) {
// find option with long format '--'
if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) {
// is next item an added argument?
if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos) {
if (checkOption(_argv[i].substr(2), _argv[i+1]) == false)
printHelp();
}
else {
if (checkOption(_argv[i].substr(2), "") == false)
printHelp();
}
// find option with short format '-'
} else if (_argv[i].rfind("-") == 0 && _argv[i].size() > 1) {
// walk through all characters
for (size_t j = 1; j < _argv[i].size(); j++) {
// only last charater could have an argument
if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos
&& j+1 == _argv[i].size()) {
if (checkOption(_argv[i].substr(j,1), _argv[i+1]) == false)
printHelp();
}
else {
if (checkOption(_argv[i].substr(j,1), "") == false)
printHelp();
}
}
}
}
// check command
if (m_needCommand == true) {
for (int i = 1; i < argc; i++) {
if (_argv[i].rfind("-", 0) != std::string::npos) {
i++;
continue;
}
m_arguments.push_back(_argv[i]);
}
if (m_arguments.size() == 0) {
std::cerr << std::endl << "command needed" << std::endl;
printHelp();
}
}
}
bool Appl::checkOption(const std::string& option, const std::string& value)
{
if (strcmp(option.c_str(), "settings") == 0)
printSettings();
if (strcmp(option.c_str(), "v") == 0 || strcmp(option.c_str(), "version") == 0)
printVersion();
if (strcmp(option.c_str(), "h") == 0 || strcmp(option.c_str(), "help") == 0)
printHelp();
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
if (o_it->shortname == option || o_it->name == option) {
// need this option and argument?
if (o_it->optiontype == ot_mandatory && value.size() == 0) {
std::cerr << std::endl << "option requires an argument '"
<< option << "'" << std::endl;
return false;
}
// add given value to option
if ((o_it->optiontype == ot_optional && value.size() != 0)
|| o_it->optiontype != ot_optional)
setOptVal(o_it->name, value, o_it->datatype);
return true;
}
}
std::cerr << std::endl << "unknown option '" << option << "'" << std::endl;
return false;
}
void Appl::setOptVal(const char* option, const std::string value, DataType datatype)
{
switch (datatype) {
case dt_bool:
m_optvals[option] = true;
break;
case dt_int:
m_optvals[option] = strtol(value.c_str(), NULL, 10);
break;
case dt_long:
m_optvals[option] = strtol(value.c_str(), NULL, 10);
break;
case dt_float:
m_optvals[option] = static_cast<float>(strtod(value.c_str(), NULL));
break;
case dt_string:
m_optvals[option] = value.c_str();
break;
default:
break;
}
}
void Appl::printVersion()
@@ -75,163 +191,60 @@ void Appl::printHelp()
std::cerr << std::endl << "Usage:" << std::endl << " "
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ;
if (m_argTxt.size() != 0)
std::cerr << " " << m_argTxt;
if (m_needCommand == true)
std::cerr << " COMMAND {ARGS...}" << std::endl << std::endl;
else
std::cerr << std::endl << std::endl;
std::cerr << std::endl << std::endl << "Options:" << std::endl << std::endl;
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " ";
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
<< " | --" << a_it->longname
<< "\t" << a_it->description
<< std::endl;
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
if (strcmp(o_it->name, "__text_only__") == 0)
continue;
else {
const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " ";
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
<< " | --" << o_it->name
<< "\t" << o_it->description
<< std::endl;
}
}
std::cerr << "-v | --version\n-h | --help" << std::endl << std::endl;
std::cerr << " | --settings\n-v | --version\n-h | --help" << std::endl << std::endl;
exit(EXIT_SUCCESS);
}
void Appl::parseArgs(int argc, char* argv[])
{
std::vector<std::string> _argv(argv, argv + argc);
m_argc = argc;
m_argv = _argv;
for (size_t i = 1; i < m_argc; i++) {
// find option with long format '--'
if (m_argv[i].rfind("--") == 0 && m_argv[i].size() > 2) {
// is next item an added argument?
if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos) {
if (checkArg(m_argv[i].substr(2), m_argv[i+1]) == false)
printHelp();
} else {
if (checkArg(m_argv[i].substr(2), "") == false)
printHelp();
}
// find option with short format '-'
} else if (m_argv[i].rfind("-") == 0 && m_argv[i].size() > 1) {
// walk through all characters
for (size_t j = 1; j < m_argv[i].size(); j++) {
// only last charater could have an argument
if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos
&& j+1 == m_argv[i].size()) {
if (checkArg(m_argv[i].substr(j,1), m_argv[i+1]) == false)
printHelp();
} else {
if (checkArg(m_argv[i].substr(j,1), "") == false)
printHelp();
}
}
}
}
// check args
if (m_argNum > 0) {
if (m_argc < (m_argNum + 1))
printHelp();
for (size_t i = 1; i < m_argc; i++) {
if (m_argv[i].rfind("-", 0) != std::string::npos) {
i++;
continue;
}
m_argValues.push_back(m_argv[i]);
}
if (m_argValues.size() < m_argNum)
printHelp();
}
}
void Appl::printSettings()
{
std::cerr << std::endl << "Settings:" << std::endl;
std::cerr << std::endl << "Settings:" << std::endl << std::endl;
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " ";
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
if (strcmp(o_it->name, "__text_only__") == 0)
continue;
const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " ";
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
<< " | --" << a_it->longname
<< " | --" << o_it->name
<< " = ";
if (a_it->datatype == type_bool) {
if (getParam<bool>(a_it->name) == true)
if (o_it->datatype == dt_bool) {
if (getOptVal<bool>(o_it->name) == true)
std::cerr << "yes" << std::endl;
else
std::cerr << "no" << std::endl;
}
else if (a_it->datatype == type_int) {
std::cerr << getParam<int>(a_it->name) << std::endl;
else if (o_it->datatype == dt_int) {
std::cerr << getOptVal<int>(o_it->name) << std::endl;
}
else if (a_it->datatype == type_long) {
std::cerr << getParam<long>(a_it->name) << std::endl;
else if (o_it->datatype == dt_long) {
std::cerr << getOptVal<long>(o_it->name) << std::endl;
}
else if (a_it->datatype == type_float) {
std::cerr << getParam<float>(a_it->name) << std::endl;
else if (o_it->datatype == dt_float) {
std::cerr << getOptVal<float>(o_it->name) << std::endl;
}
else if (a_it->datatype == type_string) {
std::cerr << getParam<const char*>(a_it->name) << std::endl;
else if (o_it->datatype == dt_string) {
std::cerr << getOptVal<const char*>(o_it->name) << std::endl;
}
}
std::cerr << std::endl;
exit(EXIT_SUCCESS);
}
bool Appl::checkArg(const std::string& name, const std::string& arg)
{
if (strcmp(name.c_str(), "v") == 0 || strcmp(name.c_str(), "version") == 0)
printVersion();
if (strcmp(name.c_str(), "h") == 0 || strcmp(name.c_str(), "help") == 0)
printHelp();
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
if (a_it->shortname == name || a_it->longname == name) {
if (a_it->optiontype == opt_mandatory && arg.size() == 0) {
std::cerr << std::endl << "option requires an argument '"
<< name << "'" << std::endl;
return false;
}
if ((a_it->optiontype == opt_optional && arg.size() != 0)
|| a_it->optiontype != opt_optional)
addParam(a_it->name, arg, a_it->datatype);
return true;
}
}
std::cerr << std::endl << "unknown option '" << name << "'" << std::endl;
return false;
}
void Appl::addParam(const char* name, const std::string arg, Datatype datatype)
{
switch (datatype) {
case type_bool:
m_params[name] = true;
break;
case type_int:
m_params[name] = strtol(arg.c_str(), NULL, 10);
break;
case type_long:
m_params[name] = strtol(arg.c_str(), NULL, 10);
break;
case type_float:
m_params[name] = static_cast<float>(strtod(arg.c_str(), NULL));
break;
case type_string:
m_params[name] = arg.c_str();
break;
default:
break;
}
}
+147 -61
View File
@@ -25,93 +25,179 @@
#include <map>
#include <vector>
/** the available data types. */
enum DataType {
dt_none, // default value
dt_bool, //
dt_int, //
dt_long, //
dt_float, //
dt_string, //
};
/** option types. */
enum OptionType {
ot_none, // default value
ot_optional, // a value is optional
ot_mandatory, // a value is mandatory
};
/** structure for defining application options */
typedef struct opt {
const char* name;
const char* shortname;
const char* description;
DataType datatype;
OptionType optiontype;
} opt_t;
/** union for option values */
union OptVal {
bool b;
int i;
long l;
float f;
const char* c;
OptVal() { memset(this, 0, sizeof(OptVal)); }
OptVal(bool _b) : b(_b) {}
OptVal(int _i) : i(_i) {}
OptVal(long _l) : l(_l) {}
OptVal(float _f) : f(_f) {}
OptVal(const char* _c) : c(_c) {}
};
/**
* @brief class for all kinds of application parameters.
*/
class Appl
{
private:
struct Option;
public:
enum Datatype { type_none, type_bool, type_int, type_long, type_float, type_string };
enum Optiontype { opt_none, opt_optional, opt_mandatory };
union Param {
bool b;
int i;
long l;
float f;
const char* c;
Param() { memset(this, 0, sizeof(Param)); }
Param(bool _b) : b(_b) {}
Param(int _i) : i(_i) {}
Param(long _l) : l(_l) {}
Param(float _f) : f(_f) {}
Param(const char* _c) : c(_c) {}
};
template <typename T>
T getParam(const char* name)
{
p_it = m_params.find(name);
return (reinterpret_cast<T&>(p_it->second));
}
static Appl& Instance();
/**
* @brief create an Appl instance and return the reference.
* @param command is true if an command is needed.
* @return the reference to instance.
*/
static Appl& Instance(const bool command=false);
/**
* @brief destructor.
*/
~Appl();
void addArgs(const std::string argTxt, const int argNum);
size_t numArg() const { return m_argValues.size(); }
std::string getArg(const int argNum) const { return m_argValues[argNum]; }
/**
* @brief save application version string.
* @param version string
*/
void setVersion(const char* version) { m_version = version; }
void addItem(const char* name, Param param, const char* shortname,
const char* longname, const char* description,
Datatype datatype, Optiontype optiontype);
/**
* @brief create new entry of application option only for help page.
* @param text string to print
*/
void addText(const char* text);
void addVersion(const char* version);
/**
* @brief create new entry of application option.
* @param name the long name
* @param shortname optional short name
* @param optval value of option
* @param datatype data type of option value
* @param optiontype type of given option
* @param description hint text for help page
*/
void addOption(const char* name, const char* shortname, OptVal optval,
DataType datatype, OptionType optiontype, const char* description);
/**
* @brief returns the value of the interested option
* @param the interested option
* @return casted value
*/
template <typename T>
T getOptVal(const char* name)
{
ov_it = m_optvals.find(name);
return (reinterpret_cast<T&>(ov_it->second));
}
/**
* @brief parse application arguments.
* @param argc the number of options
* @param argv the given options
*/
void parseArgs(int argc, char* argv[]);
void printSettings();
/**
* @brief returns the number of saved commands and arguments
* @return number of commands and arguments
*/
int numArgs() const { return m_arguments.size(); }
/**
* @brief returns the string of an interested argument (0 = command)
* @param num number of interested argument
* @return string value
*/
std::string getArg(const int num) const { return m_arguments[num]; }
private:
Appl() {}
/** private constructor - singleton pattern*/
Appl(const bool command) : m_needCommand(command) {}
Appl(const Appl&);
Appl& operator= (const Appl&);
Appl& operator=(const Appl&);
struct Arg {
const char* name;
const char* shortname;
const char* longname;
const char* description;
Datatype datatype;
Optiontype optiontype;
};
/** application options */
std::vector<opt_t> m_opts;
std::vector<opt_t>::const_iterator o_it;
size_t m_argc;
/** map option - value */
std::map<const char*, OptVal> m_optvals;
std::map<const char*, OptVal>::iterator ov_it;
/** given arguments */
std::vector<std::string> m_argv;
std::vector<Arg> m_args;
std::vector<Arg>::const_iterator a_it;
std::map<const char*, Param> m_params;
std::map<const char*, Param>::iterator p_it;
std::string m_argTxt;
size_t m_argNum;
/** application version string */
const char* m_version;
std::vector<std::string> m_argValues;
/** true if the application need a command */
bool m_needCommand;
bool checkArg(const std::string& name, const std::string& arg);
/** arguments (argument 0 = command) string */
std::vector<std::string> m_arguments;
void addParam(const char* name, Param param) { m_params[name] = param; }
/**
* @brief checks the passed parameter if this is a valid option.
* @param option to check
* @param value to save if paramter is a valid option
*/
bool checkOption(const std::string& option, const std::string& value);
void addParam(const char* name, const std::string arg, Datatype datatype);
/**
* @brief save the passed value to option.
* @param option name
* @param value to save
* @param datatype of given option
*/
void setOptVal(const char* option, const std::string value, DataType datatype);
/**
* @brief print application version.
*/
void printVersion();
/**
* @brief print help page.
*/
void printHelp();
/**
* @brief print used option settings.
*/
void printSettings();
};
#endif // LIBUTILS_APPL_H_