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