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