KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
programoptions.hpp
Go to the documentation of this file.
1 //
2 // programoptions.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2014-04-18.
6 // Copyright (c) 2014 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
15 #ifndef kssutil_programoptions_hpp
16 #define kssutil_programoptions_hpp
17 
18 #include <initializer_list>
19 #include <memory>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 
24 #include "convert.hpp"
25 
26 namespace kss { namespace util { namespace po {
27 
31  enum class HasArgument {
32  none = 0,
33  required,
34  optional
35  };
36 
41  static constexpr char noShortOption = char(0);
42 
47  static constexpr char autoShortOption = char(1);
48 
52  struct Option {
53  std::string name;
54  std::string description;
55  char shortOption = autoShortOption;
57  std::string defaultValue;
58  };
59 
68  public:
69 
73  explicit ProgramOptions(std::initializer_list<Option> options = std::initializer_list<Option>());
75 
76  // Moving is allowed, but copying is not.
79 
80  ProgramOptions(const ProgramOptions&) = delete;
81  ProgramOptions& operator=(const ProgramOptions&) = delete;
82 
89  void add(const Option& o);
90  void add(Option&& o);
91  void add(std::initializer_list<Option> options);
92 
93  template <class InputIterator>
94  void add(InputIterator first, InputIterator last) {
95  if (first == last) {
96  throw std::invalid_argument("first and last iterator are the same");
97  }
98  for (auto it = first; it != last; ++it) {
99  add(*it);
100  }
101  }
102 
112  void parse(int argc,
113  const char * const * argv,
114  bool ignoreUnknownOptions = false);
115 
120  std::string usage() const;
121 
127  bool hasOption(const std::string& name) const;
128 
137  template <class T>
138  T option(const std::string& name) const {
139  const auto s = rawOptionValue(name);
140  return kss::util::strings::convert<T>(s);
141  }
142 
143  private:
144  struct Impl;
145  std::unique_ptr<Impl> _impl;
146 
147  std::string rawOptionValue(const std::string& name) const;
148  };
149 }}}
150 
151 #endif
HasArgument hasArg
specifies if the option takes an argument
no argument is allowed
char shortOption
optional short form of the option
void add(const Option &o)
std::string description
one line description used in the help text
std::string name
the text used as the command line argument
bool hasOption(const std::string &name) const
an argument is allowed, but not required
ProgramOptions(std::initializer_list< Option > options=std::initializer_list< Option >())
std::string defaultValue
default value if the option isn&#39;t present
std::string name(const T &t=T())
Definition: rtti.hpp:47
void parse(int argc, const char *const *argv, bool ignoreUnknownOptions=false)
an argument is required
Command line argument parsing.
void add(InputIterator first, InputIterator last)
Convert strings to other types.
T option(const std::string &name) const
std::string usage() const
ProgramOptions & operator=(ProgramOptions &&) noexcept