summaryrefslogtreecommitdiff
path: root/src/argparser.cc
blob: d8e45ae0f4d63c8b124a0143b1ce1995b13b874b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 *  @file argparser.cc
 *  @brief Implementation of the command line argument parser
 *  @author Andreas Aardal Hanssen
 *  @date 2005
 */

#include "argparser.h"

#include "convert.h"

#include <map>
#include <string>

using namespace Binc;
using std::string;

CommandLineArgs::CommandLineArgs()
{
  errString = "Unknown error for args";
  ac = 0;
}

bool CommandLineArgs::parse(int argc, char *argv[])
{
  ac = -1;
  head = argv[0];
  head += " <options> --\n";

  if (argc > 1) {
    string lastKey;
    bool lastIsBoolean = false;

    for (int i = 1; i < argc; ++i) {
      string s = argv[i];
      if (s.length() < 2) {
        unqualified.push_back(s);
        continue;
      }

      if (s[0] != '-') {
        // read value of last argument
        if (lastKey == "") {
          unqualified.push_back(s);
          continue;
        }

        if (lastIsBoolean && (s != "yes" && s != "no")) {
          errString = "syntax error: " + s;
          errString += " (expected yes or no)";
          return false;
        }

        args[lastKey] = s;
        passedArgs[lastKey] = true;
        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;
          }
        }

        // break if '--' is detected
        if (s.length() == 2) {
          ac = i + 1;
          break;
        }

        // parse --argument
        string arg = s.substr(2);
        string val;
        auto epos = arg.find('=');
        if (epos != string::npos) {
          val = arg.substr(epos + 1);
          arg = arg.substr(0, epos);
        }

        if (reg.find(arg) == reg.end()) {
          errString = "unrecognized argument: --" + arg;
          return false;
        }

        if (reg.find(arg)->second.b) {
          if (val != "" && val != "yes" && val != "no") {
            errString = "syntax error: " + val;
            errString += " (expected yes or no)";
            return false;
          } else if (val == "") {
            val = "yes";
          }
        }

        if (val == "") {
          errString = "syntax error: " + arg;
          errString += " (expected --" + arg + "=<str>)";
          return false;
        }

        args[arg] = val;
        passedArgs[arg] = true;

        lastKey = "";
        lastIsBoolean = false;
      } else {
        if (lastKey != "") {
          if (lastIsBoolean) {
            args[lastKey] = "yes";
            passedArgs[lastKey] = true;
            lastKey = "";
            lastIsBoolean = false;
          } else {
            errString = "expected value of ";
            errString += lastKey;
            return false;
          }
        }

        // parse -argument
        string arg = s.substr(1);
        if (arg.length() == 1) {
          bool match = false;
          for (const auto &[first, second] : reg) {
            if (second.c.find(arg[0]) != string::npos) {
              lastKey = first;
              if (second.b) lastIsBoolean = true;
              match = true;
              break;
            }
          }

          if (!match) {
            errString = "unrecognized argument: -";
            errString += arg[0];
            return false;
          }
        } else {
          for (auto its : arg) {
            bool match = false;
            for (const auto &[first, second] : reg) {
              if (second.c.find(its) != string::npos) {
                if (!second.b) {
                  errString = "argument is not a boolean: ";
                  errString += "--" + first;
                  errString += " / -";
                  errString += second.c;
                  return false;
                }

                match = true;
                args[first] = "yes";
                passedArgs[first] = true;

                lastKey = "";
                lastIsBoolean = false;
                break;
              }
            }

            if (!match) {
              errString = "unrecognized argument: ";
              errString += s;
              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.
  for (const auto &[first, second] : reg) {
    if (args.find(first) == args.end()) {
      if (!second.o) {
        errString = "missing argument: ";
        errString += first;
        return false;
      }
      if (second.b) args[first] = "no";
    }
  }
  if (ac == -1) ac = argc;

  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;

  string shorts;
  while (name.size() > 1 && name[1] == '|') {
    shorts += name[0];
    name = name.substr(2);
  }

  reg.insert(make_pair(name, ArgOpts(shorts, boolean, optional, desc)));
}

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';

  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;
      }
      tmp += ", ";
    } else {
      tmp += "      ";
    }

    tmp += "--" + first;
    if (!second.b) tmp += "=<str>";

    if (!second.o) tmp += " (required)";

    auto lineStart = tmp.rfind('\n');
    if (lineStart == string::npos) lineStart = 0;

    int pad = 21 - (tmp.length() - lineStart);
    if (pad < 0) {
      tmp += '\n';
      pad = 20;
    }

    tmp += string(pad, ' ') + second.desc + '\n';
  }

  tmp += '\n' + tail + '\n';

  return tmp;
}

int CommandLineArgs::argc(void) const
{
  return ac;
}

void CommandLineArgs::setTail(const string &str)
{
  tail = str;
}

const std::vector<string> &CommandLineArgs::getUnqualifiedArgs() const
{
  return unqualified;
}