summaryrefslogtreecommitdiff
path: root/src/argparser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/argparser.cc')
-rw-r--r--src/argparser.cc90
1 files changed, 34 insertions, 56 deletions
diff --git a/src/argparser.cc b/src/argparser.cc
index 1382610..d8e45ae 100644
--- a/src/argparser.cc
+++ b/src/argparser.cc
@@ -1,9 +1,10 @@
-/** --------------------------------------------------------------------
+/**
* @file argparser.cc
* @brief Implementation of the command line argument parser
* @author Andreas Aardal Hanssen
* @date 2005
- * ------------------------------------------------------------------ **/
+ */
+
#include "argparser.h"
#include "convert.h"
@@ -11,17 +12,15 @@
#include <map>
#include <string>
-using namespace ::std;
using namespace Binc;
+using std::string;
-//----------------------------------------------------------------------
CommandLineArgs::CommandLineArgs()
{
errString = "Unknown error for args";
ac = 0;
}
-//----------------------------------------------------------------------
bool CommandLineArgs::parse(int argc, char *argv[])
{
ac = -1;
@@ -79,7 +78,7 @@ bool CommandLineArgs::parse(int argc, char *argv[])
// parse --argument
string arg = s.substr(2);
string val;
- string::size_type epos = arg.find('=');
+ auto epos = arg.find('=');
if (epos != string::npos) {
val = arg.substr(epos + 1);
arg = arg.substr(0, epos);
@@ -128,12 +127,11 @@ bool CommandLineArgs::parse(int argc, char *argv[])
// parse -argument
string arg = s.substr(1);
if (arg.length() == 1) {
- map<string, ArgOpts>::const_iterator it = reg.begin();
bool match = false;
- for (; it != reg.end(); ++it) {
- if (it->second.c.find(arg[0]) != string::npos) {
- lastKey = it->first;
- if (it->second.b) lastIsBoolean = true;
+ for (const auto &[first, second] : reg) {
+ if (second.c.find(arg[0]) != string::npos) {
+ lastKey = first;
+ if (second.b) lastIsBoolean = true;
match = true;
break;
}
@@ -145,23 +143,21 @@ bool CommandLineArgs::parse(int argc, char *argv[])
return false;
}
} else {
- string::const_iterator its = arg.begin();
- for (; its != arg.end(); ++its) {
- map<string, ArgOpts>::const_iterator it = reg.begin();
+ for (auto its : arg) {
bool match = false;
- for (; it != reg.end(); ++it) {
- if (it->second.c.find(*its) != string::npos) {
- if (!it->second.b) {
+ for (const auto &[first, second] : reg) {
+ if (second.c.find(its) != string::npos) {
+ if (!second.b) {
errString = "argument is not a boolean: ";
- errString += "--" + it->first;
+ errString += "--" + first;
errString += " / -";
- errString += it->second.c;
+ errString += second.c;
return false;
}
match = true;
- args[it->first] = "yes";
- passedArgs[it->first] = true;
+ args[first] = "yes";
+ passedArgs[first] = true;
lastKey = "";
lastIsBoolean = false;
@@ -192,15 +188,14 @@ bool CommandLineArgs::parse(int argc, char *argv[])
}
// 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) {
+ for (const auto &[first, second] : reg) {
+ if (args.find(first) == args.end()) {
+ if (!second.o) {
errString = "missing argument: ";
- errString += it->first;
+ errString += first;
return false;
}
- if (it->second.b) args[it->first] = "no";
+ if (second.b) args[first] = "no";
}
}
if (ac == -1) ac = argc;
@@ -208,32 +203,27 @@ bool CommandLineArgs::parse(int argc, char *argv[])
return true;
}
-//----------------------------------------------------------------------
string CommandLineArgs::errorString(void) const
{
return errString;
}
-//----------------------------------------------------------------------
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)
{
registerArg(arg, desc, boolean, true);
}
-//----------------------------------------------------------------------
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)
{
string name = arg;
@@ -247,26 +237,22 @@ void CommandLineArgs::registerArg(const string &arg, const string &desc, bool bo
reg.insert(make_pair(name, ArgOpts(shorts, boolean, optional, desc)));
}
-//----------------------------------------------------------------------
-bool CommandLineArgs::hasArg(const std::string &arg) const
+bool CommandLineArgs::hasArg(const string &arg) const
{
string tmp = arg;
lowercase(tmp);
return passedArgs.find(tmp) != passedArgs.end();
}
-//----------------------------------------------------------------------
string CommandLineArgs::usageString(void) const
{
string tmp = head;
tmp += '\n';
- map<string, ArgOpts>::const_iterator it = reg.begin();
- for (; it != reg.end(); ++it) {
- if (it->second.c != "") {
- string::const_iterator sit = it->second.c.begin();
- for (; sit != it->second.c.end(); ++sit) {
- if (sit != it->second.c.begin()) tmp += '\n';
+ for (const auto &[first, second] : reg) {
+ if (!second.c.empty()) {
+ for (auto sit = second.c.cbegin(); sit != second.c.cend(); ++sit) {
+ if (sit != second.c.begin()) tmp += '\n';
tmp += " -";
tmp += *sit;
}
@@ -275,13 +261,12 @@ string CommandLineArgs::usageString(void) const
tmp += " ";
}
- tmp += "--";
- tmp += it->first;
- if (!it->second.b) tmp += "=<str>";
+ tmp += "--" + first;
+ if (!second.b) tmp += "=<str>";
- if (!it->second.o) tmp += " (required)";
+ if (!second.o) tmp += " (required)";
- string::size_type lineStart = tmp.rfind('\n');
+ auto lineStart = tmp.rfind('\n');
if (lineStart == string::npos) lineStart = 0;
int pad = 21 - (tmp.length() - lineStart);
@@ -290,32 +275,25 @@ string CommandLineArgs::usageString(void) const
pad = 20;
}
- tmp += string(pad, ' ');
- tmp += it->second.desc;
- tmp += '\n';
+ tmp += string(pad, ' ') + second.desc + '\n';
}
- tmp += '\n';
- tmp += tail;
- tmp += '\n';
+ tmp += '\n' + tail + '\n';
return tmp;
}
-//----------------------------------------------------------------------
int CommandLineArgs::argc(void) const
{
return ac;
}
-//----------------------------------------------------------------------
void CommandLineArgs::setTail(const string &str)
{
tail = str;
}
-//----------------------------------------------------------------------
-const vector<string> &CommandLineArgs::getUnqualifiedArgs() const
+const std::vector<string> &CommandLineArgs::getUnqualifiedArgs() const
{
return unqualified;
}