Appl: ability to process a dash after a command added.

This commit is contained in:
Roland Jax
2014-12-14 08:54:18 +01:00
parent cbbca0b40a
commit d142756f35
4 changed files with 46 additions and 31 deletions
+32 -16
View File
@@ -24,7 +24,7 @@
using namespace std; using namespace std;
Appl& Appl::Instance(const bool command, const bool argument) Appl& Appl::Instance(const char* command, const char* argument)
{ {
static Appl instance(command, argument); static Appl instance(command, argument);
return instance; return instance;
@@ -68,9 +68,11 @@ void Appl::parseArgs(int argc, char* argv[])
{ {
vector<string> _argv(argv, argv + argc); vector<string> _argv(argv, argv + argc);
m_argv = _argv; m_argv = _argv;
int i;
bool lastOption = false;
// walk through all arguments // walk through all arguments
for (int i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
// find option with long format '--' // find option with long format '--'
if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) { if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) {
@@ -85,6 +87,8 @@ void Appl::parseArgs(int argc, char* argv[])
printHelp(); printHelp();
} }
lastOption = true;
// find option with short format '-' // find option with short format '-'
} else if (_argv[i].rfind("-") == 0 && _argv[i].size() > 1) { } else if (_argv[i].rfind("-") == 0 && _argv[i].size() > 1) {
@@ -102,22 +106,34 @@ void Appl::parseArgs(int argc, char* argv[])
printHelp(); printHelp();
} }
} }
lastOption = true;
} else {
// break loop with command
if (lastOption == false && strlen(m_withCommand) != 0)
break;
else
lastOption = false;
} }
} }
// check command if (i < argc && strlen(m_withCommand) != 0) {
for (int i = 1; i < argc; i++) { // 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]);
if (_argv[i].rfind("-", 0) != string::npos) {
i++;
continue;
} }
if (m_command.size() == 0)
m_command = _argv[i];
else
m_arguments.push_back(_argv[i]);
} }
} }
bool Appl::checkOption(const string& option, const string& value) bool Appl::checkOption(const string& option, const string& value)
@@ -189,13 +205,13 @@ void Appl::printVersion()
void Appl::printHelp() void Appl::printHelp()
{ {
cerr << endl << "Usage:" << endl << " " cerr << endl << "Usage:" << endl << " "
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ; << m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [Options...]" ;
if (m_withCommand == true) if (strlen(m_withCommand) != 0)
if (m_withArgument == true) if (strlen(m_withArgument) != 0)
cerr << " COMMAND {ARGS...}" << endl << endl; cerr << " " << m_withCommand << " " << m_withArgument << endl << endl;
else else
cerr << " COMMAND" << endl << endl; cerr << " " << m_withCommand << endl << endl;
else else
cerr << endl << endl; cerr << endl << endl;
+10 -10
View File
@@ -117,11 +117,11 @@ class Appl
public: public:
/** /**
* @brief create an instance and return the reference. * @brief create an instance and return the reference.
* @param command is true if an command is needed. * @param command string for help page.
* @param argument is true if command could have an argument. * @param argument string for help page.
* @return the reference to instance. * @return the reference to instance.
*/ */
static Appl& Instance(const bool command=false, const bool argument=false); static Appl& Instance(const char* command="", const char* argument="");
/** /**
* @brief destructor. * @brief destructor.
@@ -198,10 +198,10 @@ public:
private: private:
/** /**
* @brief private construtor. * @brief private construtor.
* @param command is true if an command is needed. * @param command string for help page.
* @param argument is true if command could have an argument. * @param argument string for help page.
*/ */
Appl(const bool command, const bool argument) Appl(const char* command, const char* argument)
: m_withCommand(command), m_withArgument(argument) {} : m_withCommand(command), m_withArgument(argument) {}
/** /**
@@ -235,11 +235,11 @@ private:
/** application version string */ /** application version string */
const char* m_version; const char* m_version;
/** true if the application could have a command */ /** command string for help page */
bool m_withCommand; const char* m_withCommand;
/** true if the command could have an argument */ /** argument string for help page */
bool m_withArgument; const char* m_withArgument;
/** command (argument 0 = command) string */ /** command (argument 0 = command) string */
string m_command; string m_command;
+2 -2
View File
@@ -29,14 +29,14 @@
using namespace std; using namespace std;
Appl& A = Appl::Instance(true, true); Appl& A = Appl::Instance("Command", "{Args...}");
void define_args() void define_args()
{ {
A.setVersion("ebusctl is part of """PACKAGE_STRING""); A.setVersion("ebusctl is part of """PACKAGE_STRING"");
A.addText(" 'ebusctl' is a tcp socket client for ebusd.\n\n" A.addText(" 'ebusctl' is a tcp socket client for ebusd.\n\n"
"Command: 'help' show available ebusd commands.\n\n" " hint: try 'help' for available ebusd commands.\n\n"
"Options:\n"); "Options:\n");
A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory, A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory,
+2 -3
View File
@@ -30,20 +30,19 @@
using namespace std; using namespace std;
Appl& A = Appl::Instance(true); Appl& A = Appl::Instance("/path/dumpfile");
void define_args() void define_args()
{ {
A.setVersion("ebusfeed is part of """PACKAGE_STRING""); A.setVersion("ebusfeed is part of """PACKAGE_STRING"");
A.addText(" 'ebusfeed' sends a dump file to a local serial device (pts)\n\n" A.addText(" 'ebusfeed' sends hex values from dump file to a local serial device (pts)\n\n"
" Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n" " Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n"
" 2. create symbol links to appropriate devices\n" " 2. create symbol links to appropriate devices\n"
" for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n" " for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n" " 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
" 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n" " 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n"
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n" " 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n"
"Command: '/path/to/ebus_dump.bin'\n\n"
"Options:\n"); "Options:\n");
A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory,