All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
option.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_OPTPARSE_OPTION_HPP
23 #define GEARS_OPTPARSE_OPTION_HPP
24 
25 #include "value.hpp"
26 #include "../enums/operators.hpp"
27 
28 using namespace gears::enums::operators;
29 
30 namespace gears {
31 namespace optparse {
40 enum class trait : char {
41  none = 0,
42  required = 1 << 0,
43  hidden = 1 << 1
44 };
45 
53 struct option {
54 private:
55  using value_type = std::unique_ptr<value_base>;
56  value_type ptr;
57  friend struct option_parser;
58 public:
59  std::string name;
60  std::string help;
62  char alias = '\0';
63 
67  option(std::string name, std::string help = "", value_type value = constant(true), trait flags = trait::none):
68  ptr(std::move(value)), name(std::move(name)), help(std::move(help)), flags(std::move(flags)) {}
72  option(std::string name, char alias, std::string help = "", value_type value = constant(true), trait flags = trait::none):
73  ptr(std::move(value)), name(std::move(name)), help(std::move(help)), flags(std::move(flags)), alias(alias) {}
74 
78  option(char alias, std::string help = "", value_type value = constant(true), trait flags = trait::none):
79  ptr(std::move(value)), help(std::move(help)), flags(std::move(flags)), alias(alias) {}
80 
84  option(const option& other):
85  ptr(other.ptr == nullptr ? nullptr : other.ptr->clone()),
86  name(other.name), help(other.help), flags(other.flags), alias(other.alias) {}
87 
91  option& operator=(const option& other) {
92  ptr = other.ptr == nullptr ? nullptr : other.ptr->clone();
93  name = other.name;
94  help = other.help;
95  flags = other.flags;
96  alias = other.alias;
97  return *this;
98  }
99 
103  option(option&&) = default;
104 
108  option& operator=(option&&) = default;
109 
113  ~option() = default;
114 
118  bool takes_value() const noexcept {
119  return ptr != nullptr && ptr->nargs > 0;
120  }
121 
128  size_t nargs() const noexcept {
129  return takes_value() ? ptr->nargs : 0;
130  }
131 
133 
153  bool is(char arg) const noexcept {
154  return alias == arg;
155  }
156 
157  bool is(const std::string& arg) const noexcept {
158  return name == arg;
159  }
161 
171  template<typename T>
172  const T& get() const {
173  if(ptr == nullptr) {
174  throw std::invalid_argument("option does not take value");
175  }
176  auto val = dynamic_cast<typed_value<T>*>(ptr.get());
177 
178  if(val == nullptr) {
179  throw std::invalid_argument("invalid cast for option");
180  }
181  return val->get();
182  }
183 
192  template<typename T>
193  const T& get_or(const typename std::remove_reference<T>::type& def) const noexcept {
194  if(ptr == nullptr) {
195  return def;
196  }
197 
198  auto val = dynamic_cast<typed_value<T>*>(ptr.get());
199  return val == nullptr ? def : val->get_or(def);
200  }
201 
205  bool is_active() const noexcept {
206  return ptr != nullptr && ptr->is_active();
207  }
208 
215  std::string metavar() const noexcept {
216  return takes_value() ? ptr->metavar : "";
217  }
218 };
219 } // optparse
220 } // gears
221 
222 #endif // GEARS_OPTPARSE_OPTION_HPP