Point Cloud Library (PCL)  1.9.1-dev
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 
39 #pragma once
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, const char * const * 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 if arguments do not appear in list
66  */
67  PCL_EXPORTS int
68  find_argument (int argc, const char * const * 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 if arguments do not appear in list
76  */
77  template<typename Type> int
78  parse (int argc, const char * const * 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.
94  * \param[in] argc the number of command line arguments
95  * \param[in] argv the command line arguments
96  * \param[in] str the string value to search for
97  * \param[out] val the resultant value
98  * \return index of found argument or -1 if arguments do not appear in list
99  */
100  PCL_EXPORTS int
101  parse_argument (int argc, const char * const * argv, const char * str, std::string &val);
102 
103  /** \brief Parse for a specific given command line argument.
104  * \param[in] argc the number of command line arguments
105  * \param[in] argv the command line arguments
106  * \param[in] str the string value to search for
107  * \param[out] val the resultant value
108  * \return index of found argument or -1 if arguments do not appear in list
109  */
110  PCL_EXPORTS int
111  parse_argument (int argc, const char * const * argv, const char * str, bool &val);
112 
113  /** \brief Parse for a specific given command line argument.
114  * \param[in] argc the number of command line arguments
115  * \param[in] argv the command line arguments
116  * \param[in] str the string value to search for
117  * \param[out] val the resultant value
118  * \return index of found argument or -1 if arguments do not appear in list
119  */
120  PCL_EXPORTS int
121  parse_argument (int argc, const char * const * argv, const char * str, float &val);
122 
123  /** \brief Parse for a specific given command line argument.
124  * \param[in] argc the number of command line arguments
125  * \param[in] argv the command line arguments
126  * \param[in] str the string value to search for
127  * \param[out] val the resultant value
128  * \return index of found argument or -1 if arguments do not appear in list
129  */
130  PCL_EXPORTS int
131  parse_argument (int argc, const char * const * argv, const char * str, double &val);
132 
133  /** \brief Parse for a specific given command line argument.
134  * \param[in] argc the number of command line arguments
135  * \param[in] argv the command line arguments
136  * \param[in] str the string value to search for
137  * \param[out] val the resultant value
138  * \return index of found argument or -1 if arguments do not appear in list
139  */
140  PCL_EXPORTS int
141  parse_argument (int argc, const char * const * argv, const char * str, int &val);
142 
143  /** \brief Parse for a specific given command line argument.
144  * \param[in] argc the number of command line arguments
145  * \param[in] argv the command line arguments
146  * \param[in] str the string value to search for
147  * \param[out] val the resultant value
148  * \return index of found argument or -1 if arguments do not appear in list
149  */
150  PCL_EXPORTS int
151  parse_argument (int argc, const char * const * argv, const char * str, unsigned int &val);
152 
153  /** \brief Parse for a specific given command line argument.
154  * \param[in] argc the number of command line arguments
155  * \param[in] argv the command line arguments
156  * \param[in] str the string value to search for
157  * \param[out] val the resultant value
158  * \return index of found argument or -1 if arguments do not appear in list
159  */
160  PCL_EXPORTS int
161  parse_argument (int argc, const char * const * argv, const char * str, char &val);
162 
163  /** \brief Parse for specific given command line arguments (2x values comma
164  * separated).
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  * \return index of found argument or -1 if arguments do not appear in list
172  */
173  PCL_EXPORTS int
174  parse_2x_arguments (int argc, const char * const * argv, const char * str, float &f, float &s, bool debug = true);
175 
176  /** \brief Parse for specific given command line arguments (2x values comma
177  * separated).
178  * \param[in] argc the number of command line arguments
179  * \param[in] argv the command line arguments
180  * \param[in] str the command line argument to search for
181  * \param[out] f the first output value
182  * \param[out] s the second output value
183  * \param[in] debug whether to print debug info or not
184  * \return index of found argument or -1 if arguments do not appear in list
185  */
186  PCL_EXPORTS int
187  parse_2x_arguments (int argc, const char * const * argv, const char * str, double &f, double &s, bool debug = true);
188 
189  /** \brief Parse for specific given command line arguments (2x values comma
190  * separated).
191  * \param[in] argc the number of command line arguments
192  * \param[in] argv the command line arguments
193  * \param[in] str the command line argument to search for
194  * \param[out] f the first output value
195  * \param[out] s the second output value
196  * \param[in] debug whether to print debug info or not
197  * \return index of found argument or -1 if arguments do not appear in list
198  */
199  PCL_EXPORTS int
200  parse_2x_arguments (int argc, const char * const * argv, const char * str, int &f, int &s, bool debug = true);
201 
202  /** \brief Parse for specific given command line arguments (3x values comma
203  * separated).
204  * \param[in] argc the number of command line arguments
205  * \param[in] argv the command line arguments
206  * \param[in] str the command line argument to search for
207  * \param[out] f the first output value
208  * \param[out] s the second output value
209  * \param[out] t the third output value
210  * \param[in] debug whether to print debug info or not
211  * \return index of found argument or -1 if arguments do not appear in list
212  */
213  PCL_EXPORTS int
214  parse_3x_arguments (int argc, const char * const * argv, const char * str, float &f, float &s, float &t, bool debug = true);
215 
216  /** \brief Parse for specific given command line arguments (3x values comma
217  * separated).
218  * \param[in] argc the number of command line arguments
219  * \param[in] argv the command line arguments
220  * \param[in] str the command line argument to search for
221  * \param[out] f the first output value
222  * \param[out] s the second output value
223  * \param[out] t the third output value
224  * \param[in] debug whether to print debug info or not
225  * \return index of found argument or -1 if arguments do not appear in list
226  */
227  PCL_EXPORTS int
228  parse_3x_arguments (int argc, const char * const * argv, const char * str, double &f, double &s, double &t, bool debug = true);
229 
230  /** \brief Parse for specific given command line arguments (3x values comma
231  * separated).
232  * \param[in] argc the number of command line arguments
233  * \param[in] argv the command line arguments
234  * \param[in] str the command line argument to search for
235  * \param[out] f the first output value
236  * \param[out] s the second output value
237  * \param[out] t the third output value
238  * \param[in] debug whether to print debug info or not
239  * return index of found argument or -1 if arguments do not appear in list
240  */
241  PCL_EXPORTS int
242  parse_3x_arguments (int argc, const char * const * argv, const char * str, int &f, int &s, int &t, bool debug = true);
243 
244  /** \brief Parse for specific given command line arguments (3x values comma
245  * separated).
246  * \param[in] argc the number of command line arguments
247  * \param[in] argv the command line arguments
248  * \param[in] str the command line argument to search for
249  * \param[out] v the vector into which the parsed values will be copied
250  * \return index of found argument or -1 if arguments do not appear in list
251  */
252  PCL_EXPORTS int
253  parse_x_arguments (int argc, const char * const * argv, const char * str, std::vector<double>& v);
254 
255  /** \brief Parse for specific given command line arguments (N values comma
256  * separated).
257  * \param[in] argc the number of command line arguments
258  * \param[in] argv the command line arguments
259  * \param[in] str the command line argument to search for
260  * \param[out] v the vector into which the parsed values will be copied
261  * \return index of found argument or -1 if arguments do not appear in list
262  */
263  PCL_EXPORTS int
264  parse_x_arguments (int argc, const char * const * argv, const char * str, std::vector<float>& v);
265 
266  /** \brief Parse for specific given command line arguments (N values comma
267  * separated).
268  * \param[in] argc the number of command line arguments
269  * \param[in] argv the command line arguments
270  * \param[in] str the command line argument to search for
271  * \param[out] v the vector into which the parsed values will be copied
272  * \return index of found argument or -1 if arguments do not appear in list
273  */
274  PCL_EXPORTS int
275  parse_x_arguments (int argc, const char * const * argv, const char * str, std::vector<int>& v);
276 
277  /** \brief Parse for specific given command line arguments (multiple occurrences
278  * of the same command line parameter).
279  * \param[in] argc the number of command line arguments
280  * \param[in] argv the command line arguments
281  * \param[in] str the command line argument to search for
282  * \param[out] values the resultant output values
283  * \return index of found argument or -1 if arguments do not appear in list
284  */
285  PCL_EXPORTS bool
286  parse_multiple_arguments (int argc, const char * const * argv, const char * str, std::vector<int> &values);
287 
288  /** \brief Parse for specific given command line arguments (multiple occurrences
289  * of the same command line parameter).
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  * \return true if found, false otherwise
295  */
296  PCL_EXPORTS bool
297  parse_multiple_arguments (int argc, const char * const * argv, const char * str, std::vector<float> &values);
298 
299  /** \brief Parse for specific given command line arguments (multiple occurrences
300  * of the same command line parameter).
301  * \param[in] argc the number of command line arguments
302  * \param[in] argv the command line arguments
303  * \param[in] str the command line argument to search for
304  * \param[out] values the resultant output values
305  * \return true if found, false otherwise
306  */
307  PCL_EXPORTS bool
308  parse_multiple_arguments (int argc, const char * const * argv, const char * str, std::vector<double> &values);
309 
310  /** \brief Parse for a specific given command line argument (multiple occurrences
311  * of the same command line parameter).
312  * \param[in] argc the number of command line arguments
313  * \param[in] argv the command line arguments
314  * \param[in] str the string value to search for
315  * \param[out] values the resultant output values
316  * \return true if found, false otherwise
317  */
318  PCL_EXPORTS bool
319  parse_multiple_arguments (int argc, const char * const * argv, const char * str, std::vector<std::string> &values);
320 
321  /** \brief Parse command line arguments for file names with given extension (multiple occurrences
322  * of 2x argument groups, separated by commas).
323  * \param[in] argc the number of command line arguments
324  * \param[in] argv the command line arguments
325  * \param[in] str the command line argument to search for
326  * \param[out] values_f the first vector of output values
327  * \param[out] values_s the second vector of output values
328  * \return true if found, false otherwise
329  */
330  PCL_EXPORTS bool
331  parse_multiple_2x_arguments (int argc, const char * const * argv, const char * str,
332  std::vector<double> &values_f,
333  std::vector<double> &values_s);
334 
335  /** \brief Parse command line arguments for file names with given extension (multiple occurrences
336  * of 3x argument groups, separated by commas).
337  * \param[in] argc the number of command line arguments
338  * \param[in] argv the command line arguments
339  * \param[in] str the command line argument to search for
340  * \param[out] values_f the first vector of output values
341  * \param[out] values_s the second vector of output values
342  * \param[out] values_t the third vector of output values
343  * \return true if found, false otherwise
344  */
345  PCL_EXPORTS bool
346  parse_multiple_3x_arguments (int argc, const char * const * argv, const char * str,
347  std::vector<double> &values_f,
348  std::vector<double> &values_s,
349  std::vector<double> &values_t);
350 
351  /** \brief Parse command line arguments for file names with given extension vector
352  * \param[in] argc the number of command line arguments
353  * \param[in] argv the command line arguments
354  * \param[in] extensions the extensions to search for
355  * \return a vector with file names indices
356  */
357  PCL_EXPORTS std::vector<int>
358  parse_file_extension_argument (int argc, const char * const * argv,
359  const std::vector<std::string> &extensions);
360 
361  /** \brief Parse command line arguments for file names with given extension
362  * \param[in] argc the number of command line arguments
363  * \param[in] argv the command line arguments
364  * \param[in] ext the extension to search for
365  * \return a vector with file names indices
366  */
367  PCL_EXPORTS std::vector<int>
368  parse_file_extension_argument (int argc, const char * const * argv, const std::string &ext);
369  }
370 }
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
PCL_EXPORTS bool find_switch(int argc, const char *const *argv, const char *argument_name)
Finds whether the argument with name "argument_name" is in the argument list "argv".
PCL_EXPORTS int parse_2x_arguments(int argc, const char *const *argv, const char *str, float &f, float &s, bool debug=true)
Parse for specific given command line arguments (2x values comma separated).
PCL_EXPORTS bool parse_multiple_3x_arguments(int argc, const char *const *argv, const char *str, std::vector< double > &values_f, std::vector< double > &values_s, std::vector< double > &values_t)
Parse command line arguments for file names with given extension (multiple occurrences of 3x argument...
PCL_EXPORTS bool parse_multiple_arguments(int argc, const char *const *argv, const char *str, std::vector< int > &values)
Parse for specific given command line arguments (multiple occurrences of the same command line parame...
PCL_EXPORTS int parse_argument(int argc, const char *const *argv, const char *str, std::string &val)
Parse for a specific given command line argument.
PCL_EXPORTS int find_argument(int argc, const char *const *argv, const char *argument_name)
Finds the position of the argument with name "argument_name" in the argument list "argv"...
PCL_EXPORTS int parse_3x_arguments(int argc, const char *const *argv, const char *str, float &f, float &s, float &t, bool debug=true)
Parse for specific given command line arguments (3x values comma separated).
PCL_EXPORTS std::vector< int > parse_file_extension_argument(int argc, const char *const *argv, const std::vector< std::string > &extensions)
Parse command line arguments for file names with given extension vector.
int parse(int argc, const char *const *argv, const char *argument_name, Type &value)
Template version for parsing arguments.
Definition: parse.h:78
PCL_EXPORTS int parse_x_arguments(int argc, const char *const *argv, const char *str, std::vector< double > &v)
Parse for specific given command line arguments (3x values comma separated).
PCL_EXPORTS bool parse_multiple_2x_arguments(int argc, const char *const *argv, const char *str, std::vector< double > &values_f, std::vector< double > &values_s)
Parse command line arguments for file names with given extension (multiple occurrences of 2x argument...
#define PCL_EXPORTS
Definition: pcl_macros.h:241
Defines all the PCL and non-PCL macros used.