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;
Appl& Appl::Instance(const bool command, const bool argument)
Appl& Appl::Instance(const char* command, const char* argument)
{
static Appl instance(command, argument);
return instance;
@@ -68,9 +68,11 @@ void 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 (int i = 1; i < argc; i++) {
for (i = 1; i < argc; i++) {
// find option with long format '--'
if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) {
@@ -85,6 +87,8 @@ void Appl::parseArgs(int argc, char* argv[])
printHelp();
}
lastOption = true;
// find option with short format '-'
} else if (_argv[i].rfind("-") == 0 && _argv[i].size() > 1) {
@@ -102,22 +106,34 @@ void Appl::parseArgs(int argc, char* argv[])
printHelp();
}
}
lastOption = true;
} else {
// break loop with command
if (lastOption == false && strlen(m_withCommand) != 0)
break;
else
lastOption = false;
}
}
// check command
for (int i = 1; i < argc; i++) {
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]);
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)
@@ -189,13 +205,13 @@ void Appl::printVersion()
void Appl::printHelp()
{
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 (m_withArgument == true)
cerr << " COMMAND {ARGS...}" << endl << endl;
if (strlen(m_withCommand) != 0)
if (strlen(m_withArgument) != 0)
cerr << " " << m_withCommand << " " << m_withArgument << endl << endl;
else
cerr << " COMMAND" << endl << endl;
cerr << " " << m_withCommand << endl << endl;
else
cerr << endl << endl;
+10 -10
View File
@@ -117,11 +117,11 @@ class Appl
public:
/**
* @brief create an instance and return the reference.
* @param command is true if an command is needed.
* @param argument is true if command could have an argument.
* @param command string for help page.
* @param argument string for help page.
* @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.
@@ -198,10 +198,10 @@ public:
private:
/**
* @brief private construtor.
* @param command is true if an command is needed.
* @param argument is true if command could have an argument.
* @param command string for help page.
* @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) {}
/**
@@ -235,11 +235,11 @@ private:
/** application version string */
const char* m_version;
/** true if the application could have a command */
bool m_withCommand;
/** command string for help page */
const char* m_withCommand;
/** true if the command could have an argument */
bool m_withArgument;
/** argument string for help page */
const char* m_withArgument;
/** command (argument 0 = command) string */
string m_command;
+2 -2
View File
@@ -29,14 +29,14 @@
using namespace std;
Appl& A = Appl::Instance(true, true);
Appl& A = Appl::Instance("Command", "{Args...}");
void define_args()
{
A.setVersion("ebusctl is part of """PACKAGE_STRING"");
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");
A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory,
+2 -3
View File
@@ -30,20 +30,19 @@
using namespace std;
Appl& A = Appl::Instance(true);
Appl& A = Appl::Instance("/path/dumpfile");
void define_args()
{
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"
" 2. create symbol links to appropriate devices\n"
" for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
" 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n"
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n"
"Command: '/path/to/ebus_dump.bin'\n\n"
"Options:\n");
A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory,