ViSP  2.8.0
parse-argv1.cpp
1 /****************************************************************************
2  *
3  * $Id: parse-argv1.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
77 void usage(const char *name, const char *badparam, int i_val, float f_val, double d_val)
78 {
79  fprintf(stdout, "\n\
80 Parsing command line arguments example.\n\
81 \n\
82 SYNOPSIS\n\
83  %s [-i <integer>] [-f <float>] [-d <double> [-h]\n\
84 ", name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  -i <integer> %d\n\
89  An integer value.\n\
90 \n\
91  -f <float> %f\n\
92  A float value.\n\
93 \n\
94  -d <double> %g\n\
95  A double value.\n\
96 \n\
97  -h\n\
98  Print the help.\n\n",
99  i_val, f_val, d_val);
100 
101  if (badparam) {
102  fprintf(stderr, "ERROR: \n" );
103  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
104  }
105 }
106 
119 bool getOptions(int argc, const char **argv, int &i_val, float &f_val, double &d_val)
120 {
121  const char *optarg;
122  int c;
123  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
124 
125  switch (c) {
126  case 'd': d_val = atof(optarg); break;
127  case 'f': f_val = (float) atof(optarg); break;
128  case 'i': i_val = atoi(optarg); break;
129  case 'h': usage(argv[0], NULL, i_val, f_val, d_val); return false; break;
130 
131  default:
132  usage(argv[0], optarg, i_val, f_val, d_val); return false; break;
133  }
134  }
135 
136  if ((c == 1) || (c == -1)) {
137  // standalone param or error
138  usage(argv[0], NULL, i_val, f_val, d_val);
139  std::cerr << "ERROR: " << std::endl;
140  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
141  return false;
142  }
143 
144  return true;
145 }
146 
147 int
148 main(int argc, const char ** argv)
149 {
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 
172 
173 
174 /*
175  * Local variables:
176  * c-basic-offset: 2
177  * End:
178  */
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79