Visual Servoing Platform  version 3.0.1
parse-argv1.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of command line parsing.
32  *
33  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/io/vpParseArgv.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <sstream>
58 #include <iomanip>
59 // List of allowed command line options
60 #define GETOPTARGS "d:f:i:h"
61 
62 void usage(const char *name, const char *badparam, int i_val, float f_val, double d_val);
63 bool getOptions(int argc, const char **argv, int &i_val, float &f_val, double &d_val);
64 
76 void usage(const char *name, const char *badparam, int i_val, float f_val, double d_val)
77 {
78  fprintf(stdout, "\n\
79 Parsing command line arguments example.\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-i <integer>] [-f <float>] [-d <double> [-h]\n\
83 ", name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -i <integer> %d\n\
88  An integer value.\n\
89 \n\
90  -f <float> %f\n\
91  A float value.\n\
92 \n\
93  -d <double> %g\n\
94  A double value.\n\
95 \n\
96  -h\n\
97  Print the help.\n\n",
98  i_val, f_val, d_val);
99 
100  if (badparam) {
101  fprintf(stderr, "ERROR: \n" );
102  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103  }
104 }
105 
118 bool getOptions(int argc, const char **argv, int &i_val, float &f_val, double &d_val)
119 {
120  const char *optarg_;
121  int c;
122  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
123 
124  switch (c) {
125  case 'd': d_val = atof(optarg_); break;
126  case 'f': f_val = (float) atof(optarg_); break;
127  case 'i': i_val = atoi(optarg_); break;
128  case 'h': usage(argv[0], NULL, i_val, f_val, d_val); return false; break;
129 
130  default:
131  usage(argv[0], optarg_, i_val, f_val, d_val); return false; break;
132  }
133  }
134 
135  if ((c == 1) || (c == -1)) {
136  // standalone param or error
137  usage(argv[0], NULL, i_val, f_val, d_val);
138  std::cerr << "ERROR: " << std::endl;
139  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
140  return false;
141  }
142 
143  return true;
144 }
145 
146 int
147 main(int argc, const char ** argv)
148 {
149  try {
150  using ::std::cout;
151  using ::std::endl;
152 
153  int i_val = 3;
154  float f_val = 3.14f;
155  double d_val = 3.1415;
156 
157  // Read the command line options
158  if (getOptions(argc, argv, i_val, f_val, d_val) == false) {
159  return (-1);
160  }
161 
162  cout << "Your parameters: " << endl;
163  cout << " Integer value: " << i_val << endl;
164  cout << " Float value: " << f_val << endl;
165  cout << " Double value: " << d_val << endl << endl;
166  cout << "Call " << argv[0]
167  << " -h to see how to change these parameters." << endl;
168 
169  return 0;
170  }
171  catch(vpException &e) {
172  std::cout << "Catch a ViSP exception: " << e << std::endl;
173  return 1;
174  }
175 }
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76