Visual Servoing Platform  version 3.0.1
parse-argv2.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of command line parsing.
32  *
33  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
51 #include <stdio.h>
52 #include <sstream>
53 #include <iomanip>
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  {
73  {"-bool", vpParseArgv::ARGV_CONSTANT, 0, (char *) &bool_val,
74  "Bool enabled."},
75  {"-integer", vpParseArgv::ARGV_INT, (char*) NULL, (char *) &int_val,
76  "An integer value."},
77  {"-long", vpParseArgv::ARGV_LONG, (char*) NULL, (char *) &long_val,
78  "A long value."},
79  {"-float", vpParseArgv::ARGV_FLOAT, (char*) NULL, (char *) &float_val,
80  "A float value."},
81  {"-double", vpParseArgv::ARGV_DOUBLE, (char*) NULL, (char *) &double_val,
82  "A double value."},
83  {"-string", vpParseArgv::ARGV_STRING, (char*) NULL, (char *) &string_val,
84  "A chain value."},
85  {"-h", vpParseArgv::ARGV_HELP, (char*) NULL, (char *) NULL,
86  "Print the help."},
87  {(char*) NULL, vpParseArgv::ARGV_END, (char*) NULL, (char*) NULL, (char*) NULL}
88  } ;
89 
90  // Read the command line options
91  if(vpParseArgv::parse(&argc, argv, argTable, vpParseArgv::ARGV_NO_DEFAULTS)) {
92  return (-1);
93  }
94 
95  cout << "Your parameters: " << endl;
96  cout << " Bool value: " << bool_val << endl;
97  cout << " Integer value: " << int_val << endl;
98  cout << " Long value: " << long_val << endl;
99  cout << " Float value: " << float_val << endl;
100  cout << " Double value: " << double_val << endl;
101  if (string_val != NULL)
102  cout << " String value: " << string_val << endl;
103  else
104  cout << " String value: \"\"" << endl << endl;
105 
106  cout << "Call " << argv[0]
107  << " -h to see how to change these parameters." << endl;
108 
109  return 0;
110  }
111  catch(const vpException &e) {
112  std::cout << "Catch a ViSP exception: " << e.getStringMessage() << std::endl;
113  return 1;
114  }
115 }
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).