1 #ifndef CASM_InputParser_impl
2 #define CASM_InputParser_impl
14 template <
typename... Args>
17 parse(*
this, std::forward<Args>(args)...);
23 template <
typename... Args>
25 bool _required, Args &&... args)
28 parse(*
this, std::forward<Args>(args)...);
34 template <
typename CustomParse>
43 template <
typename CustomParse>
45 fs::path _path,
bool _required)
53 template <
typename RequiredType,
typename... Args>
56 auto it =
self.find_at(option);
57 std::unique_ptr<RequiredType> res;
58 if (it ==
self.end()) {
59 std::stringstream msg;
60 msg <<
"Error: missing required option '" << option.string() <<
"'.";
66 return it->template make<RequiredType>(std::forward<Args>(args)...);
67 }
catch (std::exception &e) {
68 std::stringstream msg;
69 msg <<
"Error: could not construct type '"
70 << CASM::type_name<RequiredType>() <<
"' from option '"
71 << option.string() <<
"'.";
78 template <
typename RequiredType,
typename... Args>
81 auto it =
self.find_at(option);
82 std::unique_ptr<RequiredType> res;
83 if (it ==
self.end()) {
84 std::stringstream msg;
85 msg <<
"Error: missing required option '" << option.string() <<
"'.";
91 it->template get<RequiredType>(value, std::forward<Args>(args)...);
93 }
catch (std::exception &e) {
94 std::stringstream msg;
95 msg <<
"Error: could not construct type '"
96 << CASM::type_name<RequiredType>() <<
"' from option '"
97 << option.string() <<
"'.";
103 template <
typename T>
104 template <
typename RequiredType,
typename... Args>
108 if (option.empty()) {
111 auto it =
self.
find_at(option);
112 if (it ==
self.end()) {
113 return std::unique_ptr<RequiredType>();
120 return ptr->
make<RequiredType>(std::forward<Args>(args)...);
121 }
catch (std::exception &e) {
122 std::stringstream msg;
123 msg <<
"Error: could not construct type '"
124 << CASM::type_name<RequiredType>() <<
"' from option '"
125 << option.string() <<
"'.";
127 return std::unique_ptr<RequiredType>();
131 template <
typename T>
132 template <
typename RequiredType,
typename... Args>
136 if (option.empty()) {
139 auto it =
self.
find_at(option);
140 if (it ==
self.end()) {
148 ptr->
get<RequiredType>(value, std::forward<Args>(args)...);
150 }
catch (std::exception &e) {
151 std::stringstream msg;
152 msg <<
"Error: could not construct type '"
153 << CASM::type_name<RequiredType>() <<
"' from option '"
154 << option.string() <<
"'.";
160 template <
typename T>
161 template <
typename RequiredType,
typename... Args>
163 const RequiredType &_default,
166 if (option.empty()) {
169 auto it =
self.
find_at(option);
170 if (it ==
self.end()) {
178 return ptr->
get<RequiredType>(std::forward<Args>(args)...);
179 }
catch (std::exception &e) {
180 std::stringstream msg;
181 msg <<
"Error: could not construct type '"
182 << CASM::type_name<RequiredType>() <<
"' from option '"
183 << option.string() <<
"'.";
189 template <
typename T>
190 template <
typename RequiredType,
typename... Args>
192 const RequiredType &_default,
195 if (option.empty()) {
198 auto it =
self.
find_at(option);
199 if (it ==
self.end()) {
208 ptr->
get<RequiredType>(value, std::forward<Args>(args)...);
210 }
catch (std::exception &e) {
211 std::stringstream msg;
212 msg <<
"Error: could not construct type '"
213 << CASM::type_name<RequiredType>() <<
"' from option '"
214 << option.string() <<
"'.";
220 template <
typename T>
221 template <
typename RequiredType,
typename... Args>
223 fs::path option, Args &&... args) {
224 auto subparser = std::make_shared<InputParser<RequiredType>>(
225 this->input, this->relpath(option),
true, std::forward<Args>(args)...);
226 subparser->type_name = CASM::type_name<RequiredType>();
227 insert(subparser->path, subparser);
231 template <
typename T>
232 template <
typename RequiredType,
typename... Args>
234 fs::path option, Args &&... args) {
235 auto subparser = std::make_shared<InputParser<RequiredType>>(
236 this->input, this->relpath(option),
false, std::forward<Args>(args)...);
237 subparser->type_name = CASM::type_name<RequiredType>();
238 insert(subparser->path, subparser);
242 template <
typename T>
243 template <
typename RequiredType,
typename... Args>
245 fs::path option,
const RequiredType &_default, Args &&... args) {
247 subparse_if<RequiredType>(option, std::forward<Args>(args)...);
248 if (!subparser->exists()) {
249 subparser->value = notstd::make_unique<RequiredType>(_default);
254 template <
typename T>
255 template <
typename RequiredType,
typename... Args>
258 auto subparser = std::make_shared<InputParser<RequiredType>>(
259 this->input, this->path,
true, std::forward<Args>(args)...);
260 subparser->type_name = CASM::type_name<RequiredType>();
261 insert(subparser->path, subparser);
265 template <
typename ErrorType>
268 if (!parser.
valid()) {
273 log.
indent() << report << std::endl << std::endl;
281 log.
indent() << report << std::endl << std::endl;
285 template <
typename T>
289 parser.
value = parser.
self.template make<T>();
290 }
catch (std::exception &e) {
291 parser.
error.insert(std::string(
"Error: could not construct type '") +
292 CASM::type_name<T>() +
"'.");
jsonParser::iterator find_at(const fs::path &path)
Return iterator to sub-object or element, or 'end' if not found.
std::unique_ptr< T > make(Args &&... args) const
Get data from json.
T get(Args &&... args) const
Get data from json, using one of several alternatives.
void print_errors(KwargsParser const &parser, Log &log, std::string header="Errors")
Formatted print error messages, including all subparsers.
void print_warnings(KwargsParser const &parser, Log &log, std::string header="Warnings")
Formatted print warning messages, including all subparsers.
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
jsonParser make_report(KwargsParser const &parser)
Return parser.input with error and warning messages added in place.
void parse(InputParser< ConfigEnumOptions > &parser, std::string method_name, PrimClex const &primclex, DataFormatterDictionary< Configuration > const &dict)
bool valid() const
Return true if this and and all subparsers are valid.
std::map< fs::path, std::set< std::string > > all_warnings() const
Return warning messages from this and all subparsers.
std::set< std::string > error