summaryrefslogtreecommitdiff
path: root/src/argparser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/argparser.cc')
-rw-r--r--src/argparser.cc81
1 files changed, 40 insertions, 41 deletions
diff --git a/src/argparser.cc b/src/argparser.cc
index c8b482c..1382610 100644
--- a/src/argparser.cc
+++ b/src/argparser.cc
@@ -5,10 +5,11 @@
* @date 2005
* ------------------------------------------------------------------ **/
#include "argparser.h"
+
#include "convert.h"
-#include <string>
#include <map>
+#include <string>
using namespace ::std;
using namespace Binc;
@@ -39,7 +40,7 @@ bool CommandLineArgs::parse(int argc, char *argv[])
}
if (s[0] != '-') {
- // read value of last argument
+ // read value of last argument
if (lastKey == "") {
unqualified.push_back(s);
continue;
@@ -56,32 +57,32 @@ bool CommandLineArgs::parse(int argc, char *argv[])
lastKey = "";
lastIsBoolean = false;
} else if (s[1] == '-') {
- if (lastKey != "") {
- if (lastIsBoolean) {
- args[lastKey] = "yes";
- passedArgs[lastKey] = true;
- lastKey = "";
- lastIsBoolean = false;
- } else {
- errString = "expected value of ";
- errString += lastKey;
- return false;
- }
+ if (lastKey != "") {
+ if (lastIsBoolean) {
+ args[lastKey] = "yes";
+ passedArgs[lastKey] = true;
+ lastKey = "";
+ lastIsBoolean = false;
+ } else {
+ errString = "expected value of ";
+ errString += lastKey;
+ return false;
}
+ }
- // break if '--' is detected
- if (s.length() == 2) {
- ac = i + 1;
- break;
+ // break if '--' is detected
+ if (s.length() == 2) {
+ ac = i + 1;
+ break;
}
- // parse --argument
+ // parse --argument
string arg = s.substr(2);
string val;
string::size_type epos = arg.find('=');
if (epos != string::npos) {
val = arg.substr(epos + 1);
- arg = arg.substr(0, epos);
+ arg = arg.substr(0, epos);
}
if (reg.find(arg) == reg.end()) {
@@ -124,7 +125,7 @@ bool CommandLineArgs::parse(int argc, char *argv[])
}
}
- // parse -argument
+ // parse -argument
string arg = s.substr(1);
if (arg.length() == 1) {
map<string, ArgOpts>::const_iterator it = reg.begin();
@@ -178,26 +179,26 @@ bool CommandLineArgs::parse(int argc, char *argv[])
}
}
- if (lastKey != "") {
- if (lastIsBoolean) {
- args[lastKey] = "yes";
- passedArgs[lastKey] = true;
- } else {
- errString = "expected value of ";
- errString += lastKey;
- return false;
+ if (lastKey != "") {
+ if (lastIsBoolean) {
+ args[lastKey] = "yes";
+ passedArgs[lastKey] = true;
+ } else {
+ errString = "expected value of ";
+ errString += lastKey;
+ return false;
+ }
}
}
- }
// assign default "no" values for arguments that were not passed.
map<string, ArgOpts>::const_iterator it = reg.begin();
for (; it != reg.end(); ++it) {
if (args.find(it->first) == args.end()) {
if (!it->second.o) {
- errString = "missing argument: ";
- errString += it->first;
- return false;
+ errString = "missing argument: ";
+ errString += it->first;
+ return false;
}
if (it->second.b) args[it->first] = "no";
}
@@ -214,29 +215,26 @@ string CommandLineArgs::errorString(void) const
}
//----------------------------------------------------------------------
-const string CommandLineArgs::operator [](const string &arg) const
+const string CommandLineArgs::operator[](const string &arg) const
{
if (args.find(arg) == args.end()) return "";
return args.find(arg)->second;
}
//----------------------------------------------------------------------
-void CommandLineArgs::addOptional(const string &arg, const string &desc,
- bool boolean)
+void CommandLineArgs::addOptional(const string &arg, const string &desc, bool boolean)
{
registerArg(arg, desc, boolean, true);
}
//----------------------------------------------------------------------
-void CommandLineArgs::addRequired(const string &arg,
- const string &desc, bool boolean)
+void CommandLineArgs::addRequired(const string &arg, const string &desc, bool boolean)
{
registerArg(arg, desc, boolean, false);
}
//----------------------------------------------------------------------
-void CommandLineArgs::registerArg(const string &arg, const string &desc,
- bool boolean, bool optional)
+void CommandLineArgs::registerArg(const string &arg, const string &desc, bool boolean, bool optional)
{
string name = arg;
@@ -252,7 +250,8 @@ void CommandLineArgs::registerArg(const string &arg, const string &desc,
//----------------------------------------------------------------------
bool CommandLineArgs::hasArg(const std::string &arg) const
{
- string tmp = arg; lowercase(tmp);
+ string tmp = arg;
+ lowercase(tmp);
return passedArgs.find(tmp) != passedArgs.end();
}
@@ -295,7 +294,7 @@ string CommandLineArgs::usageString(void) const
tmp += it->second.desc;
tmp += '\n';
}
-
+
tmp += '\n';
tmp += tail;
tmp += '\n';