SUMO - Simulation of Urban MObility
OptionsParser.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Parses the command line arguments
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <cstring>
35 #include "Option.h"
36 #include "OptionsCont.h"
37 #include "OptionsParser.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 bool
46 OptionsParser::parse(int argc, char** argv) {
47  bool ok = true;
48  for (int i = 1; i < argc;) {
49  try {
50  int add;
51  // try to set the current option
52  if (i < argc - 1) {
53  add = check(argv[i], argv[i + 1], ok);
54  } else {
55  add = check(argv[i], 0, ok);
56  }
57  i += add;
58  } catch (ProcessError& e) {
59  WRITE_ERROR("On processing option '" + std::string(argv[i]) + "':\n " + e.what());
60  i++;
61  ok = false;
62  }
63  }
64  return ok;
65 }
66 
67 
68 int
69 OptionsParser::check(const char* arg1, const char* arg2, bool& ok) {
70  // the first argument should be an option
71  // (only the second may be a free string)
72  if (!checkParameter(arg1)) {
73  ok = false;
74  return 1;
75  }
76 
78  // process not abbreviated switches
79  if (!isAbbreviation(arg1)) {
80  std::string tmp(arg1 + 2);
81  const std::string::size_type idx1 = tmp.find('=');
82  // check whether a parameter was submitted
83  if (idx1 != std::string::npos) {
84  ok &= oc.set(tmp.substr(0, idx1), tmp.substr(idx1 + 1));
85  } else {
86  if (arg2 == 0 || (oc.isBool(convert(arg1 + 2)) && arg2[0] == '-')) {
87  ok &= oc.set(convert(arg1 + 2), "true");
88  } else {
89  ok &= oc.set(convert(arg1 + 2), convert(arg2));
90  return 2;
91  }
92  }
93  return 1;
94  }
95  // go through the abbreviated switches
96  for (int i = 1; arg1[i] != 0; i++) {
97  // set boolean switches
98  if (oc.isBool(convert(arg1[i]))) {
99  if (arg2 == 0 || arg2[0] == '-' || arg1[i + 1] != 0) {
100  ok &= oc.set(convert(arg1[i]), "true");
101  } else {
102  ok &= oc.set(convert(arg1[i]), convert(arg2));
103  return 2;
104  }
105  // set non-boolean switches
106  } else {
107  // check whether the parameter comes directly after the switch
108  // and process if so
109  if (arg2 == 0 || arg1[i + 1] != 0) {
110  ok &= processNonBooleanSingleSwitch(oc, arg1 + i);
111  return 1;
112  // process parameter following after a space
113  } else {
114  ok &= oc.set(convert(arg1[i]), convert(arg2));
115  // option name and attribute were in two arguments
116  return 2;
117  }
118  }
119  }
120  // all switches within the current argument were boolean switches
121  return 1;
122 }
123 
124 
125 bool
127  if (arg[1] == '=') {
128  if (strlen(arg) < 3) {
129  WRITE_ERROR("Missing value for parameter '" + std::string(arg).substr(0, 1) + "'.");
130  return false;
131  } else {
132  return oc.set(convert(arg[0]), std::string(arg + 2));
133  }
134  } else {
135  if (strlen(arg) < 2) {
136  WRITE_ERROR("Missing value for parameter '" + std::string(arg) + "'.");
137  return false;
138  } else {
139  return oc.set(convert(arg[0]), std::string(arg + 1));
140  }
141  }
142 }
143 
144 
145 bool
146 OptionsParser::checkParameter(const char* arg1) {
147  if (arg1[0] != '-') {
148  WRITE_ERROR("The parameter '" + std::string(arg1) + "' is not allowed in this context.\n Switch or parameter name expected.");
149  return false;
150  }
151  return true;
152 }
153 
154 
155 bool
156 OptionsParser::isAbbreviation(const char* arg1) {
157  return arg1[1] != '-';
158 }
159 
160 
161 std::string
162 OptionsParser::convert(const char* arg) {
163  std::string s(arg);
164  return s;
165 }
166 
167 
168 std::string
170  char buf[2];
171  buf[0] = abbr;
172  buf[1] = 0;
173  std::string s(buf);
174  return buf;
175 }
176 
177 
178 
179 /****************************************************************************/
180 
static int check(const char *arg1, const char *arg2, bool &ok)
parses the previous arguments
static bool checkParameter(const char *arg1)
Returns the whether the given token is an option.
static bool processNonBooleanSingleSwitch(OptionsCont &oc, const char *arg)
Extracts the parameter directly attached to an option.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
static bool parse(int argc, char **argv)
Parses the given command line arguments.
bool isBool(const std::string &name) const
Returns the information whether the option is a boolean option.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
static bool isAbbreviation(const char *arg1)
returns the whether the given token is an abbreviation
A storage for options typed value containers)
Definition: OptionsCont.h:99
static std::string convert(const char *arg)
Converts char* to string.