KSSUtil  v14.2.0
C++ general utility library
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 <kss/contract/all.h>
25 
26 #include "convert.hpp"
27 
28 namespace kss { namespace util { namespace po {
29 
33  enum class HasArgument {
34  none = 0,
35  required,
36  optional
37  };
38 
43  static constexpr char noShortOption = char(0);
44 
49  static constexpr char autoShortOption = char(1);
50 
54  struct Option {
55  std::string name;
56  std::string description;
57  char shortOption = autoShortOption;
59  std::string defaultValue;
60  };
61 
70  public:
71 
75  explicit ProgramOptions(std::initializer_list<Option> options = std::initializer_list<Option>());
77 
78  // Moving is allowed, but copying is not.
81 
82  ProgramOptions(const ProgramOptions&) = delete;
83  ProgramOptions& operator=(const ProgramOptions&) = delete;
84 
91  void add(const Option& o);
92  void add(Option&& o);
93  void add(std::initializer_list<Option> options);
94 
95  template <class InputIterator>
96  void add(InputIterator first, InputIterator last) {
97  kss::contract::parameters({
98  KSS_EXPR(first != last)
99  });
100  for (auto it = first; it != last; ++it) {
101  add(*it);
102  }
103  }
104 
114  void parse(int argc,
115  const char * const * argv,
116  bool ignoreUnknownOptions = false);
117 
122  std::string usage() const;
123 
129  bool hasOption(const std::string& name) const;
130 
139  template <class T>
140  T option(const std::string& name) const {
141  const auto s = rawOptionValue(name);
142  return kss::util::strings::convert<T>(s);
143  }
144 
145  private:
146  struct Impl;
147  std::unique_ptr<Impl> _impl;
148 
149  std::string rawOptionValue(const std::string& name) const;
150  };
151 }}}
152 
153 #endif
kss::util::po::Option::name
std::string name
the text used as the command line argument
Definition: programoptions.hpp:55
kss::util::po::ProgramOptions::parse
void parse(int argc, const char *const *argv, bool ignoreUnknownOptions=false)
kss::util::po::ProgramOptions::add
void add(const Option &o)
kss::util::po::Option::description
std::string description
one line description used in the help text
Definition: programoptions.hpp:56
kss::util::po::ProgramOptions::~ProgramOptions
~ProgramOptions()
kss::util::po::ProgramOptions
Command line argument parsing.
Definition: programoptions.hpp:69
kss::util::po::ProgramOptions::add
void add(InputIterator first, InputIterator last)
Definition: programoptions.hpp:96
kss::util::rtti::name
std::string name(const T &t=T())
Definition: rtti.hpp:47
kss::util::po::ProgramOptions::operator=
ProgramOptions & operator=(ProgramOptions &&) noexcept
kss::util::po::Option::defaultValue
std::string defaultValue
default value if the option isn't present
Definition: programoptions.hpp:59
kss::util::po::HasArgument
HasArgument
Definition: programoptions.hpp:33
kss::util::po::ProgramOptions::option
T option(const std::string &name) const
Definition: programoptions.hpp:140
kss::util::po::HasArgument::optional
@ optional
an argument is allowed, but not required
kss::util::po::Option::hasArg
HasArgument hasArg
specifies if the option takes an argument
Definition: programoptions.hpp:58
kss::util::po::ProgramOptions::hasOption
bool hasOption(const std::string &name) const
kss::util::po::Option::shortOption
char shortOption
optional short form of the option
Definition: programoptions.hpp:57
convert.hpp
Convert strings to other types.
kss::util::po::ProgramOptions::usage
std::string usage() const
kss
All Klassen Software Solutions libraries begin with this namespace.
Definition: add_rel_ops.hpp:19
kss::util::po::HasArgument::none
@ none
no argument is allowed
kss::util::po::HasArgument::required
@ required
an argument is required
kss::util::po::Option
Definition: programoptions.hpp:54
kss::util::po::ProgramOptions::ProgramOptions
ProgramOptions(std::initializer_list< Option > options=std::initializer_list< Option >())