22 #ifndef GEARS_OPTPARSE_OPTION_SET_HPP
23 #define GEARS_OPTPARSE_OPTION_SET_HPP
33 struct option_comparison {
34 bool operator()(
const option& lhs,
const option& rhs)
const {
35 return lhs.name < rhs.name || (lhs.name == rhs.name && lhs.alias < rhs.alias);
52 std::set<option, detail::option_comparison> cache;
53 std::vector<option> options;
66 options.emplace_back(
"help",
'h',
"shows this message and exits");
67 cache.insert(options.back());
83 options.emplace_back(
"help",
'h',
"shows this message and exits");
84 cache.insert(options.back());
105 template<
typename... Args>
106 void add(Args&&... args) {
108 options.emplace_back(std::forward<Args>(args)...);
111 auto&& it = cache.find(options.back());
112 if(it != cache.end()) {
116 cache.insert(options.back());
129 template<
typename Argument>
131 auto&& it = std::find_if(options.begin(), options.end(), [&arg](
const option& opt) {
135 if(it != options.end()) {
153 template<
typename T,
typename Argument>
154 const T&
get(
const Argument& arg)
const {
155 auto&& it = std::find_if(options.begin(), options.end(), [&arg](
const option& opt) {
159 if(it == options.end()) {
160 throw std::invalid_argument(
"option not found");
163 return it->template get<T>();
175 template<
typename T,
typename Argument>
176 const T&
get_or(
const Argument& arg,
const typename std::remove_reference<T>::type& def)
const noexcept {
177 auto&& it = std::find_if(options.begin(), options.end(), [&arg](
const option& opt) {
181 if(it == options.end()) {
185 return it->template get_or<T>(def);
197 template<
typename Argument>
199 auto&& it = std::find_if(options.begin(), options.end(), [&arg](
const option& opt) {
203 return it != options.end() && it->is_active();
210 return options.size();
217 return options.empty();
231 return options.begin();
234 auto begin() const noexcept -> decltype(options.
begin()) {
235 return options.begin();
249 auto end() noexcept -> decltype(options.
end()) {
250 return options.end();
253 auto end() const noexcept -> decltype(options.
end()) {
254 return options.end();
261 #endif // GEARS_OPTPARSE_OPTION_SET_HPP