cisst-saw
Loading...
Searching...
No Matches
cmnCommandLineOptions.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5 Author(s): Anton Deguet
6 Created on: 2012-08-27
7
8 (C) Copyright 2012-2023 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20
21#ifndef _cmnCommandLineOptions_h
22#define _cmnCommandLineOptions_h
23
27
28#include <string>
29#include <list>
30#include <sstream>
31#include <iostream>
32
33// Always the last cisst include
35
36
37
38// utility to convert const char * to a C type
39template <typename _elementType>
40bool cmnCommandLineOptionsConvert(const char * value, _elementType & element) {
41 std::stringstream localStream;
42 localStream << value;
43 bool error = false;
44 try {
45 localStream >> element;
46 } catch (...) {
47 error = true;
48 }
49 if (!error) {
50 return true;
51 }
52 return false;
53}
54
55
56// template specialization to make sure spaces in strings (\ ) don't
57// act as separators using >>
58template <>
59inline bool cmnCommandLineOptionsConvert<std::string>(const char * value, std::string & element) {
60 element = value;
61 return true;
62}
63
64
112
113#undef OPTIONAL // work-around for Windows
114
116{
117 CMN_DECLARE_SERVICES(CMN_NO_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
118
119 public:
121
122 protected:
125 protected:
126#if (CISST_COMPILER == CISST_DOTNET2003)
127 // Workaround for Visual Studio.NET 2003
128 public:
129#endif
130 OptionBase(const std::string & shortOption, const std::string & longOption,
131 const std::string & description, RequiredType required);
132 virtual ~OptionBase() {};
133 virtual bool SetValue(const char * value) = 0;
134 virtual std::string PrintValues(void) const = 0;
135 std::string Short;
136 std::string Long;
137 std::string Description;
139 bool Set;
140 };
141
144 protected:
145 OptionNoValue(const std::string & shortOption, const std::string & longOption,
146 const std::string & description, RequiredType required = OPTIONAL_OPTION);
147 virtual ~OptionNoValue() {};
148 bool SetValue(const char * value);
149 std::string PrintValues(void) const {
150 return std::string();
151 }
152 };
153
156 protected:
157#if (CISST_COMPILER == CISST_DOTNET2003)
158 // Workaround for Visual Studio.NET 2003
159 public:
160#endif
161 OptionOneValueBase(const std::string & shortOption, const std::string & longOption,
162 const std::string & description, RequiredType required);
164 };
165 template <typename _elementType>
168 protected:
169 OptionOneValue(const std::string & shortOption, const std::string & longOption,
170 const std::string & description, RequiredType required,
171 _elementType * value):
172 OptionOneValueBase(shortOption, longOption, description, required),
173 Value(value)
174 {
175 }
176 bool SetValue(const char * value) {
177 bool result = cmnCommandLineOptionsConvert(value, *(this->Value));
178 if (result) {
179 this->Set = true;
180 }
181 return result;
182 }
183 std::string PrintValues(void) const {
184 std::stringstream stream;
185 stream << *Value;
186 return stream.str();
187 }
188 _elementType * Value;
189 };
190
193 protected:
194#if (CISST_COMPILER == CISST_DOTNET2003)
195 // Workaround for Visual Studio.NET 2003
196 public:
197#endif
198 OptionMultipleValuesBase(const std::string & shortOption, const std::string & longOption,
199 const std::string & description, RequiredType required);
201 };
202 template <typename _elementType>
205 protected:
206 OptionMultipleValues(const std::string & shortOption, const std::string & longOption,
207 const std::string & description, RequiredType required,
208 std::list<_elementType> * value):
209 OptionMultipleValuesBase(shortOption, longOption, description, required),
210 Value(value)
211 {
212 }
213 bool SetValue(const char * value) {
214 _elementType temp;
215 bool result = cmnCommandLineOptionsConvert(value, temp);
216 if (result) {
217 Value->push_back(temp);
218 this->Set = true;
219 }
220 return result;
221 }
222 std::string PrintValues(void) const {
223 typedef typename std::list<_elementType> ListType;
224 typedef typename ListType::const_iterator const_iterator;
225 const const_iterator end = Value->end();
226 const_iterator iter;
227 std::stringstream stream;
228 for (iter = Value->begin(); iter != end; ++iter) {
229 if (iter != Value->begin()) {
230 stream << ", ";
231 }
232 stream << *iter;
233 }
234 return stream.str();
235 }
236 std::list<_elementType> * Value;
237 };
238
239 public:
241
243
244 bool AddOptionNoValue(const std::string & shortOption, const std::string & longOption,
245 const std::string & description, RequiredType required = OPTIONAL_OPTION);
246
247 template <typename _elementType>
248 bool AddOptionOneValue(const std::string & shortOption, const std::string & longOption,
249 const std::string & description, RequiredType required, _elementType * value) {
250 std::string cleanedShort, cleanedLong;
251 if (!this->ValidOptions(shortOption, longOption, cleanedShort, cleanedLong)) {
252 return false;
253 }
254 typedef OptionOneValue<_elementType> OptionType;
255 OptionType * option = new OptionOneValue<_elementType>(cleanedShort, cleanedLong, description,
256 required, value);
257 this->Options.push_back(option);
258 return true;
259 }
260
261 template <typename _elementType>
262 bool AddOptionMultipleValues(const std::string & shortOption, const std::string & longOption,
263 const std::string & description, RequiredType required, std::list<_elementType> * value) {
264 std::string cleanedShort, cleanedLong;
265 if (!this->ValidOptions(shortOption, longOption, cleanedShort, cleanedLong)) {
266 return false;
267 }
268 typedef OptionMultipleValues<_elementType> OptionType;
269 OptionType * option = new OptionMultipleValues<_elementType>(cleanedShort, cleanedLong, description,
270 required, value);
271 this->Options.push_back(option);
272 return true;
273 }
274
279 bool Parse(int argc, const char * argv[], std::string & errorMessage);
280
285 bool Parse(int argc, char * argv[], std::string & errorMessage);
286 bool Parse(const std::vector<std::string> & arguments, std::string & errorMessage);
287
290 bool Parse(int argc, char * argv[], std::ostream & outputStream);
291 bool Parse(const std::vector<std::string> & arguments, std::ostream & outputStream);
292
295 void PrintUsage(std::ostream & outputStream);
296
300 bool IsSet(const std::string & option);
301
302
303 /* Print out the list of options parsed successfully. This can be
304 used after Parse. */
305 void PrintParsedArguments(std::string & parsedArguments) const;
306
307 protected:
308 std::string ProgramName;
309 typedef std::list<OptionBase *> OptionsType;
311 OptionBase * GetShortNoDash(const std::string & shortOption);
312 OptionBase * GetLongNoDashDash(const std::string & longOption);
313 bool ValidOptions(const std::string & shortOption, const std::string & longOption,
314 std::string & cleanedShort, std::string & cleanedLong);
315 OptionBase * Get(const std::string & option);
316};
317
318
319// Add services instantiation
321
322
323#endif // _cmnCommandLineOptions_h
Definition cmnCommandLineOptions.h:123
RequiredType Required
Definition cmnCommandLineOptions.h:138
std::string Description
Definition cmnCommandLineOptions.h:137
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:124
bool Set
Definition cmnCommandLineOptions.h:139
std::string Long
Definition cmnCommandLineOptions.h:136
virtual bool SetValue(const char *value)=0
virtual ~OptionBase()
Definition cmnCommandLineOptions.h:132
std::string Short
Definition cmnCommandLineOptions.h:135
OptionBase(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required)
virtual std::string PrintValues(void) const =0
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:192
OptionMultipleValuesBase(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required)
virtual ~OptionMultipleValuesBase()
Definition cmnCommandLineOptions.h:200
Definition cmnCommandLineOptions.h:203
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:204
std::string PrintValues(void) const
Definition cmnCommandLineOptions.h:222
OptionMultipleValues(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, std::list< _elementType > *value)
Definition cmnCommandLineOptions.h:206
std::list< _elementType > * Value
Definition cmnCommandLineOptions.h:236
bool SetValue(const char *value)
Definition cmnCommandLineOptions.h:213
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:143
std::string PrintValues(void) const
Definition cmnCommandLineOptions.h:149
virtual ~OptionNoValue()
Definition cmnCommandLineOptions.h:147
OptionNoValue(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required=OPTIONAL_OPTION)
bool SetValue(const char *value)
virtual ~OptionOneValueBase()
Definition cmnCommandLineOptions.h:163
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:155
OptionOneValueBase(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required)
Definition cmnCommandLineOptions.h:166
friend class cmnCommandLineOptions
Definition cmnCommandLineOptions.h:167
OptionOneValue(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, _elementType *value)
Definition cmnCommandLineOptions.h:169
bool SetValue(const char *value)
Definition cmnCommandLineOptions.h:176
std::string PrintValues(void) const
Definition cmnCommandLineOptions.h:183
_elementType * Value
Definition cmnCommandLineOptions.h:188
Set command line options and parse command line arguments.
Definition cmnCommandLineOptions.h:116
void PrintParsedArguments(std::string &parsedArguments) const
void PrintUsage(std::ostream &outputStream)
bool Parse(int argc, char *argv[], std::ostream &outputStream)
OptionBase * GetLongNoDashDash(const std::string &longOption)
std::string ProgramName
Definition cmnCommandLineOptions.h:308
std::list< OptionBase * > OptionsType
Definition cmnCommandLineOptions.h:309
bool Parse(const std::vector< std::string > &arguments, std::ostream &outputStream)
OptionBase * Get(const std::string &option)
bool AddOptionNoValue(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required=OPTIONAL_OPTION)
bool Parse(int argc, const char *argv[], std::string &errorMessage)
bool Parse(const std::vector< std::string > &arguments, std::string &errorMessage)
bool Parse(int argc, char *argv[], std::string &errorMessage)
OptionsType Options
Definition cmnCommandLineOptions.h:310
bool IsSet(const std::string &option)
RequiredType
Definition cmnCommandLineOptions.h:120
@ REQUIRED_OPTION
Definition cmnCommandLineOptions.h:120
@ SQUASH_REQUIRED_OPTION
Definition cmnCommandLineOptions.h:120
@ OPTIONAL_OPTION
Definition cmnCommandLineOptions.h:120
bool ValidOptions(const std::string &shortOption, const std::string &longOption, std::string &cleanedShort, std::string &cleanedLong)
OptionBase * GetShortNoDash(const std::string &shortOption)
bool AddOptionMultipleValues(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, std::list< _elementType > *value)
Definition cmnCommandLineOptions.h:262
bool AddOptionOneValue(const std::string &shortOption, const std::string &longOption, const std::string &description, RequiredType required, _elementType *value)
Definition cmnCommandLineOptions.h:248
Base class for high level objects.
Definition cmnGenericObject.h:53
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:325
bool cmnCommandLineOptionsConvert(const char *value, _elementType &element)
Definition cmnCommandLineOptions.h:40
bool cmnCommandLineOptionsConvert< std::string >(const char *value, std::string &element)
Definition cmnCommandLineOptions.h:59
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Defines cmnGenericObject.
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
Portability across compilers and operating systems tools.
Break strings into tokens.
const_iterator end(void) const
Definition vctDynamicConstMatrixBase.h:209
OwnerType::const_iterator const_iterator
Definition vctDynamicConstMatrixBase.h:95