Utilities  1
Collection of utility classes and functions used by my C++ applications.
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Friends Macros
argumentparser.h
Go to the documentation of this file.
1 #ifndef ARGUMENTPARSER_H
2 #define ARGUMENTPARSER_H
3 
4 #include "global.h"
5 
6 #include <string>
7 #include <vector>
8 #include <list>
9 #include <initializer_list>
10 #include <functional>
11 #include <stdexcept>
12 
13 namespace ApplicationUtilities {
14 
15 class Argument;
16 
18 
19 typedef std::initializer_list<Argument *> ArgumentInitializerList;
20 typedef std::vector<Argument *> ArgumentVector;
21 typedef std::vector<std::string> StringVector;
22 typedef std::list<std::string> StringList;
23 typedef std::function<bool (Argument *)> ArgumentPredicate;
24 
25 Argument *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except);
26 
28 {
29  friend class ArgumentParser;
30 
31 public:
32  typedef std::function <void (const StringVector &)> CallbackFunction;
33 
34  Argument(const std::string &name, const std::string abbreviation = std::string(), const std::string &description = std::string());
35  Argument(const char *name, const char *abbreviation = nullptr, const char *description = nullptr);
36  ~Argument();
37 
38  const std::string &name() const;
39  void setName(const std::string &name);
40  const std::string &abbreviation() const;
41  void setAbbreviation(const std::string &abbreviation);
42  const std::string &description() const;
43  void setDescription(const std::string &description);
44  const StringVector &values() const;
45  const std::string &value(StringVector::size_type index) const;
46  StringVector::size_type valueCount() const;
47  int requiredValueCount() const;
48  void setRequiredValueCount(int requiredValueCount);
49  const StringList &valueNames() const;
50  void setValueNames(std::initializer_list<std::string> valueNames);
51  void appendValueName(const char *valueName);
52  void appendValueName(const std::string &valueName);
53  bool allRequiredValuesPresent() const;
54  bool isPresent() const;
55  bool isRequired() const;
56  void setRequired(bool value);
57  bool isCombinable() const;
58  void setCombinable(bool value);
59  void setCallback(CallbackFunction callback);
60  void printInfo(std::ostream &os) const;
61  const ArgumentVector &secondaryArguments() const;
62  void setSecondaryArguments(const ArgumentInitializerList &secondaryArguments);
63  bool hasSecondaryArguments() const;
64  const ArgumentVector parents() const;
65  bool isMainArgument() const;
66  std::string parentNames() const;
67  bool isParentPresent() const;
68  Argument *conflictsWithArgument() const;
69 
70 private:
71  std::string m_name;
72  std::string m_abbreviation;
73  std::string m_description;
74  bool m_required;
75  bool m_combinable;
76  int m_requiredValueCount;
77  StringList m_valueNames;
78  bool m_present;
79  StringVector m_values;
80  ArgumentVector m_secondaryArgs;
81  CallbackFunction m_callbackFunction;
82  ArgumentVector m_parents;
83  bool m_isMainArg;
84 };
85 
92 inline const std::string &Argument::name() const
93 {
94  return m_name;
95 }
96 
105 inline void Argument::setName(const std::string &name)
106 {
107  if(name.empty() || name.find(' ') != std::string::npos || name.find('=') != std::string::npos) {
108  throw std::invalid_argument("name mustn't be empty or contain white spaces or equation chars");
109  }
110  m_name = name;
111 }
112 
119 inline const std::string &Argument::abbreviation() const
120 {
121  return m_abbreviation;
122 }
123 
133 inline void Argument::setAbbreviation(const std::string &abbreviation)
134 {
135  if(!abbreviation.empty() && (abbreviation.find(' ') != std::string::npos || abbreviation.find('=') != std::string::npos)) {
136  throw std::invalid_argument("abbreviation mustn't contain white spaces or equation chars");
137  }
138  m_abbreviation = abbreviation;
139 }
140 
146 inline const std::string &Argument::description() const
147 {
148  return m_description;
149 }
150 
156 inline void Argument::setDescription(const std::string &description)
157 {
158  m_description = description;
159 }
160 
165 inline const StringVector &Argument::values() const
166 {
167  return m_values;
168 }
169 
174 inline const std::string &Argument::value(StringVector::size_type index) const
175 {
176  return m_values.at(index);
177 }
178 
183 inline StringVector::size_type Argument::valueCount() const
184 {
185  return m_values.size();
186 }
187 
200 {
201  return m_requiredValueCount;
202 }
203 
215 inline void Argument::setRequiredValueCount(int requiredValueCount)
216 {
217  m_requiredValueCount = requiredValueCount;
218 }
219 
227 inline const StringList &Argument::valueNames() const
228 {
229  return m_valueNames;
230 }
231 
244 inline void Argument::setValueNames(std::initializer_list<std::string> valueNames)
245 {
246  m_valueNames = std::list<std::string>(valueNames);
247 }
248 
253 inline void Argument::appendValueName(const char *valueName)
254 {
255  m_valueNames.emplace_back(valueName);
256 }
257 
265 inline void Argument::appendValueName(const std::string &valueName)
266 {
267  m_valueNames.push_back(valueName);
268 }
269 
274 {
275  if(m_requiredValueCount > 0) {
276  return m_values.size() >= static_cast<unsigned int>(m_requiredValueCount);
277  } else {
278  return true;
279  }
280 }
281 
286 inline bool Argument::isPresent() const
287 {
288  return m_present;
289 }
290 
298 inline bool Argument::isRequired() const
299 {
300  return m_required;
301 }
302 
310 inline void Argument::setRequired(bool value)
311 {
312  m_required = value;
313 }
314 
323 inline bool Argument::isCombinable() const
324 {
325  return m_combinable;
326 }
327 
336 inline void Argument::setCombinable(bool value)
337 {
338  m_combinable = value;
339 }
340 
346 {
347  m_callbackFunction = callback;
348 }
349 
355 inline const ArgumentVector &Argument::secondaryArguments() const
356 {
357  return m_secondaryArgs;
358 }
359 
367 {
368  return !m_secondaryArgs.empty();
369 }
370 
379 inline const ArgumentVector Argument::parents() const
380 {
381  return m_parents;
382 }
383 
390 inline bool Argument::isMainArgument() const
391 {
392  return m_isMainArg;
393 }
394 
396 {
397 public:
398  ArgumentParser();
399 
400  void setMainArguments(const ArgumentInitializerList &mainArguments);
401  void printHelp(std::ostream &os) const;
402  Argument *findArg(const ArgumentPredicate &predicate) const;
403  Argument *findArg(const ArgumentVector &arguments, const ArgumentPredicate &predicate) const;
404  void verifySetup() const;
405  void parseArgs(int argc, char *argv[]);
406  unsigned int actualArgumentCount() const;
407  const std::string &currentDirectory() const;
408  bool areUnknownArgumentsIgnored() const;
409  void setIgnoreUnknownArguments(bool ignore);
410 
411 private:
412  ArgumentVector m_mainArgs;
413  unsigned int m_actualArgc;
414  std::string m_currentDirectory;
415  bool m_ignoreUnknownArgs;
416 };
417 
421 inline unsigned int ArgumentParser::actualArgumentCount() const
422 {
423  return m_actualArgc;
424 }
425 
429 inline const std::string &ArgumentParser::currentDirectory() const
430 {
431  return m_currentDirectory;
432 }
433 
444 {
445  return m_ignoreUnknownArgs;
446 }
447 
456 {
457  m_ignoreUnknownArgs = ignore;
458 }
459 
460 }
461 
462 
463 
464 #endif // ARGUMENTPARSER_H
std::initializer_list< Argument * > ArgumentInitializerList
StringVector::size_type valueCount() const
Returns the number of values which could be found when parsing the command line arguments.
void setDescription(const std::string &description)
Sets the description of the argument.
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
void setCombinable(bool value)
Sets if this argument can be combined.
const StringVector & values() const
Returns the additional values for the argument.
const ArgumentVector & secondaryArguments() const
Returns the secondary arguments for this argument.
const ArgumentVector parents() const
Returns the parents of this argument.
Contains currently only ArgumentParser and related classes.
const std::string & abbreviation() const
Returns the abbreviation of the argument.
Argument * firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except)
This function return the first present and uncombinable argument of the given list of arguments...
std::list< std::string > StringList
#define LIB_EXPORT
This macro marks a symbol for shared library export.
Definition: global.h:50
std::vector< std::string > StringVector
void setRequired(bool value)
Sets if this argument is mandatory or not.
const std::string & value(StringVector::size_type index) const
Returns the value with the give index.
std::function< bool(Argument *)> ArgumentPredicate
bool isRequired() const
Returns an indication whether the argument is mandatory.
bool isCombinable() const
Returns an indication whether the argument is combinable.
void setRequiredValueCount(int requiredValueCount)
Sets the number of values which are required to be given for this argument.
void setName(const std::string &name)
Sets the name of the argument.
void setAbbreviation(const std::string &abbreviation)
Sets the abbreviation of the argument.
const std::string & name() const
Returns the name of the argument.
bool isMainArgument() const
Returns an indication whether the argument is used as main argument.
void appendValueName(const char *valueName)
Appends a value name.
void setValueNames(std::initializer_list< std::string > valueNames)
Sets the names of the requried values.
bool allRequiredValuesPresent() const
Returns an indication whether all required values are present.
The Argument class is a wrapper for command line argument information.
const std::string & currentDirectory() const
Returns the current directory.
void setCallback(CallbackFunction callback)
Sets a callback function which will be called by the parser if the argument could be found and no par...
unsigned int actualArgumentCount() const
Returns the actual number of arguments that could be found when parsing.
const StringList & valueNames() const
Returns the names of the requried values.
const std::string & description() const
Returns the description of the argument.
std::function< void(const StringVector &)> CallbackFunction
int requiredValueCount() const
Returns the number of values which are required to be given for this argument.
bool hasSecondaryArguments() const
Returns an indication whether the argument has secondary arguments.
void setIgnoreUnknownArguments(bool ignore)
Set to true if the parser should ignore unknown arguments when parsing.
The ArgumentParser class provides a means for handling command line arguments.
bool areUnknownArgumentsIgnored() const
Returns an indication whether unknown arguments detected when parsing should be ignored.
std::vector< Argument * > ArgumentVector