KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
argumentvector.hpp
Go to the documentation of this file.
1 //
2 // argumentvector.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2019-03-30.
6 // Copyright © 2019 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
15 #ifndef argumentvector_hpp
16 #define argumentvector_hpp
17 
18 #include <initializer_list>
19 #include <stdexcept>
20 #include <string>
21 #include <vector>
22 
23 namespace kss { namespace util { namespace po {
24 
47  public:
48 
53  ArgumentVector() = default;
54  explicit ArgumentVector(std::initializer_list<std::string> args);
55  ArgumentVector(const ArgumentVector& av);
56  ArgumentVector(ArgumentVector&&) = default;
59  ~ArgumentVector() = default;
60 
64  void add(const std::string& arg);
65  void add(std::string&& arg);
66  void add(std::initializer_list<std::string> args);
67 
68  template <class InputIterator>
69  void add(InputIterator first, InputIterator last) {
70  if (first == last) {
71  throw std::invalid_argument("first and last iterator are the same");
72  }
73  for (auto it = first; it != last; ++it) {
74  argumentStrings.push_back(*it);
75  }
76  rebuild();
77  }
78 
82  int argc() const noexcept;
83 
88  char* const* argv() noexcept;
89  const char* const* argv() const noexcept;
90 
91  private:
92  std::vector<std::string> argumentStrings;
93  std::vector<char*> argumentPointers;
94 
95  void rebuild();
96  };
97 
98 }}}
99 
100 #endif /* argumentvector_hpp */
int argc() const noexcept
void add(InputIterator first, InputIterator last)
char *const * argv() noexcept
void add(const std::string &arg)
ArgumentVector & operator=(const ArgumentVector &av)
Creates argc/argv type groups based on lists of strings.