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-2016 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 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
50 OptionsParser::parse(int argc, char** argv) {
51  bool ok = true;
52  for (int i = 1; i < argc;) {
53  try {
54  int add;
55  // try to set the current option
56  if (i < argc - 1) {
57  add = check(argv[i], argv[i + 1], ok);
58  } else {
59  add = check(argv[i], 0, ok);
60  }
61  i += add;
62  } catch (ProcessError& e) {
63  WRITE_ERROR("On processing option '" + std::string(argv[i]) + "':\n " + e.what());
64  i++;
65  ok = false;
66  }
67  }
68  return ok;
69 }
70 
71 
72 int
73 OptionsParser::check(const char* arg1, const char* arg2, bool& ok) {
74  // the first argument should be an option
75  // (only the second may be a free string)
76  if (!checkParameter(arg1)) {
77  ok = false;
78  return 1;
79  }
80 
82  // process not abbreviated switches
83  if (!isAbbreviation(arg1)) {
84  std::string tmp(arg1 + 2);
85  size_t idx1 = tmp.find('=');
86  // check whether a parameter was submitted
87  if (idx1 != std::string::npos) {
88  ok &= oc.set(tmp.substr(0, idx1), tmp.substr(idx1 + 1));
89  } else {
90  if (arg2 == 0 || (oc.isBool(convert(arg1 + 2)) && arg2[0] == '-')) {
91  ok &= oc.set(convert(arg1 + 2), "true");
92  } else {
93  ok &= oc.set(convert(arg1 + 2), convert(arg2));
94  return 2;
95  }
96  }
97  return 1;
98  }
99  // go through the abbreviated switches
100  for (int i = 1; arg1[i] != 0; i++) {
101  // set boolean switches
102  if (oc.isBool(convert(arg1[i]))) {
103  if (arg2 == 0 || arg2[0] == '-' || arg1[i + 1] != 0) {
104  ok &= oc.set(convert(arg1[i]), "true");
105  } else {
106  ok &= oc.set(convert(arg1[i]), convert(arg2));
107  return 2;
108  }
109  // set non-boolean switches
110  } else {
111  // check whether the parameter comes directly after the switch
112  // and process if so
113  if (arg2 == 0 || arg1[i + 1] != 0) {
114  ok &= processNonBooleanSingleSwitch(oc, arg1 + i);
115  return 1;
116  // process parameter following after a space
117  } else {
118  ok &= oc.set(convert(arg1[i]), convert(arg2));
119  // option name and attribute were in two arguments
120  return 2;
121  }
122  }
123  }
124  // all switches within the current argument were boolean switches
125  return 1;
126 }
127 
128 
129 bool
131  if (arg[1] == '=') {
132  if (strlen(arg) < 3) {
133  WRITE_ERROR("Missing value for parameter '" + std::string(arg).substr(0, 1) + "'.");
134  return false;
135  } else {
136  return oc.set(convert(arg[0]), std::string(arg + 2));
137  }
138  } else {
139  if (strlen(arg) < 2) {
140  WRITE_ERROR("Missing value for parameter '" + std::string(arg) + "'.");
141  return false;
142  } else {
143  return oc.set(convert(arg[0]), std::string(arg + 1));
144  }
145  }
146 }
147 
148 
149 bool
150 OptionsParser::checkParameter(const char* arg1) {
151  if (arg1[0] != '-') {
152  WRITE_ERROR("The parameter '" + std::string(arg1) + "' is not allowed in this context.\n Switch or parameter name expected.");
153  return false;
154  }
155  return true;
156 }
157 
158 
159 bool
160 OptionsParser::isAbbreviation(const char* arg1) {
161  return arg1[1] != '-';
162 }
163 
164 
165 std::string
166 OptionsParser::convert(const char* arg) {
167  std::string s(arg);
168  return s;
169 }
170 
171 
172 std::string
174  char buf[2];
175  buf[0] = abbr;
176  buf[1] = 0;
177  std::string s(buf);
178  return buf;
179 }
180 
181 
182 
183 /****************************************************************************/
184 
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:69
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:108
static std::string convert(const char *arg)
Converts char* to string.