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