Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
parse-argv1.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#include <stdlib.h>
51#include <visp3/core/vpConfig.h>
52#include <visp3/core/vpDebug.h>
53#include <visp3/io/vpParseArgv.h>
54// List of allowed command line options
55#define GETOPTARGS "d:f:i:h"
56
57#ifdef ENABLE_VISP_NAMESPACE
58using namespace VISP_NAMESPACE_NAME;
59#endif
60
61void usage(const char *name, const char *badparam, int i_val, float f_val, double d_val);
62bool getOptions(int argc, const char **argv, int &i_val, float &f_val, double &d_val);
63
75void usage(const char *name, const char *badparam, int i_val, float f_val, double d_val)
76{
77 fprintf(stdout, "\n\
78Parsing command line arguments example.\n\
79\n\
80SYNOPSIS\n\
81 %s [-i <integer>] [-f <float>] [-d <double> [-h]\n\
82",
83name);
84
85 fprintf(stdout, "\n\
86OPTIONS: Default\n\
87 -i <integer> %d\n\
88 An integer value.\n\
89\n\
90 -f <float> %f\n\
91 A float value.\n\
92\n\
93 -d <double> %g\n\
94 A double value.\n\
95\n\
96 -h\n\
97 Print the help.\n\n",
98 i_val, f_val, d_val);
99
100 if (badparam) {
101 fprintf(stderr, "ERROR: \n");
102 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103 }
104}
105
118bool getOptions(int argc, const char **argv, int &i_val, float &f_val, double &d_val)
119{
120 const char *optarg_;
121 int c;
122 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
123
124 switch (c) {
125 case 'd':
126 d_val = atof(optarg_);
127 break;
128 case 'f':
129 f_val = static_cast<float>(atof(optarg_));
130 break;
131 case 'i':
132 i_val = atoi(optarg_);
133 break;
134 case 'h':
135 usage(argv[0], nullptr, i_val, f_val, d_val);
136 return false;
137
138 default:
139 usage(argv[0], optarg_, i_val, f_val, d_val);
140 return false;
141 }
142 }
143
144 if ((c == 1) || (c == -1)) {
145 // standalone param or error
146 usage(argv[0], nullptr, i_val, f_val, d_val);
147 std::cerr << "ERROR: " << std::endl;
148 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
149 return false;
150 }
151
152 return true;
153}
154
155int main(int argc, const char **argv)
156{
157 try {
158 using ::std::cout;
159 using ::std::endl;
160
161 int i_val = 3;
162 float f_val = 3.14f;
163 double d_val = 3.1415;
164
165 // Read the command line options
166 if (getOptions(argc, argv, i_val, f_val, d_val) == false) {
167 return EXIT_FAILURE;
168 }
169
170 cout << "Your parameters: " << endl;
171 cout << " Integer value: " << i_val << endl;
172 cout << " Float value: " << f_val << endl;
173 cout << " Double value: " << d_val << endl << endl;
174 cout << "Call " << argv[0] << " -h to see how to change these parameters." << endl;
175
176 return EXIT_SUCCESS;
177 }
178 catch (const vpException &e) {
179 std::cout << "Catch a ViSP exception: " << e << std::endl;
180 return EXIT_FAILURE;
181 }
182}
error that can be emitted by ViSP classes.
Definition vpException.h:60
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)