Visual Servoing Platform version 3.6.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 vpParseArgv_h
27#define vpParseArgv_h
28
29#include <visp3/core/vpConfig.h>
30#include <visp3/core/vpException.h>
31
41 \code
42#include <stdio.h>
43#include <visp3/core/vpMath.h>
44#include <visp3/io/vpParseArgv.h>
45
46// Usage : [-bool] [-int <integer value>] [-long <long value>]
47// [-float <float value>] [-double <double value>] [-string <string value>] [-h]
48int main(int argc, const char ** argv)
49{
50 // Variables to set by command line parsing
51 bool b_val = false;
52 int i_val = 10;
53 long l_val = 123456;
54 float f_val = 0.1f;
55 double d_val = M_PI;
56 char *s_val;
57
58 // Parse the command line to set the variables
59 vpParseArgv::vpArgvInfo argTable[] =
60 {
61 {"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *) &b_val,
62 "Flag enabled."},
63 {"-int", vpParseArgv::ARGV_INT, (char*) NULL, (char *) &i_val,
64 "An integer value."},
65 {"-long", vpParseArgv::ARGV_LONG, (char*) NULL, (char *) &l_val,
66 "An integer value."},
67 {"-float", vpParseArgv::ARGV_FLOAT, (char*) NULL, (char *) &f_val,
68 "A float value."},
69 {"-double", vpParseArgv::ARGV_DOUBLE, (char*) NULL, (char *) &d_val,
70 "A double value."},
71 {"-string", vpParseArgv::ARGV_STRING, (char*) NULL, (char *) &s_val,
72 "A string value."},
73 {"-h", vpParseArgv::ARGV_HELP, (char*) NULL, (char *) NULL,
74 "Print the help."},
75 {(char*) NULL, vpParseArgv::ARGV_END, (char*) NULL, (char*) NULL, (char*) NULL} } ;
76
77 // Read the command line options
78 if(vpParseArgv::parse(&argc, argv, argTable,
79 vpParseArgv::ARGV_NO_LEFTOVERS |
80 vpParseArgv::ARGV_NO_ABBREV |
81 vpParseArgv::ARGV_NO_DEFAULTS)) {
82 return (false);
83 }
84
85 // b_val, i_val, l_val, f_val, d_val, s_val may have new values
86}
87 \endcode
88
89 The code below shows an other way to parse command line arguments using
90 vpParseArgv class. Here command line options are only one character long.
91 \code
92#include <stdio.h>
93#include <stdlib.h>
94#include <visp3/core/vpMath.h>
95#include <visp3/io/vpParseArgv.h>
96
97// List of allowed command line options
98#define GETOPTARGS "bi:l:f:d:h" // double point mean here that the preceding option request an argument
99
100// Usage : [-b] [-i <integer value>] [-l <long value>]
101// [-f <float value>] [-d <double value>] [-s <string value>] [-h]
102int main(int argc, const char ** argv)
103{
104 // Variables to set by command line parsing
105 bool b_val = false;
106 int i_val = 10;
107 long l_val = 123456;
108 float f_val = 0.1f;
109 double d_val = M_PI;
110 std::string s_val;
111
112 // Parse the command line to set the variables
113 const char *optarg;
114 int c;
115 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
116
117 switch (c) {
118 case 'b': b_val = true; break;
119 case 'i': i_val = atoi(optarg); break;
120 case 'l': l_val = atol(optarg); break;
121 case 'f': f_val = static_cast<float>(atof(optarg)); break;
122 case 'd': d_val = atof(optarg); break;
123 case 's': s_val = std::string(optarg); break;
124 case 'h': printf("Usage: ...\n"); return EXIT_SUCCESS; break;
125
126 default:
127 printf("Usage: ...\n"); return EXIT_SUCCESS; break;
128 }
129 }
130 if ((c == 1) || (c == -1)) {
131 // standalone param or error
132 printf("Usage: ...\n");
133 return EXIT_FAILURE;
134 }
135
136 // b_val, i_val, l_val, f_val, d_val, s_val may have new values
137}
138 \endcode
139
140*/
141
142class VISP_EXPORT vpParseArgv
143{
144public:
163
167 typedef enum {
174 } vpArgvFlags;
175
176#ifndef DOXYGEN_SHOULD_SKIP_THIS
181 typedef struct {
182 const char *key;
183 vpArgvType type;
184 const char *src;
185 const char *dst;
186 const char *help;
187 } vpArgvInfo;
188#endif /* DOXYGEN_SHOULD_SKIP_THIS */
189
190public:
191 static vpArgvInfo defaultTable[2];
192 static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags);
193 static int parse(int argc, const char **argv, const char *validOpts, const char **param);
194
195private:
196 static void printUsage(vpArgvInfo *argTable, int flags);
197};
198
199#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.