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