Visual Servoing Platform version 3.7.0
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
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
144class VISP_EXPORT vpParseArgv
145{
146public:
166
170 typedef enum
171 {
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
195public:
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
200private:
201 static void printUsage(vpArgvInfo *argTable, int flags);
202};
203
204END_VISP_NAMESPACE
205
206#endif
Command line argument parsing.
@ ARGV_NO_DEFAULTS
No default options like -help.
@ ARGV_NO_PRINT
No printings.
@ ARGV_DONT_SKIP_FIRST_ARG
Don't skip first argument.
@ ARGV_NO_LEFTOVERS
Print an error message if an option is not in the argument list.
@ 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_CONSTANT
Stand alone argument. Same as vpParseArgv::ARGV_CONSTANT_INT.
@ 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_CONSTANT_INT
Stand alone argument associated to an int var that is set to 1.
@ ARGV_END
End of the argument list.
@ ARGV_HELP
Argument is for help displaying.