Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpParseArgv.h
1
/*
2
* Declarations for Tk-related things that are visible
3
* outside of the Tk module itself.
4
*
5
* Copyright 1989-1992 Regents of the University of California.
6
* Permission to use, copy, modify, and distribute this
7
* software and its documentation for any purpose and without
8
* fee is hereby granted, provided that the above copyright
9
* notice appear in all copies. The University of California
10
* makes no representations about the suitability of this
11
* software for any purpose. It is provided "as is" without
12
* express or implied warranty.
13
*
14
* This file has been modified to be used only for argv parsing without
15
* reference to tk, tcl or X11. Base on tk.h from tk2.3
16
*
17
* Modifications by Peter Neelin (November 27, 1992)
18
* Modifications by Fabien Spindler (June 20, 2006)
19
*/
20
25
26
#ifndef VP_PARSE_ARGV_H
27
#define VP_PARSE_ARGV_H
28
29
#include <visp3/core/vpConfig.h>
30
#include <visp3/core/vpException.h>
31
32
BEGIN_VISP_NAMESPACE
33
41
name with more than one character.
42
43
\code
44
#include <stdio.h>
45
#include <visp3/core/vpMath.h>
46
#include <visp3/io/vpParseArgv.h>
47
48
// Usage : [-bool] [-int <integer value>] [-long <long value>]
49
// [-float <float value>] [-double <double value>] [-string <string value>] [-h]
50
int main(int argc, const char ** argv)
51
{
52
// Variables to set by command line parsing
53
bool b_val = false;
54
int i_val = 10;
55
long l_val = 123456;
56
float f_val = 0.1f;
57
double d_val = M_PI;
58
char *s_val;
59
60
// Parse the command line to set the variables
61
vpParseArgv::vpArgvInfo argTable[] =
62
{
63
{"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *) &b_val,
64
"Flag enabled."},
65
{"-int", vpParseArgv::ARGV_INT, (char*) nullptr, (char *) &i_val,
66
"An integer value."},
67
{"-long", vpParseArgv::ARGV_LONG, (char*) nullptr, (char *) &l_val,
68
"An integer value."},
69
{"-float", vpParseArgv::ARGV_FLOAT, (char*) nullptr, (char *) &f_val,
70
"A float value."},
71
{"-double", vpParseArgv::ARGV_DOUBLE, (char*) nullptr, (char *) &d_val,
72
"A double value."},
73
{"-string", vpParseArgv::ARGV_STRING, (char*) nullptr, (char *) &s_val,
74
"A string value."},
75
{"-h", vpParseArgv::ARGV_HELP, (char*) nullptr, (char *) nullptr,
76
"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,
81
vpParseArgv::ARGV_NO_LEFTOVERS |
82
vpParseArgv::ARGV_NO_ABBREV |
83
vpParseArgv::ARGV_NO_DEFAULTS)) {
84
return (false);
85
}
86
87
// b_val, i_val, l_val, f_val, d_val, s_val may have new values
88
}
89
\endcode
90
91
The code below shows an other way to parse command line arguments using
92
vpParseArgv class. Here command line options are only one character long.
93
\code
94
#include <stdio.h>
95
#include <stdlib.h>
96
#include <visp3/core/vpMath.h>
97
#include <visp3/io/vpParseArgv.h>
98
99
// List of allowed command line options
100
#define GETOPTARGS "bi:l:f:d:h" // double point mean here that the preceding option request an argument
101
102
// Usage : [-b] [-i <integer value>] [-l <long value>]
103
// [-f <float value>] [-d <double value>] [-s <string value>] [-h]
104
int main(int argc, const char ** argv)
105
{
106
// Variables to set by command line parsing
107
bool b_val = false;
108
int i_val = 10;
109
long l_val = 123456;
110
float f_val = 0.1f;
111
double d_val = M_PI;
112
std::string s_val;
113
114
// Parse the command line to set the variables
115
const char *optarg;
116
int c;
117
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
118
119
switch (c) {
120
case 'b': b_val = true; break;
121
case 'i': i_val = atoi(optarg); break;
122
case 'l': l_val = atol(optarg); break;
123
case 'f': f_val = static_cast<float>(atof(optarg)); break;
124
case 'd': d_val = atof(optarg); break;
125
case 's': s_val = std::string(optarg); break;
126
case 'h': printf("Usage: ...\n"); return EXIT_SUCCESS; break;
127
128
default:
129
printf("Usage: ...\n"); return EXIT_SUCCESS; break;
130
}
131
}
132
if ((c == 1) || (c == -1)) {
133
// standalone param or error
134
printf("Usage: ...\n");
135
return EXIT_FAILURE;
136
}
137
138
// b_val, i_val, l_val, f_val, d_val, s_val may have new values
139
}
140
\endcode
141
142
*/
143
144
class
VISP_EXPORT
vpParseArgv
145
{
146
public
:
150
typedef
enum
151
{
152
ARGV_CONSTANT
,
153
ARGV_CONSTANT_INT
,
154
ARGV_CONSTANT_BOOL
,
155
ARGV_INT
,
156
ARGV_LONG
,
157
ARGV_STRING
,
158
ARGV_REST
,
159
ARGV_FLOAT
,
160
ARGV_DOUBLE
,
161
ARGV_FUNC
,
162
ARGV_GENFUNC
,
163
ARGV_HELP
,
164
ARGV_END
165
} vpArgvType;
166
170
typedef
enum
171
{
172
ARGV_NO_DEFAULTS
= 0x1,
173
ARGV_NO_LEFTOVERS
= 0x2,
174
ARGV_NO_ABBREV
= 0x4,
176
ARGV_DONT_SKIP_FIRST_ARG
= 0x8,
177
ARGV_NO_PRINT
= 0x10
178
} vpArgvFlags;
179
180
#ifndef DOXYGEN_SHOULD_SKIP_THIS
185
typedef
struct
186
{
187
const
char
*key;
188
vpArgvType type;
189
const
char
*src;
190
const
char
*dst;
191
const
char
*help;
192
} vpArgvInfo;
193
#endif
/* DOXYGEN_SHOULD_SKIP_THIS */
194
195
public
:
196
static
vpArgvInfo defaultTable[2];
197
static
bool
parse(
int
*argcPtr,
const
char
**argv, vpArgvInfo *argTable,
int
flags);
198
static
int
parse(
int
argc,
const
char
**argv,
const
char
*validOpts,
const
char
**param);
199
200
private
:
201
static
void
printUsage(vpArgvInfo *argTable,
int
flags);
202
};
203
204
END_VISP_NAMESPACE
205
206
#endif
vpParseArgv
Command line argument parsing.
Definition
vpParseArgv.h:145
vpParseArgv::ARGV_NO_DEFAULTS
@ ARGV_NO_DEFAULTS
No default options like -help.
Definition
vpParseArgv.h:172
vpParseArgv::ARGV_NO_PRINT
@ ARGV_NO_PRINT
No printings.
Definition
vpParseArgv.h:177
vpParseArgv::ARGV_NO_ABBREV
@ ARGV_NO_ABBREV
Definition
vpParseArgv.h:174
vpParseArgv::ARGV_DONT_SKIP_FIRST_ARG
@ ARGV_DONT_SKIP_FIRST_ARG
Don't skip first argument.
Definition
vpParseArgv.h:176
vpParseArgv::ARGV_NO_LEFTOVERS
@ ARGV_NO_LEFTOVERS
Print an error message if an option is not in the argument list.
Definition
vpParseArgv.h:173
vpParseArgv::ARGV_DOUBLE
@ ARGV_DOUBLE
Argument is associated to a double.
Definition
vpParseArgv.h:160
vpParseArgv::ARGV_LONG
@ ARGV_LONG
Argument is associated to a long.
Definition
vpParseArgv.h:156
vpParseArgv::ARGV_FUNC
@ ARGV_FUNC
Definition
vpParseArgv.h:161
vpParseArgv::ARGV_STRING
@ ARGV_STRING
Argument is associated to a char * string.
Definition
vpParseArgv.h:157
vpParseArgv::ARGV_REST
@ ARGV_REST
Definition
vpParseArgv.h:158
vpParseArgv::ARGV_FLOAT
@ ARGV_FLOAT
Argument is associated to a float.
Definition
vpParseArgv.h:159
vpParseArgv::ARGV_CONSTANT
@ ARGV_CONSTANT
Stand alone argument. Same as vpParseArgv::ARGV_CONSTANT_INT.
Definition
vpParseArgv.h:152
vpParseArgv::ARGV_INT
@ ARGV_INT
Argument is associated to an int.
Definition
vpParseArgv.h:155
vpParseArgv::ARGV_GENFUNC
@ ARGV_GENFUNC
Definition
vpParseArgv.h:162
vpParseArgv::ARGV_CONSTANT_BOOL
@ ARGV_CONSTANT_BOOL
Stand alone argument associated to a bool var that is set to true.
Definition
vpParseArgv.h:154
vpParseArgv::ARGV_CONSTANT_INT
@ ARGV_CONSTANT_INT
Stand alone argument associated to an int var that is set to 1.
Definition
vpParseArgv.h:153
vpParseArgv::ARGV_END
@ ARGV_END
End of the argument list.
Definition
vpParseArgv.h:164
vpParseArgv::ARGV_HELP
@ ARGV_HELP
Argument is for help displaying.
Definition
vpParseArgv.h:163
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
io
include
visp3
io
vpParseArgv.h
Generated by
1.17.0