diff --git a/ostd/argparse.hh b/ostd/argparse.hh index 0b7b325..d5ef056 100644 --- a/ostd/argparse.hh +++ b/ostd/argparse.hh @@ -16,6 +16,7 @@ #ifndef OSTD_ARGPARSE_HH #define OSTD_ARGPARSE_HH +#include #include #include #include @@ -23,6 +24,7 @@ #include #include #include +#include #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 +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 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(), + "argument requires format '%s' (got '%s')", fmts, istr + ).get()}; + } + }, argst); + }; +} + /** @} */ } /* namespace ostd */