commandline.cc 4.37 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @file:   commandline.cc
 * @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 *
 * Created on July 3, 2013, 2:49 PM
 */
#include <cstdlib>
8
#include <iostream>
9
10

#include "radixio/commandline.hh"
11
12
namespace radix {

13
14
15
CommandLine::CommandLine(int argc, char **argv)
    : mArgc(argc)
    , mArgv(argv) {
16
17
18
19
20
21
22
23
  this->mExecutable = std::string(argv[0]);
  for (int i = 1; i < argc; i++) {
    std::string temp = std::string(argv[i]);
    //
    // Check for '-' as first character
    //
    if (temp.size() > 0) {
      if (temp.at(0) == '-') {
24
        //
25
        // Check for flag=value
26
        //
27
28
        size_t index = temp.find('=');
        if (index == std::string::npos)  // we didn't find equal sign
29
        {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
          //
          // This is just a flag with empty value
          //
          this->mFlagToValues[temp] = std::string("");
        } else {
          //
          // We found equal sign, so we must divide into flag and value
          //
          std::string flag = temp.substr(0, index);
          //
          // Check to make sure empty string wasn't provided
          // flag=''

          std::string value = "";
          if (temp.size() - 1 != index) {
            value = temp.substr(index + 1, temp.size() - 1);
          }
          this->mFlagToValues.insert(std::make_pair(flag, value));
        }  // else
      }    // if = '-'
      else {
        mArgs.push_back(temp);
      }
    }  // if size > 0
  }    // loop
}  // constructor

CommandLine::CommandLine(const CommandLine &orig)
    : mArgc(orig.mArgc)
    , mArgv(orig.mArgv)
    , mFlagToValues(orig.mFlagToValues)
    , mArgs(orig.mArgs) {}

CommandLine::~CommandLine() {}

std::string CommandLine::toString() const {
  std::stringstream ss;
  ss << "executable: " << executable() << std::endl;
  ss << "options: " << std::endl;
  std::map<std::string, std::string>::const_iterator iter;
  for (iter = mFlagToValues.begin(); iter != mFlagToValues.end(); ++iter) {
    ss << iter->first << "=" << iter->second << std::endl;
  }
  ss << "arguments: " << std::endl;
  const std::vector<std::string> &args = arguments();
  for (size_t i = 0; i < args.size(); ++i) {
    ss << (i + 1) << ". " << args[i] << std::endl;
  }
  return ss.str();
79
80
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
bool CommandLine::exists(std::string flag) const {
  std::map<std::string, std::string>::const_iterator iter;
  iter = mFlagToValues.find(flag);
  if (iter == mFlagToValues.end()) return false;
  return true;
}  // exists

const std::vector<std::string> &CommandLine::arguments() const {
  return mArgs;
}  // arguments

int CommandLine::valueAsInt(std::string flag) const {
  std::map<std::string, std::string>::const_iterator iter;
  int result = 0;
  iter       = this->mFlagToValues.find(flag);
  if (iter == this->mFlagToValues.end()) return -1;
  try {
    result = std::atoi(iter->second.c_str());
  } catch (...) {
    return -1;
  }
  return result;
}  // value

float CommandLine::valueAsFloat(std::string flag) const {
  std::map<std::string, std::string>::const_iterator iter;
  float result = 0.0f;
  iter         = this->mFlagToValues.find(flag);
  if (iter == this->mFlagToValues.end()) return -1;
  try {
    result = std::atof(iter->second.c_str());
  } catch (...) {
    return -1.0f;
  }
  return result;
}  // valueAsFloat

std::string CommandLine::value(std::string flag) const {
  std::map<std::string, std::string>::const_iterator iter;
  iter = this->mFlagToValues.find(flag);
  if (iter == this->mFlagToValues.end()) return "";
  return iter->second;
}  // value

std::string CommandLine::value(std::string flag,
                               std::string default_value) const {
  std::map<std::string, std::string>::const_iterator iter;
  iter = this->mFlagToValues.find(flag);
  if (iter == this->mFlagToValues.end()) {
130
    return default_value;
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  } else {
    return iter->second;
  }
}  // value

std::string CommandLine::executable() const { return this->mExecutable; }

// specialize boolean so we can handle yes/true/on and no/false/off
template <>
bool CommandLine::valueAs(std::string flag, bool default_value) const {
  if (exists(flag)) {
    std::string sv = value(flag);
    if (sv.compare("yes") == 0 || sv.compare("true") == 0 ||
        sv.compare("on") == 0 || sv.compare("1") == 0)
      return true;
    if (sv.compare("no") == 0 || sv.compare("false") == 0 ||
        sv.compare("off") == 0 || sv.compare("0") == 0)
      return false;
  }
  return default_value;
151
152
}

153
}  // namespace radix