class Appl: integrated 'version and help' options.
This commit is contained in:
+6
-16
@@ -17,9 +17,10 @@
|
||||
* along with ebusd. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "appl.h"
|
||||
#include "port.h"
|
||||
#include "decode.h"
|
||||
#include "appl.h"
|
||||
#include "tcpsocket.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
@@ -63,9 +64,7 @@ void define_args()
|
||||
"port (8888)\n",
|
||||
Appl::type_int, Appl::opt_mandatory);
|
||||
|
||||
A.addItem("p_help", Appl::Param(false), "h", "help",
|
||||
"print this message",
|
||||
Appl::type_bool, Appl::opt_none);
|
||||
A.addVersion("ebusctl is part of """PACKAGE_STRING"");
|
||||
}
|
||||
|
||||
|
||||
@@ -166,20 +165,11 @@ void scanVaillant(TCPSocket* socket, const std::string address)
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// define Arguments and Application variables
|
||||
// define arguments and application variables
|
||||
define_args();
|
||||
|
||||
// parse Arguments
|
||||
if (A.parseArgs(argc, argv) == false) {
|
||||
A.printArgs();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// print Help
|
||||
if (A.getParam<bool>("p_help") == true) {
|
||||
A.printArgs();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
// parse arguments
|
||||
A.parseArgs(argc, argv);
|
||||
|
||||
if (strcasecmp(A.getArg(0).c_str(), "feed") == 0) {
|
||||
std::string dev(A.getParam<const char*>("p_device"));
|
||||
|
||||
+10
-31
@@ -121,13 +121,7 @@ void define_args()
|
||||
"\tprint daemon settings\n",
|
||||
Appl::type_bool, Appl::opt_none);
|
||||
|
||||
A.addItem("p_version", Appl::Param(false), "v", "version",
|
||||
"\tprint ebusd version\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);
|
||||
A.addVersion(""PACKAGE_STRING"");
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
@@ -140,11 +134,11 @@ void shutdown()
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGTERM, SIG_DFL);
|
||||
|
||||
// delete Daemon pid file
|
||||
// delete daemon pid file
|
||||
if (D.status() == true)
|
||||
D.stop();
|
||||
|
||||
// stop Logger
|
||||
// stop logger
|
||||
L.log(bas, event, "ebusd stopped");
|
||||
L.stop();
|
||||
L.join();
|
||||
@@ -174,26 +168,11 @@ void signal_handler(int sig)
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// define Arguments and Application variables
|
||||
// define arguments and application variables
|
||||
define_args();
|
||||
|
||||
// parse Arguments
|
||||
if (A.parseArgs(argc, argv) == false) {
|
||||
A.printArgs();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// print version
|
||||
if (A.getParam<bool>("p_version") == true) {
|
||||
std::cerr << PACKAGE_STRING << std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
// print help
|
||||
if (A.getParam<bool>("p_help") == true) {
|
||||
A.printArgs();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
// parse arguments
|
||||
A.parseArgs(argc, argv);
|
||||
|
||||
// print daemon settings
|
||||
if (A.getParam<bool>("p_settings") == true)
|
||||
@@ -211,18 +190,18 @@ int main(int argc, char* argv[])
|
||||
"logfile", A.getParam<const char*>("p_logfile"));
|
||||
}
|
||||
|
||||
// trap Signals that we expect to receive
|
||||
// trap signals that we expect to receive
|
||||
signal(SIGHUP, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// start Logger
|
||||
// start logger
|
||||
L.start("logger");
|
||||
// wait for Logger be ready
|
||||
// wait for logger be ready
|
||||
usleep(100000);
|
||||
L.log(bas, event, "ebusd started");
|
||||
|
||||
// create BaseLoop
|
||||
// create baseloop
|
||||
baseloop = new BaseLoop();
|
||||
baseloop->start();
|
||||
|
||||
|
||||
+28
-12
@@ -59,7 +59,18 @@ void Appl::addItem(const char* name, Param param, const char* shortname,
|
||||
}
|
||||
}
|
||||
|
||||
void Appl::printArgs()
|
||||
void Appl::addVersion(const char* version)
|
||||
{
|
||||
m_version = version;
|
||||
}
|
||||
|
||||
void Appl::printVersion()
|
||||
{
|
||||
std::cerr << m_version << std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void Appl::printHelp()
|
||||
{
|
||||
std::cerr << std::endl << "Usage:" << std::endl << " "
|
||||
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ;
|
||||
@@ -77,10 +88,11 @@ void Appl::printArgs()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "-v | --version\n-h | --help" << std::endl << std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
bool Appl::parseArgs(int argc, char* argv[])
|
||||
void Appl::parseArgs(int argc, char* argv[])
|
||||
{
|
||||
std::vector<std::string> _argv(argv, argv + argc);
|
||||
m_argc = argc;
|
||||
@@ -93,10 +105,10 @@ bool Appl::parseArgs(int argc, char* argv[])
|
||||
// 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)
|
||||
return false;
|
||||
printHelp();
|
||||
} else {
|
||||
if (checkArg(m_argv[i].substr(2), "") == false)
|
||||
return false;
|
||||
printHelp();
|
||||
}
|
||||
|
||||
// find option with short format '-'
|
||||
@@ -109,10 +121,10 @@ bool Appl::parseArgs(int argc, char* argv[])
|
||||
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;
|
||||
printHelp();
|
||||
} else {
|
||||
if (checkArg(m_argv[i].substr(j,1), "") == false)
|
||||
return false;
|
||||
printHelp();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,7 +134,7 @@ bool Appl::parseArgs(int argc, char* argv[])
|
||||
// check args
|
||||
if (m_argNum > 0) {
|
||||
if (m_argc < (m_argNum + 1))
|
||||
return false;
|
||||
printHelp();
|
||||
|
||||
for (size_t i = 1; i < m_argc; i++) {
|
||||
|
||||
@@ -134,10 +146,8 @@ bool Appl::parseArgs(int argc, char* argv[])
|
||||
}
|
||||
|
||||
if (m_argValues.size() < m_argNum)
|
||||
return false;
|
||||
printHelp();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Appl::printSettings()
|
||||
@@ -175,6 +185,12 @@ void Appl::printSettings()
|
||||
|
||||
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) {
|
||||
@@ -191,7 +207,7 @@ bool Appl::checkArg(const std::string& name, const std::string& arg)
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << m_argv[0].substr(2) << ": Unknown Option -- " << name << std::endl;
|
||||
std::cerr << std::endl << "unknown option '" << name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,9 +69,9 @@ public:
|
||||
const char* longname, const char* description,
|
||||
Datatype datatype, Optiontype optiontype);
|
||||
|
||||
void printArgs();
|
||||
void addVersion(const char* version);
|
||||
|
||||
bool parseArgs(int argc, char* argv[]);
|
||||
void parseArgs(int argc, char* argv[]);
|
||||
void printSettings();
|
||||
|
||||
private:
|
||||
@@ -100,6 +100,8 @@ private:
|
||||
std::string m_argTxt;
|
||||
size_t m_argNum;
|
||||
|
||||
const char* m_version;
|
||||
|
||||
std::vector<std::string> m_argValues;
|
||||
|
||||
bool checkArg(const std::string& name, const std::string& arg);
|
||||
@@ -107,6 +109,9 @@ private:
|
||||
void addParam(const char* name, Param param) { m_params[name] = param; }
|
||||
|
||||
void addParam(const char* name, const std::string arg, Datatype datatype);
|
||||
|
||||
void printVersion();
|
||||
void printHelp();
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_APPL_H_
|
||||
|
||||
Reference in New Issue
Block a user