BUG: Appl::parseArgs searching for '-' in argument reduced to first character; Appl::printSettings added.
This commit is contained in:
+13
-6
@@ -85,7 +85,7 @@ void define_args()
|
|||||||
Appl::type_int, Appl::opt_mandatory);
|
Appl::type_int, Appl::opt_mandatory);
|
||||||
|
|
||||||
A.addItem("p_level", Appl::Param(trace), "", "loglevel",
|
A.addItem("p_level", Appl::Param(trace), "", "loglevel",
|
||||||
"\tlogging level (error=1, event=2, default: trace=3, debug=4)\n",
|
"\tlogging level (error=0, event=1, default: trace=2, debug=3)\n",
|
||||||
Appl::type_int, Appl::opt_mandatory);
|
Appl::type_int, Appl::opt_mandatory);
|
||||||
|
|
||||||
A.addItem("p_dump", Appl::Param(false), "D", "dump",
|
A.addItem("p_dump", Appl::Param(false), "D", "dump",
|
||||||
@@ -100,6 +100,10 @@ void define_args()
|
|||||||
"\tmax size for dump file in kB (default: 100)\n",
|
"\tmax size for dump file in kB (default: 100)\n",
|
||||||
Appl::type_long, Appl::opt_mandatory);
|
Appl::type_long, Appl::opt_mandatory);
|
||||||
|
|
||||||
|
A.addItem("p_settings", Appl::Param(false), "", "settings",
|
||||||
|
"\tprint daemon settings\n",
|
||||||
|
Appl::type_bool, Appl::opt_none);
|
||||||
|
|
||||||
A.addItem("p_help", Appl::Param(false), "h", "help",
|
A.addItem("p_help", Appl::Param(false), "h", "help",
|
||||||
"\tprint this message",
|
"\tprint this message",
|
||||||
Appl::type_bool, Appl::opt_none);
|
Appl::type_bool, Appl::opt_none);
|
||||||
@@ -171,7 +175,7 @@ int main(int argc, char* argv[])
|
|||||||
define_args();
|
define_args();
|
||||||
|
|
||||||
// parse Arguments
|
// parse Arguments
|
||||||
if (A.parse(argc, argv) == false) {
|
if (A.parseArgs(argc, argv) == false) {
|
||||||
A.printArgs();
|
A.printArgs();
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -182,6 +186,10 @@ int main(int argc, char* argv[])
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print Daemon settings
|
||||||
|
if (A.getParam<bool>("p_settings") == true)
|
||||||
|
A.printSettings();
|
||||||
|
|
||||||
// make me Daemon
|
// make me Daemon
|
||||||
if (A.getParam<bool>("p_foreground") == true) {
|
if (A.getParam<bool>("p_foreground") == true) {
|
||||||
L += new LogConsole(A.getParam<int>("p_area"),
|
L += new LogConsole(A.getParam<int>("p_area"),
|
||||||
@@ -201,14 +209,13 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
// start Logger
|
// start Logger
|
||||||
L.start("logInstance");
|
L.start("logInstance");
|
||||||
|
// wait for Logger be ready
|
||||||
|
usleep(100000);
|
||||||
L.log(bas, event, "ebus-daemon started");
|
L.log(bas, event, "ebus-daemon started");
|
||||||
|
|
||||||
// print Daemon status
|
|
||||||
if (D.status() == true)
|
|
||||||
L.log(bas, event, "change to daemon");
|
|
||||||
|
|
||||||
// create Commands DB
|
// create Commands DB
|
||||||
commands = ConfigCommands(A.getParam<const char*>("p_ebusconfdir"), CSV).getCommands();
|
commands = ConfigCommands(A.getParam<const char*>("p_ebusconfdir"), CSV).getCommands();
|
||||||
|
L.log(bas, debug, "ebus configuration dir: %s", A.getParam<const char*>("p_ebusconfdir"));
|
||||||
L.log(bas, event, "commands DB with %d entries created", commands->size());
|
L.log(bas, event, "commands DB with %d entries created", commands->size());
|
||||||
|
|
||||||
// create EBusLoop
|
// create EBusLoop
|
||||||
|
|||||||
+1
-1
@@ -55,7 +55,7 @@ int main(int argc, char* argv[])
|
|||||||
define_args();
|
define_args();
|
||||||
|
|
||||||
// parse Arguments
|
// parse Arguments
|
||||||
if (A.parse(argc, argv) == false) {
|
if (A.parseArgs(argc, argv) == false) {
|
||||||
A.printArgs();
|
A.printArgs();
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
+50
-19
@@ -58,7 +58,7 @@ void Appl::printArgs()
|
|||||||
std::cerr << std::endl << "Usage:" << std::endl << " "
|
std::cerr << std::endl << "Usage:" << std::endl << " "
|
||||||
<< m_argv[0].substr(2) << " [Options]" << std::endl
|
<< m_argv[0].substr(2) << " [Options]" << std::endl
|
||||||
<< std::endl << "Options:" << std::endl;
|
<< std::endl << "Options:" << std::endl;
|
||||||
|
|
||||||
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
|
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
|
||||||
const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " ";
|
const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " ";
|
||||||
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
|
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
|
||||||
@@ -70,53 +70,84 @@ void Appl::printArgs()
|
|||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Appl::parse(int argc, char* argv[])
|
bool Appl::parseArgs(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
std::vector<std::string> _argv(argv, argv + argc);
|
std::vector<std::string> _argv(argv, argv + argc);
|
||||||
m_argc = argc;
|
m_argc = argc;
|
||||||
m_argv = _argv;
|
m_argv = _argv;
|
||||||
|
|
||||||
for (int i = 1; i < m_argc; i++) {
|
for (int i = 1; i < m_argc; i++) {
|
||||||
|
|
||||||
// find option with long format '--'
|
// find option with long format '--'
|
||||||
if (m_argv[i].rfind("--") == 0 && m_argv[i].size() > 2) {
|
if (m_argv[i].rfind("--") == 0 && m_argv[i].size() > 2) {
|
||||||
|
|
||||||
// is next item an added argument?
|
// is next item an added argument?
|
||||||
if (i+1 < m_argc && m_argv[i+1].rfind("-") == std::string::npos) {
|
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)
|
if (checkArg(m_argv[i].substr(2), m_argv[i+1]) == false)
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (checkArg(m_argv[i].substr(2), "") == false)
|
if (checkArg(m_argv[i].substr(2), "") == false)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find option with short format '-'
|
// find option with short format '-'
|
||||||
} else if (m_argv[i].rfind("-") == 0 && m_argv[i].size() > 1) {
|
} else if (m_argv[i].rfind("-") == 0 && m_argv[i].size() > 1) {
|
||||||
|
|
||||||
// walk through all characters
|
// walk through all characters
|
||||||
for (size_t j = 1; j < m_argv[i].size(); j++) {
|
for (size_t j = 1; j < m_argv[i].size(); j++) {
|
||||||
|
|
||||||
// only last charater could have an argument
|
// only last charater could have an argument
|
||||||
if (i+1 < m_argc && m_argv[i+1].rfind("-") == std::string::npos
|
if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos
|
||||||
&& j+1 == m_argv[i].size()) {
|
&& j+1 == m_argv[i].size()) {
|
||||||
if (checkArg(m_argv[i].substr(j,1), m_argv[i+1]) == false)
|
if (checkArg(m_argv[i].substr(j,1), m_argv[i+1]) == false)
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (checkArg(m_argv[i].substr(j,1), "") == false)
|
if (checkArg(m_argv[i].substr(j,1), "") == false)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Appl::checkArg(const std::string name, const std::string arg)
|
void Appl::printSettings()
|
||||||
|
{
|
||||||
|
std::cerr << std::endl << "Settings:" << 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
|
||||||
|
<< " = ";
|
||||||
|
if (a_it->datatype == type_bool) {
|
||||||
|
if (getParam<bool>(a_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 (a_it->datatype == type_long) {
|
||||||
|
std::cerr << getParam<long>(a_it->name) << std::endl;
|
||||||
|
}
|
||||||
|
else if (a_it->datatype == type_float) {
|
||||||
|
std::cerr << getParam<float>(a_it->name) << std::endl;
|
||||||
|
}
|
||||||
|
else if (a_it->datatype == type_string) {
|
||||||
|
std::cerr << getParam<const char*>(a_it->name) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Appl::checkArg(const std::string& name, const std::string& arg)
|
||||||
{
|
{
|
||||||
for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) {
|
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->shortname == name || a_it->longname == name) {
|
||||||
|
|
||||||
if (a_it->optiontype == opt_mandatory && arg.size() == 0) {
|
if (a_it->optiontype == opt_mandatory && arg.size() == 0) {
|
||||||
std::cerr << std::endl << "option requires an argument '"
|
std::cerr << std::endl << "option requires an argument '"
|
||||||
<< name << "'" << std::endl;
|
<< name << "'" << std::endl;
|
||||||
@@ -141,10 +172,10 @@ void Appl::addParam(const char* name, const std::string arg, Datatype datatype)
|
|||||||
case type_bool:
|
case type_bool:
|
||||||
m_params[name] = true;
|
m_params[name] = true;
|
||||||
break;
|
break;
|
||||||
case type_int:
|
case type_int:
|
||||||
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
||||||
break;
|
break;
|
||||||
case type_long:
|
case type_long:
|
||||||
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
||||||
break;
|
break;
|
||||||
case type_float:
|
case type_float:
|
||||||
|
|||||||
+8
-7
@@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
class Appl
|
class Appl
|
||||||
{
|
{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Option;
|
struct Option;
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Appl& Instance();
|
static Appl& Instance();
|
||||||
|
|
||||||
~Appl();
|
~Appl();
|
||||||
|
|
||||||
void addItem(const char* name, Param param, const char* shortname,
|
void addItem(const char* name, Param param, const char* shortname,
|
||||||
@@ -67,13 +67,14 @@ public:
|
|||||||
|
|
||||||
void printArgs();
|
void printArgs();
|
||||||
|
|
||||||
bool parse(int argc, char* argv[]);
|
bool parseArgs(int argc, char* argv[]);
|
||||||
|
void printSettings();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Appl() {}
|
Appl() {}
|
||||||
Appl(const Appl&);
|
Appl(const Appl&);
|
||||||
Appl& operator= (const Appl&);
|
Appl& operator= (const Appl&);
|
||||||
|
|
||||||
struct Arg {
|
struct Arg {
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* shortname;
|
const char* shortname;
|
||||||
@@ -82,17 +83,17 @@ private:
|
|||||||
Datatype datatype;
|
Datatype datatype;
|
||||||
Optiontype optiontype;
|
Optiontype optiontype;
|
||||||
};
|
};
|
||||||
|
|
||||||
int m_argc;
|
int m_argc;
|
||||||
std::vector<std::string> m_argv;
|
std::vector<std::string> m_argv;
|
||||||
|
|
||||||
std::vector<Arg> m_args;
|
std::vector<Arg> m_args;
|
||||||
std::vector<Arg>::const_iterator a_it;
|
std::vector<Arg>::const_iterator a_it;
|
||||||
|
|
||||||
std::map<const char*, Param> m_params;
|
std::map<const char*, Param> m_params;
|
||||||
std::map<const char*, Param>::iterator p_it;
|
std::map<const char*, Param>::iterator p_it;
|
||||||
|
|
||||||
bool checkArg(const std::string name, const std::string arg);
|
bool checkArg(const std::string& name, const std::string& arg);
|
||||||
|
|
||||||
void addParam(const char* name, Param param) { m_params[name] = param; }
|
void addParam(const char* name, Param param) { m_params[name] = param; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user