25#ifndef __CMD_ARGS_HPP__
26#define __CMD_ARGS_HPP__
42 std::vector<std::pair<std::string, std::string>>
key_desc_;
48 std::map<std::string, std::string>
keys_;
51 std::vector<T> get_vector(std::string
const key__)
const
53 auto s =
keys_.at(key__);
54 std::replace(s.begin(), s.end(),
':',
' ');
55 std::istringstream iss(s);
65 template <
typename T, std::
size_t N>
66 std::array<T, N> get_array(std::string
const key__)
const
68 auto v = this->get_vector<T>(key__);
71 s <<
"[cmd_args::get_array] wrong size of array for the command-line argument " << key__ << std::endl
72 <<
"[cmd_args::get_array] expected size : " << N <<
", provided size : " << v.size();
73 throw std::runtime_error(s.str());
77 std::copy(v.begin(), v.end(), out.begin());
82 void check_for_key(std::string
const key__)
const;
98 cmd_args(
int argn__,
char** argv__, std::initializer_list<std::pair<std::string, std::string>> keys__);
100 void register_key(std::string
const key__, std::string
const description__);
102 void parse_args(
int argn__,
char** argv__);
106 inline bool exist(
const std::string key__)
const
108 return keys_.count(key__);
112 template <
typename T>
113 inline T
value(std::string
const key__)
const
115 check_for_key(key__);
117 std::istringstream(
keys_.at(key__)) >> v;
138 template <
typename T>
139 inline T
value(std::string
const key__, T default_val__)
const
142 return default_val__;
145 std::istringstream(
keys_.at(key__)) >> v;
149 template <
typename T>
150 inline std::vector<T>
value(std::string
const key__, std::vector<T> default_val__)
const
153 return default_val__;
155 return get_vector<T>(key__);
158 template <
typename T, std::
size_t N>
159 inline std::array<T, N>
value(std::string
const key__, std::array<T, N> default_val__)
const
162 return default_val__;
164 return get_array<T, N>(key__);
167 std::string operator[](
const std::string key__)
const
169 return keys_.at(key__);
172 std::map<std::string, std::string> keys()
const
179inline std::string cmd_args::value<std::string>(
const std::string key__)
const
181 return keys_.at(key__);
185inline std::string cmd_args::value<std::string>(
const std::string key__,
const std::string default_val__)
const
188 return default_val__;
190 return keys_.at(key__);
194inline std::vector<double> cmd_args::value<std::vector<double>>(
const std::string key__)
const
196 check_for_key(key__);
197 return get_vector<double>(key__);
201inline std::vector<int> cmd_args::value<std::vector<int>>(
const std::string key__)
const
203 check_for_key(key__);
204 return get_vector<int>(key__);
Simple command line arguments handler.
std::vector< std::pair< std::string, std::string > > key_desc_
Helper string for each key.
T value(std::string const key__, T default_val__) const
Get a value if key exists or return a default value.
T value(std::string const key__) const
Get a value or terminate if key is not found.
std::map< std::string, int > known_keys_
Mapping between a key and its kind (with or without value).
std::map< std::string, std::string > keys_
Key to value mapping.
void copy(T *target__, T const *source__, size_t n__)
Copy memory inside a device.
Namespace of the SIRIUS library.