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);
|
||||
|
||||
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);
|
||||
|
||||
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",
|
||||
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",
|
||||
"\tprint this message",
|
||||
Appl::type_bool, Appl::opt_none);
|
||||
@@ -171,7 +175,7 @@ int main(int argc, char* argv[])
|
||||
define_args();
|
||||
|
||||
// parse Arguments
|
||||
if (A.parse(argc, argv) == false) {
|
||||
if (A.parseArgs(argc, argv) == false) {
|
||||
A.printArgs();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -182,6 +186,10 @@ int main(int argc, char* argv[])
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
// print Daemon settings
|
||||
if (A.getParam<bool>("p_settings") == true)
|
||||
A.printSettings();
|
||||
|
||||
// make me Daemon
|
||||
if (A.getParam<bool>("p_foreground") == true) {
|
||||
L += new LogConsole(A.getParam<int>("p_area"),
|
||||
@@ -201,14 +209,13 @@ int main(int argc, char* argv[])
|
||||
|
||||
// start Logger
|
||||
L.start("logInstance");
|
||||
// wait for Logger be ready
|
||||
usleep(100000);
|
||||
L.log(bas, event, "ebus-daemon started");
|
||||
|
||||
// print Daemon status
|
||||
if (D.status() == true)
|
||||
L.log(bas, event, "change to daemon");
|
||||
|
||||
// create Commands DB
|
||||
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());
|
||||
|
||||
// create EBusLoop
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ int main(int argc, char* argv[])
|
||||
define_args();
|
||||
|
||||
// parse Arguments
|
||||
if (A.parse(argc, argv) == false) {
|
||||
if (A.parseArgs(argc, argv) == false) {
|
||||
A.printArgs();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
+50
-19
@@ -58,7 +58,7 @@ void Appl::printArgs()
|
||||
std::cerr << std::endl << "Usage:" << std::endl << " "
|
||||
<< m_argv[0].substr(2) << " [Options]" << std::endl
|
||||
<< std::endl << "Options:" << 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
|
||||
@@ -70,53 +70,84 @@ void Appl::printArgs()
|
||||
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);
|
||||
m_argc = argc;
|
||||
m_argv = _argv;
|
||||
|
||||
|
||||
for (int 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("-") == 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)
|
||||
return false;
|
||||
} else {
|
||||
} else {
|
||||
if (checkArg(m_argv[i].substr(2), "") == 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) {
|
||||
|
||||
|
||||
// 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("-") == std::string::npos
|
||||
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)
|
||||
return false;
|
||||
} else {
|
||||
} else {
|
||||
if (checkArg(m_argv[i].substr(j,1), "") == false)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
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++) {
|
||||
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;
|
||||
@@ -141,10 +172,10 @@ void Appl::addParam(const char* name, const std::string arg, Datatype datatype)
|
||||
case type_bool:
|
||||
m_params[name] = true;
|
||||
break;
|
||||
case type_int:
|
||||
case type_int:
|
||||
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
||||
break;
|
||||
case type_long:
|
||||
case type_long:
|
||||
m_params[name] = strtol(arg.c_str(), NULL, 10);
|
||||
break;
|
||||
case type_float:
|
||||
|
||||
+8
-7
@@ -27,7 +27,7 @@
|
||||
|
||||
class Appl
|
||||
{
|
||||
|
||||
|
||||
private:
|
||||
struct Option;
|
||||
|
||||
@@ -58,7 +58,7 @@ public:
|
||||
}
|
||||
|
||||
static Appl& Instance();
|
||||
|
||||
|
||||
~Appl();
|
||||
|
||||
void addItem(const char* name, Param param, const char* shortname,
|
||||
@@ -67,13 +67,14 @@ public:
|
||||
|
||||
void printArgs();
|
||||
|
||||
bool parse(int argc, char* argv[]);
|
||||
bool parseArgs(int argc, char* argv[]);
|
||||
void printSettings();
|
||||
|
||||
private:
|
||||
Appl() {}
|
||||
Appl(const Appl&);
|
||||
Appl& operator= (const Appl&);
|
||||
|
||||
|
||||
struct Arg {
|
||||
const char* name;
|
||||
const char* shortname;
|
||||
@@ -82,17 +83,17 @@ private:
|
||||
Datatype datatype;
|
||||
Optiontype optiontype;
|
||||
};
|
||||
|
||||
|
||||
int m_argc;
|
||||
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;
|
||||
|
||||
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; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user