add arg_store_format

master
Daniel Kolesa 2017-05-19 01:37:45 +02:00
parent b9c7de76eb
commit 7fee6b7b05
1 changed files with 19 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#ifndef OSTD_ARGPARSE_HH
#define OSTD_ARGPARSE_HH
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <optional>
@ -23,6 +24,7 @@
#include <memory>
#include <stdexcept>
#include <utility>
#include <functional>
#include "ostd/algorithm.hh"
#include "ostd/format.hh"
@ -663,6 +665,23 @@ auto arg_store_false(bool &ref) {
return arg_store_const(false, ref);
}
template<typename ...A>
auto arg_store_format(string_range fmt, A &...args) {
/* TODO: use ostd::format once it supports reading */
return [fmts = std::string{fmt}, argst = std::tie(args...)](
iterator_range<string_range const *> r
) mutable {
std::apply([&fmts, istr = std::string{r[0]}](auto &...refs) {
if (sscanf(istr.data(), fmts.data(), &refs...) != sizeof...(A)) {
throw arg_error{format(
appender<std::string>(),
"argument requires format '%s' (got '%s')", fmts, istr
).get()};
}
}, argst);
};
}
/** @} */
} /* namespace ostd */