Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
parse-argv2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  *
35 *****************************************************************************/
36 
49 #include <iomanip>
50 #include <sstream>
51 #include <stdio.h>
52 
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/io/vpParseArgv.h>
55 
56 int main(int argc, const char **argv)
57 {
58  try {
59  using ::std::cout;
60  using ::std::endl;
61 
62  bool bool_val = false;
63  int int_val = 3;
64  long long_val = 33333333;
65  float float_val = 3.14f;
66  double double_val = 3.1415;
67  char *string_val = nullptr;
68 
69  vpParseArgv::vpArgvInfo argTable[] = {
70  {"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *)&bool_val, "Bool enabled."},
71  {"-integer", vpParseArgv::ARGV_INT, (char *)nullptr, (char *)&int_val, "An integer value."},
72  {"-long", vpParseArgv::ARGV_LONG, (char *)nullptr, (char *)&long_val, "A long value."},
73  {"-float", vpParseArgv::ARGV_FLOAT, (char *)nullptr, (char *)&float_val, "A float value."},
74  {"-double", vpParseArgv::ARGV_DOUBLE, (char *)nullptr, (char *)&double_val, "A double value."},
75  {"-string", vpParseArgv::ARGV_STRING, (char *)nullptr, (char *)&string_val, "A chain value."},
76  {"-h", vpParseArgv::ARGV_HELP, (char *)nullptr, (char *)nullptr, "Print the help."},
77  {(char *)nullptr, vpParseArgv::ARGV_END, (char *)nullptr, (char *)nullptr, (char *)nullptr}};
78 
79  // Read the command line options
80  if (vpParseArgv::parse(&argc, argv, argTable, vpParseArgv::ARGV_NO_DEFAULTS)) {
81  return EXIT_FAILURE;
82  }
83 
84  cout << "Your parameters: " << endl;
85  cout << " Bool value: " << bool_val << endl;
86  cout << " Integer value: " << int_val << endl;
87  cout << " Long value: " << long_val << endl;
88  cout << " Float value: " << float_val << endl;
89  cout << " Double value: " << double_val << endl;
90  if (string_val != nullptr)
91  cout << " String value: " << string_val << endl;
92  else
93  cout << " String value: \"\"" << endl << endl;
94 
95  cout << "Call " << argv[0] << " -h to see how to change these parameters." << endl;
96 
97  return EXIT_SUCCESS;
98  } catch (const vpException &e) {
99  std::cout << "Catch a ViSP exception: " << e.getStringMessage() << std::endl;
100  return EXIT_FAILURE;
101  }
102 }
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const std::string & getStringMessage() const
Definition: vpException.cpp:66
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
@ ARGV_NO_DEFAULTS
No default options like -help.
Definition: vpParseArgv.h:168
@ ARGV_DOUBLE
Argument is associated to a double.
Definition: vpParseArgv.h:157
@ ARGV_LONG
Argument is associated to a long.
Definition: vpParseArgv.h:153
@ ARGV_STRING
Argument is associated to a char * string.
Definition: vpParseArgv.h:154
@ ARGV_FLOAT
Argument is associated to a float.
Definition: vpParseArgv.h:156
@ ARGV_INT
Argument is associated to an int.
Definition: vpParseArgv.h:152
@ ARGV_CONSTANT_BOOL
Stand alone argument associated to a bool var that is set to true.
Definition: vpParseArgv.h:151
@ ARGV_END
End of the argument list.
Definition: vpParseArgv.h:161
@ ARGV_HELP
Argument is for help displaying.
Definition: vpParseArgv.h:160