switched from Appl class to argp
This commit is contained in:
@@ -4,9 +4,7 @@ AM_CXXFLAGS = -fpic \
|
||||
|
||||
noinst_LIBRARIES = libutils.a
|
||||
|
||||
libutils_a_SOURCES = appl.cpp \
|
||||
appl.h \
|
||||
log.cpp \
|
||||
libutils_a_SOURCES = log.cpp \
|
||||
log.h \
|
||||
tcpsocket.cpp \
|
||||
tcpsocket.h \
|
||||
|
||||
@@ -1,277 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
||||
*
|
||||
* This file is part of ebusd.
|
||||
*
|
||||
* ebusd is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* ebusd is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ebusd. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "appl.h"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Appl& Appl::Instance(const char* command, const char* argument)
|
||||
{
|
||||
static Appl instance(command, argument);
|
||||
return instance;
|
||||
}
|
||||
|
||||
Appl::~Appl()
|
||||
{
|
||||
m_opts.clear();
|
||||
m_optvals.clear();
|
||||
}
|
||||
|
||||
void Appl::addText(const char* text)
|
||||
{
|
||||
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::addOption(const char* name, const char* shortname, OptVal optval,
|
||||
DataType datatype, OptionType optiontype, const char* description)
|
||||
{
|
||||
if (strlen(name) != 0) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
bool Appl::parseArgs(int argc, char* argv[])
|
||||
{
|
||||
vector<string> _argv(argv, argv + argc);
|
||||
m_argv = _argv;
|
||||
int i;
|
||||
bool lastOption = false;
|
||||
|
||||
// walk through all arguments
|
||||
for (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) == string::npos) {
|
||||
if (checkOption(_argv[i].substr(2), _argv[i+1]) == false)
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
if (checkOption(_argv[i].substr(2), "") == false)
|
||||
return false;
|
||||
}
|
||||
|
||||
lastOption = true;
|
||||
|
||||
// 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) == string::npos
|
||||
&& j+1 == _argv[i].size()) {
|
||||
if (checkOption(_argv[i].substr(j,1), _argv[i+1]) == false)
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
if (checkOption(_argv[i].substr(j,1), "") == false)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
lastOption = true;
|
||||
|
||||
|
||||
}
|
||||
else {
|
||||
// break loop with command
|
||||
if (lastOption == false && strlen(m_withCommand) != 0)
|
||||
break;
|
||||
else
|
||||
lastOption = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (i < argc && strlen(m_withCommand) != 0) {
|
||||
// save command
|
||||
m_command = _argv[i];
|
||||
|
||||
if (strlen(m_withArgument) != 0) {
|
||||
// save args of command
|
||||
for (++i; i < argc; i++)
|
||||
m_arguments.push_back(_argv[i]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Appl::checkOption(const string& option, const string& value)
|
||||
{
|
||||
if (strcmp(option.c_str(), "settings") == 0)
|
||||
return printSettings();
|
||||
|
||||
if (strcmp(option.c_str(), "version") == 0)
|
||||
return printVersion();
|
||||
|
||||
if (strcmp(option.c_str(), "h") == 0 || strcmp(option.c_str(), "help") == 0)
|
||||
return 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) {
|
||||
cerr << endl << "option requires an argument '"
|
||||
<< option << "'" << endl;
|
||||
return printHelp();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
cerr << endl << "unknown option '" << option << "'" << endl;
|
||||
return printHelp();
|
||||
}
|
||||
|
||||
void Appl::setOptVal(const char* option, const string value, DataType datatype)
|
||||
{
|
||||
switch (datatype) {
|
||||
case dt_bool:
|
||||
m_optvals[option] = true;
|
||||
break;
|
||||
case dt_hex:
|
||||
m_optvals[option] = strtol(value.c_str(), NULL, 16);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool Appl::printVersion()
|
||||
{
|
||||
cerr << m_version << endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Appl::printHelp()
|
||||
{
|
||||
cerr << endl << "Usage:" << endl << " "
|
||||
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [Options...]" ;
|
||||
|
||||
if (strlen(m_withCommand) != 0)
|
||||
if (strlen(m_withArgument) != 0)
|
||||
cerr << " " << m_withCommand << " " << m_withArgument << endl << endl;
|
||||
else
|
||||
cerr << " " << m_withCommand << endl << endl;
|
||||
else
|
||||
cerr << endl << endl;
|
||||
|
||||
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
|
||||
if (strcmp(o_it->name, "__text_only__") == 0)
|
||||
cerr << o_it->description << endl;
|
||||
else {
|
||||
const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " ";
|
||||
cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
|
||||
<< " | --" << o_it->name
|
||||
<< "\t" << o_it->description
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
cerr << endl << " | --settings\n | --version\n-h | --help" << endl << endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Appl::printSettings()
|
||||
{
|
||||
cerr << endl << "Settings:" << endl << endl;
|
||||
|
||||
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 : " ";
|
||||
cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
|
||||
<< " | --" << o_it->name
|
||||
<< " = ";
|
||||
if (o_it->datatype == dt_bool) {
|
||||
if (getOptVal<bool>(o_it->name) == true)
|
||||
cerr << "yes" << endl;
|
||||
else
|
||||
cerr << "no" << endl;
|
||||
}
|
||||
else if (o_it->datatype == dt_hex) {
|
||||
cerr << hex << getOptVal<int>(o_it->name) << dec << endl;
|
||||
}
|
||||
else if (o_it->datatype == dt_int) {
|
||||
cerr << getOptVal<int>(o_it->name) << endl;
|
||||
}
|
||||
else if (o_it->datatype == dt_long) {
|
||||
cerr << getOptVal<long>(o_it->name) << endl;
|
||||
}
|
||||
else if (o_it->datatype == dt_float) {
|
||||
cerr << getOptVal<float>(o_it->name) << endl;
|
||||
}
|
||||
else if (o_it->datatype == dt_string) {
|
||||
cerr << getOptVal<const char*>(o_it->name) << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cerr << endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,290 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
||||
*
|
||||
* This file is part of ebusd.
|
||||
*
|
||||
* ebusd is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* ebusd is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ebusd. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_APPL_H_
|
||||
#define LIBUTILS_APPL_H_
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
/** \file appl.h */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** the available data types. */
|
||||
enum DataType {
|
||||
dt_none, //!< default for __text_only__
|
||||
dt_bool, //!< boolean
|
||||
dt_hex, //!< hex integer
|
||||
dt_int, //!< dec integer
|
||||
dt_long, //!< long
|
||||
dt_float, //!< float
|
||||
dt_string //!< string
|
||||
};
|
||||
|
||||
/** option types. */
|
||||
enum OptionType {
|
||||
ot_none, //!< no option type is needed
|
||||
ot_optional, //!< a value is optional
|
||||
ot_mandatory //!< a value is mandatory
|
||||
};
|
||||
|
||||
/** structure for defining application options */
|
||||
typedef struct {
|
||||
const char* name; //!< long option name
|
||||
const char* shortname; //!< short option name
|
||||
const char* description; //!< description for this option
|
||||
DataType datatype; //!< data type for this option
|
||||
OptionType optiontype; //!< indicates whether an option takes an argument
|
||||
} opt_t;
|
||||
|
||||
/**
|
||||
* union for option values
|
||||
*/
|
||||
union OptVal {
|
||||
/** boolean */
|
||||
bool b;
|
||||
|
||||
/** integer */
|
||||
int i;
|
||||
|
||||
/** long */
|
||||
long l;
|
||||
|
||||
/** float */
|
||||
float f;
|
||||
|
||||
/** string */
|
||||
const char* c;
|
||||
|
||||
/**
|
||||
* clear memory
|
||||
*/
|
||||
OptVal() { memset(this, 0, sizeof(OptVal)); }
|
||||
|
||||
/**
|
||||
* create boolean type
|
||||
* @param _b the boolean
|
||||
*/
|
||||
OptVal(bool _b) : b(_b) {}
|
||||
|
||||
/**
|
||||
* create integer type
|
||||
* @param _i the integer
|
||||
*/
|
||||
OptVal(int _i) : i(_i) {}
|
||||
|
||||
/**
|
||||
* create long type
|
||||
* @param _l the long
|
||||
*/
|
||||
OptVal(long _l) : l(_l) {}
|
||||
|
||||
/**
|
||||
* create float type
|
||||
* @param _f the float
|
||||
*/
|
||||
OptVal(float _f) : f(_f) {}
|
||||
|
||||
/**
|
||||
* create string type
|
||||
* @param _c the string
|
||||
*/
|
||||
OptVal(const char* _c) : c(_c) {}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* class for all kinds of application parameters.
|
||||
*/
|
||||
class Appl
|
||||
{
|
||||
|
||||
public:
|
||||
/**
|
||||
* create an instance and return the reference.
|
||||
* @param command string for help page.
|
||||
* @param argument string for help page.
|
||||
* @return the reference to instance.
|
||||
*/
|
||||
static Appl& Instance(const char* command="", const char* argument="");
|
||||
|
||||
/**
|
||||
* destructor.
|
||||
*/
|
||||
~Appl();
|
||||
|
||||
/**
|
||||
* save application version string.
|
||||
* @param version string.
|
||||
*/
|
||||
void setVersion(const char* version) { m_version = version; }
|
||||
|
||||
/**
|
||||
* create new entry of application option only for help page.
|
||||
* @param text string to print.
|
||||
*/
|
||||
void addText(const char* text);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* returns the value of the interested option.
|
||||
* @param name 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* parse application arguments.
|
||||
* @param argc the number of options.
|
||||
* @param argv the given options.
|
||||
* @return false if the arguments are invalid or a help/error message was printed and the process should exit.
|
||||
*/
|
||||
bool parseArgs(int argc, char* argv[]);
|
||||
|
||||
/**
|
||||
* returns the number of saved commands and arguments.
|
||||
* @return number of commands and arguments.
|
||||
*/
|
||||
int numArgs() const { return m_arguments.size(); }
|
||||
/**
|
||||
* returns the string of an interested argument.
|
||||
* @param num number of interested argument.
|
||||
* @return the argument string.
|
||||
*/
|
||||
string getArg(const int num) const { return m_arguments[num]; }
|
||||
|
||||
/**
|
||||
* returns the string of given command.
|
||||
* @return the command string.
|
||||
*/
|
||||
string getCommand() const { return m_command; }
|
||||
|
||||
/**
|
||||
* Get whether a command string is missing.
|
||||
* @return true if a command string is missing.
|
||||
*/
|
||||
bool missingCommand() const { return (m_command.size() == 0 ? true : false); }
|
||||
|
||||
private:
|
||||
/**
|
||||
* private construtor.
|
||||
* @param command string for help page.
|
||||
* @param argument string for help page.
|
||||
*/
|
||||
Appl(const char* command, const char* argument)
|
||||
: m_version(NULL), m_withCommand(command), m_withArgument(argument) {}
|
||||
|
||||
/**
|
||||
* private copy construtor.
|
||||
* @param reference to an instance.
|
||||
*/
|
||||
Appl(const Appl&);
|
||||
|
||||
/**
|
||||
* private = operator.
|
||||
* @param reference to an instance.
|
||||
* @return reference to instance.
|
||||
*/
|
||||
Appl& operator=(const Appl&);
|
||||
|
||||
/** application options */
|
||||
vector<opt_t> m_opts;
|
||||
|
||||
/** application options iterator */
|
||||
vector<opt_t>::const_iterator o_it;
|
||||
|
||||
/** map option - value */
|
||||
map<const char*, OptVal> m_optvals;
|
||||
|
||||
/** map option - value iterator */
|
||||
map<const char*, OptVal>::iterator ov_it;
|
||||
|
||||
/** given arguments */
|
||||
vector<string> m_argv;
|
||||
|
||||
/** application version string */
|
||||
const char* m_version;
|
||||
|
||||
/** command string for help page */
|
||||
const char* m_withCommand;
|
||||
|
||||
/** argument string for help page */
|
||||
const char* m_withArgument;
|
||||
|
||||
/** command (argument 0 = command) string */
|
||||
string m_command;
|
||||
|
||||
/** arguments (argument >= 1) string */
|
||||
vector<string> m_arguments;
|
||||
|
||||
/**
|
||||
* checks the passed parameter if this is a valid option.
|
||||
* @param option to check.
|
||||
* @param value to save.
|
||||
* @return true if parameter is a valid option.
|
||||
*/
|
||||
bool checkOption(const string& option, const string& value);
|
||||
|
||||
/**
|
||||
* save the passed value to option.
|
||||
* @param option name.
|
||||
* @param value to save.
|
||||
* @param datatype of given option.
|
||||
*/
|
||||
void setOptVal(const char* option, const string value, DataType datatype);
|
||||
|
||||
/**
|
||||
* print application version.
|
||||
* @return false.
|
||||
*/
|
||||
bool printVersion();
|
||||
|
||||
/**
|
||||
* print help page.
|
||||
* @return false.
|
||||
*/
|
||||
bool printHelp();
|
||||
|
||||
/**
|
||||
* print used option settings.
|
||||
* @return false.
|
||||
*/
|
||||
bool printSettings();
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_APPL_H_
|
||||
Reference in New Issue
Block a user