Visual Servoing Platform  version 3.4.0
parse-argv2.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 
55 #include <visp3/core/vpDebug.h>
56 #include <visp3/io/vpParseArgv.h>
57 
58 int main(int argc, const char **argv)
59 {
60  try {
61  using ::std::cout;
62  using ::std::endl;
63 
64  bool bool_val = false;
65  int int_val = 3;
66  long long_val = 33333333;
67  float float_val = 3.14f;
68  double double_val = 3.1415;
69  char *string_val = NULL;
70 
71  vpParseArgv::vpArgvInfo argTable[] = {
72  {"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *)&bool_val, "Bool enabled."},
73  {"-integer", vpParseArgv::ARGV_INT, (char *)NULL, (char *)&int_val, "An integer value."},
74  {"-long", vpParseArgv::ARGV_LONG, (char *)NULL, (char *)&long_val, "A long value."},
75  {"-float", vpParseArgv::ARGV_FLOAT, (char *)NULL, (char *)&float_val, "A float value."},
76  {"-double", vpParseArgv::ARGV_DOUBLE, (char *)NULL, (char *)&double_val, "A double value."},
77  {"-string", vpParseArgv::ARGV_STRING, (char *)NULL, (char *)&string_val, "A chain value."},
78  {"-h", vpParseArgv::ARGV_HELP, (char *)NULL, (char *)NULL, "Print the help."},
79  {(char *)NULL, vpParseArgv::ARGV_END, (char *)NULL, (char *)NULL, (char *)NULL}};
80 
81  // Read the command line options
82  if (vpParseArgv::parse(&argc, argv, argTable, vpParseArgv::ARGV_NO_DEFAULTS)) {
83  return (-1);
84  }
85 
86  cout << "Your parameters: " << endl;
87  cout << " Bool value: " << bool_val << endl;
88  cout << " Integer value: " << int_val << endl;
89  cout << " Long value: " << long_val << endl;
90  cout << " Float value: " << float_val << endl;
91  cout << " Double value: " << double_val << endl;
92  if (string_val != NULL)
93  cout << " String value: " << string_val << endl;
94  else
95  cout << " String value: \"\"" << endl << endl;
96 
97  cout << "Call " << argv[0] << " -h to see how to change these parameters." << endl;
98 
99  return EXIT_SUCCESS;
100  } catch (const vpException &e) {
101  std::cout << "Catch a ViSP exception: " << e.getStringMessage() << std::endl;
102  return EXIT_FAILURE;
103  }
104 }
Argument is associated to a double.
Definition: vpParseArgv.h:162
End of the argument list.
Definition: vpParseArgv.h:166
Stand alone argument associated to a bool var that is set to true.
Definition: vpParseArgv.h:156
Argument is associated to a char * string.
Definition: vpParseArgv.h:159
Argument is for help displaying.
Definition: vpParseArgv.h:165
error that can be emited by ViSP classes.
Definition: vpException.h:71
Argument is associated to a long.
Definition: vpParseArgv.h:158
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Argument is associated to a float.
Definition: vpParseArgv.h:161
Command line argument parsing.
Definition: vpParseArgv.h:147
No default options like -help.
Definition: vpParseArgv.h:173
const std::string & getStringMessage() const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92
Argument is associated to an int.
Definition: vpParseArgv.h:157