creativity  v1.3.0
Agent-based model of creativity and piracy
CmdArgs.hpp
1 #pragma once
2 #include "creativity/cmdargs/Validation.hpp"
3 #include "creativity/cmdargs/strings.hpp"
4 #include <boost/program_options/options_description.hpp>
5 #include <boost/program_options/positional_options.hpp>
6 #include <boost/program_options/value_semantic.hpp>
7 #include <string>
8 #include <limits>
9 #include <type_traits>
10 #include <vector>
11 
12 namespace boost { namespace program_options { class variables_map; } }
13 
14 namespace creativity {
16 namespace cmdargs {
17 
19 template <typename T> constexpr T inf_or_max() { return std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max(); }
21 template <typename T> constexpr T neginf_or_lowest() { return std::numeric_limits<T>::has_infinity ? -std::numeric_limits<T>::infinity() : std::numeric_limits<T>::lowest(); }
22 
31 class CmdArgs {
32 
33  protected:
35  CmdArgs() = default;
36 
37  public:
54  void parse(int argc, char const* const* argv);
55 
61  template <typename T>
62  static typename std::enable_if<not std::is_unsigned<T>::value and not std::is_same<T, bool>::value,
63  boost::program_options::typed_value<T>*
64  >::type
65  value(T& store) {
66  return boost::program_options::value<T>(&store)->default_value(store)->value_name(type_string<T>());
67  }
68 
74  template <typename T>
75  static typename std::enable_if<std::is_unsigned<T>::value and not std::is_same<T, bool>::value,
76  boost::program_options::typed_value<Validation<T>>*>::type
77  value(T &storage) {
78  return value<Validation<T>>(storage);
79  }
80 
84  static boost::program_options::typed_value<bool>* value(bool& store) {
85  return boost::program_options::bool_switch(&store)->default_value(store);
86  }
87 
96  template <typename V>
97  static boost::program_options::typed_value<V>*
98  value(typename V::value_type &store) {
99  return boost::program_options::value<V>()->default_value(store)->value_name(V::validationString())
100  ->notifier([&store](const V &v) { store = v; /* NB: implicit conversion */ });
101  }
102 
112  template <typename V, typename A>
113  static boost::program_options::typed_value<std::vector<V>>*
114  value(std::vector<typename V::value_type, A> &store) {
115  return boost::program_options::value<std::vector<V>>()->value_name(
116  V::validationString() + " [" + V::validationString() + " ...]")
117  ->notifier([&store](const std::vector<V> &v) {
118  store.clear();
119  store.reserve(v.size());
120  for (const auto &val : v) store.push_back((const typename V::value_type) val);
121  });
122  }
123 
126  template <typename T, typename A>
127  typename std::enable_if<not std::is_unsigned<T>::value, boost::program_options::typed_value<std::vector<T, A>>*>::type
128  value(std::vector<T, A> &store) {
129  return boost::program_options::value<std::vector<T, A>>(&store)->value_name(
130  type_string<T>() + " [" + type_string<T>() + " ...]");
131  }
132 
135  template <typename T, typename A>
136  typename std::enable_if<std::is_unsigned<T>::value, boost::program_options::typed_value<std::vector<Validation<T>>>*>::type
137  value(std::vector<T, A> &store) {
138  return value<Validation<T>>(store);
139  }
140 
142  template <long minimum, long denom = 1, typename T>
143  static inline boost::program_options::typed_value<Min<T, minimum, denom>>* min(T &store) { return value<Min<T, minimum, denom>>(store); }
144 
146  template <long maximum, long denom = 1, typename T>
147  static boost::program_options::typed_value<Max<T, maximum, denom>>* max(T &store) { return value<Max<T, maximum, denom>>(store); }
148 
150  template <long min, long max, long denom = 1, typename T>
151  static boost::program_options::typed_value<Range<T, min, max, denom>>* range(T &store) { return value<Range<T, min, max, denom>>(store); }
152 
154  template <long lower, long denom = 1, typename T>
155  static boost::program_options::typed_value<Above<T, lower, denom>>* above(T &store) { return value<Above<T, lower, denom>>(store); }
156 
158  template <long upper, long denom = 1, typename T>
159  static boost::program_options::typed_value<Below<T, upper, denom>>* below(T &store) { return value<Below<T, upper, denom>>(store); }
160 
162  virtual std::string version() const;
163 
168  virtual std::string versionSuffix() const;
169 
173  virtual std::string usage() const;
174 
176  virtual std::string help() const;
177 
178  protected:
184  virtual void addOptions();
185 
187  std::string prog_name_;
188 
190  boost::program_options::options_description options_;
191 
193  boost::program_options::options_description invisible_;
194 
196  boost::program_options::positional_options_description positional_;
197 
203  virtual void postParse(boost::program_options::variables_map &vars);
204 };
205 
206 
207 
208 
209 
210 }}
static boost::program_options::typed_value< Min< T, minimum, denom > > * min(T &store)
Shortcut for value<Min<T, n, d>>(val) with T last (so that it can be inferred from val) ...
Definition: CmdArgs.hpp:143
This class handles command line argument parsing.
Definition: CmdArgs.hpp:31
constexpr T neginf_or_lowest()
constexpr that returns negative infinity, if T has such a value, or the lowest value T supports if it...
Definition: CmdArgs.hpp:21
static boost::program_options::typed_value< Below< T, upper, denom > > * below(T &store)
Shortcut for value<Below<T, b, d>>(val) with T last (so that it can be inferred from val) ...
Definition: CmdArgs.hpp:159
Definition: CLI.hpp:5
Primary namespace for all Creativity library code.
Definition: config.hpp:4
static boost::program_options::typed_value< bool > * value(bool &store)
Creates an option value for a boolean value, that is, for an switch without an argument, with default value as given in store.
Definition: CmdArgs.hpp:84
static boost::program_options::typed_value< V > * value(typename V::value_type &store)
Creates an option value object with explicit validation wrapper class V.
Definition: CmdArgs.hpp:98
static std::enable_if< not std::is_unsigned< T >::value and not std::is_same< T, bool >::value, boost::program_options::typed_value< T > *>::type value(T &store)
Creates an option value object without any special validation wrapper class.
Definition: CmdArgs.hpp:65
std::string prog_name_
The program name, populated by parse().
Definition: CmdArgs.hpp:187
constexpr T inf_or_max()
constexpr that returns positive infinity, if T has such a value, or the maximum value T supports if i...
Definition: CmdArgs.hpp:19
static boost::program_options::typed_value< Above< T, lower, denom > > * above(T &store)
Shortcut for value<Above<T, a, d>>(val) with T last (so that it can be inferred from val) ...
Definition: CmdArgs.hpp:155
static boost::program_options::typed_value< Max< T, maximum, denom > > * max(T &store)
Shortcut for value<Max<T, n, d>>(val) with T last (so that it can be inferred from val) ...
Definition: CmdArgs.hpp:147
boost::program_options::positional_options_description positional_
Positional options object.
Definition: CmdArgs.hpp:196
std::enable_if< not std::is_unsigned< T >::value, boost::program_options::typed_value< std::vector< T, A > > * >::type value(std::vector< T, A > &store)
Takes a std::vector of values for options that store multiple values.
Definition: CmdArgs.hpp:128
std::enable_if< std::is_unsigned< T >::value, boost::program_options::typed_value< std::vector< Validation< T > > > * >::type value(std::vector< T, A > &store)
Takes a std::vector of values for options that store multiple values.
Definition: CmdArgs.hpp:137
boost::program_options::options_description options_
The options descriptions variable for all options.
Definition: CmdArgs.hpp:190
static boost::program_options::typed_value< Range< T, min, max, denom > > * range(T &store)
Shortcut for value<Range<T, a, b, d>>(val) with T last (so that it can be inferred from val) ...
Definition: CmdArgs.hpp:151
boost::program_options::options_description invisible_
Like options_, but for hidden options that aren&#39;t to be displayed in –help output.
Definition: CmdArgs.hpp:193
static std::enable_if< std::is_unsigned< T >::value and not std::is_same< T, bool >::value, boost::program_options::typed_value< Validation< T > > * >::type value(T &storage)
Creates an option value object around an unsigned primitive type, with automatic value storage and de...
Definition: CmdArgs.hpp:77
static boost::program_options::typed_value< std::vector< V > > * value(std::vector< typename V::value_type, A > &store)
Creates an option value object around a vector of options with validation wrapper class V applied to ...
Definition: CmdArgs.hpp:114