Eclipse SUMO - Simulation of Urban MObility
OptionsParser.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Parses the command line arguments
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <iostream>
26 #include <cstring>
27 #include "Option.h"
28 #include "OptionsCont.h"
29 #include "OptionsParser.h"
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
37 bool
38 OptionsParser::parse(int argc, char** argv) {
39  bool ok = true;
40  for (int i = 1; i < argc;) {
41  try {
42  int add;
43  // try to set the current option
44  if (i < argc - 1) {
45  add = check(argv[i], argv[i + 1], ok);
46  } else {
47  add = check(argv[i], nullptr, ok);
48  }
49  i += add;
50  } catch (ProcessError& e) {
51  WRITE_ERROR("On processing option '" + std::string(argv[i]) + "':\n " + e.what());
52  i++;
53  ok = false;
54  }
55  }
56  return ok;
57 }
58 
59 
60 int
61 OptionsParser::check(const char* arg1, const char* arg2, bool& ok) {
62  // the first argument should be an option
63  // (only the second may be a free string)
64  if (!checkParameter(arg1)) {
65  ok = false;
66  return 1;
67  }
68 
70  // process not abbreviated switches
71  if (!isAbbreviation(arg1)) {
72  std::string tmp(arg1 + 2);
73  const std::string::size_type idx1 = tmp.find('=');
74  // check whether a parameter was submitted
75  if (idx1 != std::string::npos) {
76  ok &= oc.set(tmp.substr(0, idx1), tmp.substr(idx1 + 1));
77  } else {
78  if (arg2 == nullptr || (oc.isBool(convert(arg1 + 2)) && arg2[0] == '-')) {
79  ok &= oc.set(convert(arg1 + 2), "true");
80  } else {
81  ok &= oc.set(convert(arg1 + 2), convert(arg2));
82  return 2;
83  }
84  }
85  return 1;
86  }
87  // go through the abbreviated switches
88  for (int i = 1; arg1[i] != 0; i++) {
89  // set boolean switches
90  if (oc.isBool(convert(arg1[i]))) {
91  if (arg2 == nullptr || arg2[0] == '-' || arg1[i + 1] != 0) {
92  ok &= oc.set(convert(arg1[i]), "true");
93  } else {
94  ok &= oc.set(convert(arg1[i]), convert(arg2));
95  return 2;
96  }
97  // set non-boolean switches
98  } else {
99  // check whether the parameter comes directly after the switch
100  // and process if so
101  if (arg2 == nullptr || arg1[i + 1] != 0) {
102  ok &= processNonBooleanSingleSwitch(oc, arg1 + i);
103  return 1;
104  // process parameter following after a space
105  } else {
106  ok &= oc.set(convert(arg1[i]), convert(arg2));
107  // option name and attribute were in two arguments
108  return 2;
109  }
110  }
111  }
112  // all switches within the current argument were boolean switches
113  return 1;
114 }
115 
116 
117 bool
119  if (arg[1] == '=') {
120  if (strlen(arg) < 3) {
121  WRITE_ERROR("Missing value for parameter '" + std::string(arg).substr(0, 1) + "'.");
122  return false;
123  } else {
124  return oc.set(convert(arg[0]), std::string(arg + 2));
125  }
126  } else {
127  if (strlen(arg) < 2) {
128  WRITE_ERROR("Missing value for parameter '" + std::string(arg) + "'.");
129  return false;
130  } else {
131  return oc.set(convert(arg[0]), std::string(arg + 1));
132  }
133  }
134 }
135 
136 
137 bool
138 OptionsParser::checkParameter(const char* arg1) {
139  if (arg1[0] != '-') {
140  WRITE_ERROR("The parameter '" + std::string(arg1) + "' is not allowed in this context.\n Switch or parameter name expected.");
141  return false;
142  }
143  return true;
144 }
145 
146 
147 bool
148 OptionsParser::isAbbreviation(const char* arg1) {
149  return arg1[1] != '-';
150 }
151 
152 
153 std::string
154 OptionsParser::convert(const char* arg) {
155  std::string s(arg);
156  return s;
157 }
158 
159 
160 std::string
162  char buf[2];
163  buf[0] = abbr;
164  buf[1] = 0;
165  std::string s(buf);
166  return buf;
167 }
168 
169 
170 
171 /****************************************************************************/
172 
OptionsParser::processNonBooleanSingleSwitch
static bool processNonBooleanSingleSwitch(OptionsCont &oc, const char *arg)
Extracts the parameter directly attached to an option.
Definition: OptionsParser.cpp:118
OptionsParser::isAbbreviation
static bool isAbbreviation(const char *arg1)
returns the whether the given token is an abbreviation
Definition: OptionsParser.cpp:148
OptionsCont.h
OptionsCont::set
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
Definition: OptionsCont.cpp:241
MsgHandler.h
OptionsParser::check
static int check(const char *arg1, const char *arg2, bool &ok)
parses the previous arguments
Definition: OptionsParser.cpp:61
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
OptionsParser::parse
static bool parse(int argc, char **argv)
Parses the given command line arguments.
Definition: OptionsParser.cpp:38
OptionsParser.h
ProcessError
Definition: UtilExceptions.h:39
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
OptionsCont::isBool
bool isBool(const std::string &name) const
Returns the information whether the option is a boolean option.
Definition: OptionsCont.cpp:434
Option.h
OptionsParser::checkParameter
static bool checkParameter(const char *arg1)
Returns the whether the given token is an option.
Definition: OptionsParser.cpp:138
config.h
OptionsParser::convert
static std::string convert(const char *arg)
Converts char* to string.
Definition: OptionsParser.cpp:154
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283