Point Cloud Library (PCL)  1.7.0
parse.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 #ifndef PCL_CONSOLE_PARSE_H_
39 #define PCL_CONSOLE_PARSE_H_
40 
41 #include <vector>
42 #include <sstream>
43 #include <pcl/pcl_macros.h>
44 
45 namespace pcl
46 {
47  namespace console
48  {
49  /** \brief Finds whether the argument with name "argument_name" is in the argument list "argv".
50  * An example for a widely used switch argument is the "-r" flag for unix commands that indicates whether
51  * the command should run recursively or not.
52  * \param[in] argc the number of command line arguments
53  * \param[in] argv the command line arguments
54  * \param[in] argument_name the string value to search for
55  * \return true if argument was found, false otherwise
56  * \note find_switch is simply returning find_argument != -1.
57  */
58  PCL_EXPORTS bool
59  find_switch (int argc, char** argv, const char* argument_name);
60 
61  /** \brief Finds the position of the argument with name "argument_name" in the argument list "argv"
62  * \param[in] argc the number of command line arguments
63  * \param[in] argv the command line arguments
64  * \param[in] argument_name the string value to search for
65  * \return index of found argument or -1 of arguments does not appear in list
66  */
67  PCL_EXPORTS int
68  find_argument (int argc, char** argv, const char* argument_name);
69 
70  /** \brief Template version for parsing arguments. Template parameter needs to have input stream operator overloaded!
71  * \param[in] argc the number of command line arguments
72  * \param[in] argv the command line arguments
73  * \param[in] argument_name the name of the argument to search for
74  * \param[out] value The value of the argument
75  * \return index of found argument or -1 of arguments does not appear in list
76  */
77  template<typename Type> int
78  parse (int argc, char** argv, const char* argument_name, Type& value)
79  {
80  int index = find_argument (argc, argv, argument_name) + 1;
81 
82  if (index > 0 && index < argc)
83  {
84  std::istringstream stream;
85  stream.clear ();
86  stream.str (argv[index]);
87  stream >> value;
88  }
89 
90  return (index - 1);
91  }
92 
93  /** \brief Parse for a specific given command line argument. Returns the value
94  * sent as a string.
95  * \param[in] argc the number of command line arguments
96  * \param[in] argv the command line arguments
97  * \param[in] str the string value to search for
98  * \param[out] val the resultant value
99  */
100  PCL_EXPORTS int
101  parse_argument (int argc, char** argv, const char* str, std::string &val);
102 
103  /** \brief Parse for a specific given command line argument. Returns the value
104  * sent as a boolean.
105  * \param[in] argc the number of command line arguments
106  * \param[in] argv the command line arguments
107  * \param[in] str the string value to search for
108  * \param[out] val the resultant value
109  */
110  PCL_EXPORTS int
111  parse_argument (int argc, char** argv, const char* str, bool &val);
112 
113  /** \brief Parse for a specific given command line argument. Returns the value
114  * sent as a double.
115  * \param[in] argc the number of command line arguments
116  * \param[in] argv the command line arguments
117  * \param[in] str the string value to search for
118  * \param[out] val the resultant value
119  */
120  PCL_EXPORTS int
121  parse_argument (int argc, char** argv, const char* str, float &val);
122 
123  /** \brief Parse for a specific given command line argument. Returns the value
124  * sent as a double.
125  * \param[in] argc the number of command line arguments
126  * \param[in] argv the command line arguments
127  * \param[in] str the string value to search for
128  * \param[out] val the resultant value
129  */
130  PCL_EXPORTS int
131  parse_argument (int argc, char** argv, const char* str, double &val);
132 
133  /** \brief Parse for a specific given command line argument. Returns the value
134  * sent as an int.
135  * \param[in] argc the number of command line arguments
136  * \param[in] argv the command line arguments
137  * \param[in] str the string value to search for
138  * \param[out] val the resultant value
139  */
140  PCL_EXPORTS int
141  parse_argument (int argc, char** argv, const char* str, int &val);
142 
143  /** \brief Parse for a specific given command line argument. Returns the value
144  * sent as an unsigned int.
145  * \param[in] argc the number of command line arguments
146  * \param[in] argv the command line arguments
147  * \param[in] str the string value to search for
148  * \param[out] val the resultant value
149  */
150  PCL_EXPORTS int
151  parse_argument (int argc, char** argv, const char* str, unsigned int &val);
152 
153  /** \brief Parse for a specific given command line argument. Returns the value
154  * sent as an int.
155  * \param[in] argc the number of command line arguments
156  * \param[in] argv the command line arguments
157  * \param[in] str the string value to search for
158  * \param[out] val the resultant value
159  */
160  PCL_EXPORTS int
161  parse_argument (int argc, char** argv, const char* str, char &val);
162 
163  /** \brief Parse for specific given command line arguments (2x values comma
164  * separated). Returns the values sent as doubles.
165  * \param[in] argc the number of command line arguments
166  * \param[in] argv the command line arguments
167  * \param[in] str the command line argument to search for
168  * \param[out] f the first output value
169  * \param[out] s the second output value
170  * \param[in] debug whether to print debug info or not
171  */
172  PCL_EXPORTS int
173  parse_2x_arguments (int argc, char** argv, const char* str, float &f, float &s, bool debug = true);
174 
175  /** \brief Parse for specific given command line arguments (2x values comma
176  * separated). Returns the values sent as doubles.
177  * \param[in] argc the number of command line arguments
178  * \param[in] argv the command line arguments
179  * \param[in] str the command line argument to search for
180  * \param[out] f the first output value
181  * \param[out] s the second output value
182  * \param[in] debug whether to print debug info or not
183  */
184  PCL_EXPORTS int
185  parse_2x_arguments (int argc, char** argv, const char* str, double &f, double &s, bool debug = true);
186 
187  /** \brief Parse for specific given command line arguments (2x values comma
188  * separated). Returns the values sent as ints.
189  * \param[in] argc the number of command line arguments
190  * \param[in] argv the command line arguments
191  * \param[in] str the command line argument to search for
192  * \param[out] f the first output value
193  * \param[out] s the second output value
194  * \param[in] debug whether to print debug info or not
195  */
196  PCL_EXPORTS int
197  parse_2x_arguments (int argc, char** argv, const char* str, int &f, int &s, bool debug = true);
198 
199  /** \brief Parse for specific given command line arguments (3x values comma
200  * separated). Returns the values sent as doubles.
201  * \param[in] argc the number of command line arguments
202  * \param[in] argv the command line arguments
203  * \param[in] str the command line argument to search for
204  * \param[out] f the first output value
205  * \param[out] s the second output value
206  * \param[out] t the third output value
207  * \param[in] debug whether to print debug info or not
208  */
209  PCL_EXPORTS int
210  parse_3x_arguments (int argc, char** argv, const char* str, float &f, float &s, float &t, bool debug = true);
211 
212  /** \brief Parse for specific given command line arguments (3x values comma
213  * separated). Returns the values sent as doubles.
214  * \param[in] argc the number of command line arguments
215  * \param[in] argv the command line arguments
216  * \param[in] str the command line argument to search for
217  * \param[out] f the first output value
218  * \param[out] s the second output value
219  * \param[out] t the third output value
220  * \param[in] debug whether to print debug info or not
221  */
222  PCL_EXPORTS int
223  parse_3x_arguments (int argc, char** argv, const char* str, double &f, double &s, double &t, bool debug = true);
224 
225  /** \brief Parse for specific given command line arguments (3x values comma
226  * separated). Returns the values sent as ints.
227  * \param[in] argc the number of command line arguments
228  * \param[in] argv the command line arguments
229  * \param[in] str the command line argument to search for
230  * \param[out] f the first output value
231  * \param[out] s the second output value
232  * \param[out] t the third output value
233  * \param[in] debug whether to print debug info or not
234  */
235  PCL_EXPORTS int
236  parse_3x_arguments (int argc, char** argv, const char* str, int &f, int &s, int &t, bool debug = true);
237 
238  /** \brief Parse for specific given command line arguments (3x values comma
239  * separated). Returns the values sent as doubles.
240  * \param[in] argc the number of command line arguments
241  * \param[in] argv the command line arguments
242  * \param[in] str the command line argument to search for
243  * \param[out] v the vector into which the parsed values will be copied
244  */
245  PCL_EXPORTS int
246  parse_x_arguments (int argc, char** argv, const char* str, std::vector<double>& v);
247 
248  /** \brief Parse for specific given command line arguments (N values comma
249  * separated). Returns the values sent as ints.
250  * \param[in] argc the number of command line arguments
251  * \param[in] argv the command line arguments
252  * \param[in] str the command line argument to search for
253  * \param[out] v the vector into which the parsed values will be copied
254  */
255  PCL_EXPORTS int
256  parse_x_arguments (int argc, char** argv, const char* str, std::vector<float>& v);
257 
258  /** \brief Parse for specific given command line arguments (N values comma
259  * separated). Returns the values sent as ints.
260  * \param[in] argc the number of command line arguments
261  * \param[in] argv the command line arguments
262  * \param[in] str the command line argument to search for
263  * \param[out] v the vector into which the parsed values will be copied
264  */
265  PCL_EXPORTS int
266  parse_x_arguments (int argc, char** argv, const char* str, std::vector<int>& v);
267 
268  /** \brief Parse for specific given command line arguments (multiple occurances
269  * of the same command line parameter). Returns the values sent as a vector.
270  * \param[in] argc the number of command line arguments
271  * \param[in] argv the command line arguments
272  * \param[in] str the command line argument to search for
273  * \param[out] values the resultant output values
274  */
275  PCL_EXPORTS bool
276  parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<int> &values);
277 
278  /** \brief Parse for specific given command line arguments (multiple occurances
279  * of the same command line parameter). Returns the values sent as a vector.
280  * \param[in] argc the number of command line arguments
281  * \param[in] argv the command line arguments
282  * \param[in] str the command line argument to search for
283  * \param[out] values the resultant output values
284  */
285  PCL_EXPORTS bool
286  parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<float> &values);
287 
288  /** \brief Parse for specific given command line arguments (multiple occurances
289  * of the same command line parameter). Returns the values sent as a vector.
290  * \param[in] argc the number of command line arguments
291  * \param[in] argv the command line arguments
292  * \param[in] str the command line argument to search for
293  * \param[out] values the resultant output values
294  */
295  PCL_EXPORTS bool
296  parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<double> &values);
297 
298  /** \brief Parse for a specific given command line argument (multiple occurences
299  * of the same command line parameter). Returns the value sent as a vector.
300  * \param[in] argc the number of command line arguments
301  * \param[in] argv the command line arguments
302  * \param[in] str the string value to search for
303  * \param[out] values the resultant output values
304  */
305  PCL_EXPORTS bool
306  parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<std::string> &values);
307 
308  /** \brief Parse for specific given command line arguments (multiple occurances
309  * of 2x argument groups, separated by commas). Returns 2 vectors holding the
310  * given values.
311  * \param[in] argc the number of command line arguments
312  * \param[in] argv the command line arguments
313  * \param[in] str the command line argument to search for
314  * \param[out] values_f the first vector of output values
315  * \param[out] values_s the second vector of output values
316  */
317  PCL_EXPORTS bool
318  parse_multiple_2x_arguments (int argc, char** argv, const char* str,
319  std::vector<double> &values_f,
320  std::vector<double> &values_s);
321 
322  /** \brief Parse for specific given command line arguments (multiple occurances
323  * of 3x argument groups, separated by commas). Returns 3 vectors holding the
324  * given values.
325  * \param[in] argc the number of command line arguments
326  * \param[in] argv the command line arguments
327  * \param[in] str the command line argument to search for
328  * \param[out] values_f the first vector of output values
329  * \param[out] values_s the second vector of output values
330  * \param[out] values_t the third vector of output values
331  */
332  PCL_EXPORTS bool
333  parse_multiple_3x_arguments (int argc, char** argv, const char* str,
334  std::vector<double> &values_f,
335  std::vector<double> &values_s,
336  std::vector<double> &values_t);
337 
338  /** \brief Parse command line arguments for file names. Returns a vector with
339  * file names indices.
340  * \param[in] argc the number of command line arguments
341  * \param[in] argv the command line arguments
342  * \param[in] ext the extension to search for
343  */
344  PCL_EXPORTS std::vector<int>
345  parse_file_extension_argument (int argc, char** argv, const std::string &ext);
346  }
347 }
348 
349 #endif // PCL_CONSOLE_PARSE_H_
350