Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
parse-argv2.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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 */
34
40
46
47#include <iomanip>
48#include <sstream>
49#include <stdio.h>
50
51#include <visp3/core/vpConfig.h>
52#include <visp3/core/vpDebug.h>
53#include <visp3/io/vpParseArgv.h>
54
55int main(int argc, const char **argv)
56{
57#ifdef ENABLE_VISP_NAMESPACE
58 using namespace VISP_NAMESPACE_NAME;
59#endif
60
61 try {
62 using ::std::cout;
63 using ::std::endl;
64
65 bool bool_val = false;
66 int int_val = 3;
67 long long_val = 33333333;
68 float float_val = 3.14f;
69 double double_val = 3.1415;
70 char *string_val = nullptr;
71
72 vpParseArgv::vpArgvInfo argTable[] = {
73 {"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *)&bool_val, "Bool enabled."},
74 {"-integer", vpParseArgv::ARGV_INT, (char *)nullptr, (char *)&int_val, "An integer value."},
75 {"-long", vpParseArgv::ARGV_LONG, (char *)nullptr, (char *)&long_val, "A long value."},
76 {"-float", vpParseArgv::ARGV_FLOAT, (char *)nullptr, (char *)&float_val, "A float value."},
77 {"-double", vpParseArgv::ARGV_DOUBLE, (char *)nullptr, (char *)&double_val, "A double value."},
78 {"-string", vpParseArgv::ARGV_STRING, (char *)nullptr, (char *)&string_val, "A chain value."},
79 {"-h", vpParseArgv::ARGV_HELP, (char *)nullptr, (char *)nullptr, "Print the help."},
80 {(char *)nullptr, vpParseArgv::ARGV_END, (char *)nullptr, (char *)nullptr, (char *)nullptr} };
81
82 // Read the command line options
83 if (vpParseArgv::parse(&argc, argv, argTable, vpParseArgv::ARGV_NO_DEFAULTS)) {
84 return EXIT_FAILURE;
85 }
86
87 cout << "Your parameters: " << endl;
88 cout << " Bool value: " << bool_val << endl;
89 cout << " Integer value: " << int_val << endl;
90 cout << " Long value: " << long_val << endl;
91 cout << " Float value: " << float_val << endl;
92 cout << " Double value: " << double_val << endl;
93 if (string_val != nullptr)
94 cout << " String value: " << string_val << endl;
95 else
96 cout << " String value: \"\"" << endl << endl;
97
98 cout << "Call " << argv[0] << " -h to see how to change these parameters." << endl;
99
100 return EXIT_SUCCESS;
101 }
102 catch (const vpException &e) {
103 std::cout << "Catch a ViSP exception: " << e.getStringMessage() << std::endl;
104 return EXIT_FAILURE;
105 }
106}
error that can be emitted by ViSP classes.
Definition vpException.h:60
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
@ ARGV_NO_DEFAULTS
No default options like -help.
@ ARGV_DOUBLE
Argument is associated to a double.
@ ARGV_LONG
Argument is associated to a long.
@ ARGV_STRING
Argument is associated to a char * string.
@ ARGV_FLOAT
Argument is associated to a float.
@ ARGV_INT
Argument is associated to an int.
@ ARGV_CONSTANT_BOOL
Stand alone argument associated to a bool var that is set to true.
@ ARGV_END
End of the argument list.
@ ARGV_HELP
Argument is for help displaying.