command_parser.cpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* MIT License */
3 /* */
4 /* Copyright (c) 2021 Analog Devices, Inc. */
5 /* */
6 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
7 /* of this software and associated documentation files (the "Software"), to deal */
8 /* in the Software without restriction, including without limitation the rights */
9 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
10 /* copies of the Software, and to permit persons to whom the Software is */
11 /* furnished to do so, subject to the following conditions: */
12 /* */
13 /* The above copyright notice and this permission notice shall be included in all */
14 /* copies or substantial portions of the Software. */
15 /* */
16 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
17 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
18 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
19 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
20 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
21 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
22 /* SOFTWARE. */
23 /**********************************************************************************/
24 
25 #include "command_parser.h"
26 
28  const std::string &command, const std::string &value,
29  std::vector<std::pair<std::string, std::string>> &m_command_vector) {
30  m_command_vector.push_back({command, value});
31 }
32 
34  const int &argc, const std::map<std::string, struct Argument> &command_map,
35  std::vector<std::pair<std::string, int>> &arg_position) {
36  for (auto ct = command_map.begin(); ct != command_map.end(); ct++) {
37  if (ct->second.is_mandatory == true) {
38  if (ct->second.position == "last") {
39  arg_position.push_back({ct->first, argc - 1});
40  } else {
41  arg_position.push_back(
42  {ct->first, std::stoi(ct->second.position)});
43  }
44  }
45  }
46 }
47 
49  return arg.find("-h") != std::string::npos ||
50  arg.find("--help") != std::string::npos;
51 }
52 
54  return arg.find("=") != std::string::npos;
55 }
56 
58  return (arg.substr(0, 2) == "--") || (arg.substr(0, 1) == "-");
59  //return arg.find("-") != std::string::npos;
60 }
61 
63  const std::string &arg, int &arg_number,
64  std::vector<std::pair<std::string, std::string>> &m_command_vector) {
65  addCommand(arg, "true", m_command_vector);
66  arg_number++;
67 }
68 
70  const std::string &arg, int &arg_number, const int &euqal_pos,
71  std::vector<std::pair<std::string, std::string>> &m_command_vector) {
72  addCommand(arg.substr(0, euqal_pos), arg.substr(euqal_pos + 1),
74  arg_number++;
75 }
76 
78  const std::string &arg, const std::string &value, int &arg_number,
79  std::vector<std::pair<std::string, std::string>> &m_command_vector) {
81  arg_number += 2;
82 }
83 
85  const std::string &arg, int &arg_number,
86  std::vector<std::pair<std::string, std::string>> &m_command_vector,
87  const std::map<std::string, struct Argument> &command_map) {
88  bool arg_found = false;
89  for (const auto &entry : command_map) {
90  const Argument &argument = entry.second;
91  if (argument.long_option == arg) {
92  arg_found = true;
93  if (argument.has_value) {
94  addCommand(arg, "", m_command_vector);
95  arg_number++;
96  } else {
97  processFlag(arg, arg_number, m_command_vector);
98  }
99  break;
100  }
101  }
102  if (!arg_found) {
103  addCommand(arg, "", m_command_vector);
104  arg_number++;
105  }
106 }
107 
109  const std::string &arg, int &arg_number,
110  std::vector<std::pair<std::string, std::string>> &m_command_vector,
111  const std::map<std::string, struct Argument> &command_map) {
112  auto command = command_map.find(arg);
113  if (command != command_map.end()) {
114  if (command->second.has_value) {
115  addCommand(arg, "", m_command_vector);
116  arg_number++;
117  } else {
118  processFlag(arg, arg_number, m_command_vector);
119  }
120  } else {
121  addCommand(arg, "", m_command_vector);
122  arg_number++;
123  }
124 }
125 
127  int argc, char *argv[],
128  std::map<std::string, struct Argument> command_map) {
129  int arg_number = 1;
130 
131  // Stores mandatory arguments and their location
132  std::vector<std::pair<std::string, int>> arg_position;
133 
134  getMandatoryArgs(argc, command_map, arg_position);
135 
136  for (int i = 1; i < argc; i++) {
137  bool mandatory = false;
138  int equal_pos = std::string(argv[i]).find("=");
139  for (int j = 0; j < arg_position.size(); j++) {
140  if (arg_number == arg_position[j].second && !isHelpArg(argv[i])) {
141  if (hasEqual(argv[i])) {
142  processEqualArg(argv[i], arg_number, equal_pos,
144  } else if (arg_number != argc - 1) {
145  processNonEqualArg(argv[i], argv[i + 1], arg_number,
147  i++;
148  } else {
149  addCommand(arg_position[j].first, argv[i],
151  }
152  mandatory = true;
153  break;
154  }
155  }
156  if (mandatory) {
157  continue;
158  }
159  if (isHelpArg(argv[i])) {
160  processFlag(argv[i], arg_number, m_command_vector);
161  continue;
162  }
163  if (hasEqual(argv[i])) { // Solves -arg/--arg=value
164  processEqualArg(argv[i], arg_number, equal_pos, m_command_vector);
165  continue;
166  }
167  if (i < argc - 1 && !hasEqual(argv[i]) &&
168  !nextArgIsCommand(argv[i + 1])) { // Solves -arg/--arg value
169  processNonEqualArg(argv[i], argv[i + 1], arg_number,
171  i++;
172  continue;
173  }
174  if (std::string(argv[i]).find("--") !=
175  -1) { // Solves -arg/--arg -arg value
176  processLongArg(argv[i], arg_number, m_command_vector, command_map);
177  } else {
178  processShorArg(argv[i], arg_number, m_command_vector, command_map);
179  }
180  }
181 }
182 
184  std::map<std::string, struct Argument> &command_map,
185  std::string &arg_error) {
186  // Check if arguments used exist
187  for (int i = 0; i < m_command_vector.size(); i++) {
188  bool is_command = false;
189  for (auto ct = command_map.begin(); ct != command_map.end(); ct++) {
190  if (m_command_vector[i].first == ct->first ||
191  m_command_vector[i].first == ct->second.long_option) {
192  is_command = true;
193  break;
194  }
195  }
196  if (!is_command) {
197  arg_error = m_command_vector[i].first;
198  return -1;
199  }
200  }
201  return 0;
202 }
203 
205  for (int i = 0; i < m_command_vector.size(); i++) {
206  if (m_command_vector[i].first == "-h" ||
207  m_command_vector[i].first == "--help") {
208  if (i != 0 || m_command_vector.size() != 1) {
209  return -1;
210  }
211  return 1;
212  }
213  }
214  return 0;
215 }
216 
218  std::map<std::string, struct Argument> &command_map,
219  std::string &arg_error) {
220  // Checks if argument has default and value assigned.
221  // If there is value assigned, it will send it
222  for (int i = 0; i < m_command_vector.size(); i++) {
223  for (auto ct = command_map.begin(); ct != command_map.end(); ct++) {
224  if (m_command_vector[i].first == ct->first ||
225  m_command_vector[i].first == ct->second.long_option) {
226  if (m_command_vector[i].second == "" &&
227  ct->second.value == "") {
228  arg_error = ct->first;
229  return -1;
230  } else if (m_command_vector[i].second == "" &&
231  ct->second.value != "") {
232  // Argument doesn't have value assigned but has default
233  break;
234  } else {
235  // Argument has value assigned
236  ct->second.value = m_command_vector[i].second;
237  break;
238  }
239  }
240  }
241  }
242  return 0;
243 }
244 
246  std::map<std::string, struct Argument> &command_map,
247  std::string &arg_error) {
248  // Check if mandatory arguments are provided
249  for (auto ct = command_map.begin(); ct != command_map.end(); ct++) {
250  if (ct->second.is_mandatory == true && ct->second.value == "") {
251  arg_error = ct->first;
252  return -1;
253  }
254  }
255  return 0;
256 }
257 
259  std::map<std::string, struct Argument> &command_map,
260  std::string &arg_error) {
261  // Mandatory arguments location check
262  for (auto ct = command_map.begin(); ct != command_map.end(); ct++) {
263  if (ct->second.is_mandatory == true) {
264  int index;
265  if (ct->second.position != "last") {
266  index = std::stoi(ct->second.position) - 1;
267  } else {
268  index = m_command_vector.size() - 1;
269  }
270  if (m_command_vector[index].first != ct->first &&
271  m_command_vector[index].first != ct->second.long_option) {
272  arg_error = ct->first;
273  return -1;
274  }
275  }
276  }
277  return 0;
278 }
CommandParser::processFlag
void processFlag(const std::string &arg, int &arg_number, std::vector< std::pair< std::string, std::string >> &m_command_vector)
Definition: command_parser.cpp:62
CommandParser::checkMandatoryArguments
int checkMandatoryArguments(std::map< std::string, struct Argument > &command_map, std::string &arg_error)
Definition: command_parser.cpp:245
CommandParser::helpMenu
int helpMenu()
Definition: command_parser.cpp:204
command
ROSLIB_DECL std::string command(const std::string &cmd)
CommandParser::nextArgIsCommand
bool nextArgIsCommand(const std::string &arg)
Definition: command_parser.cpp:57
command_parser.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
Argument::long_option
std::string long_option
Definition: command_parser.h:30
CommandParser::m_command_vector
std::vector< std::pair< std::string, std::string > > m_command_vector
Definition: command_parser.h:85
CommandParser::processShorArg
void processShorArg(const std::string &arg, int &arg_number, std::vector< std::pair< std::string, std::string >> &m_command_vector, const std::map< std::string, struct Argument > &command_map)
Definition: command_parser.cpp:108
CommandParser::addCommand
void addCommand(const std::string &command, const std::string &value, std::vector< std::pair< std::string, std::string >> &m_command_vector)
Definition: command_parser.cpp:27
CommandParser::checkMandatoryPosition
int checkMandatoryPosition(std::map< std::string, struct Argument > &command_map, std::string &arg_error)
Definition: command_parser.cpp:258
Argument
Definition: command_parser.h:29
CommandParser::parseArguments
void parseArguments(int argc, char *argv[], std::map< std::string, struct Argument > command_map)
Definition: command_parser.cpp:126
CommandParser::processEqualArg
void processEqualArg(const std::string &arg, int &arg_number, const int &euqal_pos, std::vector< std::pair< std::string, std::string >> &m_command_vector)
Definition: command_parser.cpp:69
CommandParser::hasEqual
bool hasEqual(const std::string &arg)
Definition: command_parser.cpp:53
CommandParser::processNonEqualArg
void processNonEqualArg(const std::string &arg, const std::string &value, int &arg_number, std::vector< std::pair< std::string, std::string >> &m_command_vector)
Definition: command_parser.cpp:77
i
int i
Definition: gmock-matchers_test.cc:764
CommandParser::getMandatoryArgs
void getMandatoryArgs(const int &argc, const std::map< std::string, struct Argument > &command_map, std::vector< std::pair< std::string, int >> &arg_position)
Definition: command_parser.cpp:33
CommandParser::isHelpArg
bool isHelpArg(const std::string &arg)
Definition: command_parser.cpp:48
CommandParser::checkValue
int checkValue(std::map< std::string, struct Argument > &command_map, std::string &arg_error)
Definition: command_parser.cpp:217
first
GLint first
Definition: glcorearb.h:2830
CommandParser::checkArgumentExist
int checkArgumentExist(std::map< std::string, struct Argument > &command_map, std::string &arg_error)
Definition: command_parser.cpp:183
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
CommandParser::processLongArg
void processLongArg(const std::string &arg, int &arg_number, std::vector< std::pair< std::string, std::string >> &m_command_vector, const std::map< std::string, struct Argument > &command_map)
Definition: command_parser.cpp:84
index
GLuint index
Definition: glcorearb.h:3055
Argument::has_value
bool has_value
Definition: command_parser.h:34


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48