cmdopt2/parser: remove ostream usage in favour of FILE*

This commit is contained in:
Danny Robson 2022-03-21 10:49:51 +10:00
parent 9caba84fc6
commit 787b91c6ef
2 changed files with 14 additions and 38 deletions

View File

@ -217,60 +217,37 @@ parser::parse (int const argc, const char *const *argv)
///////////////////////////////////////////////////////////////////////////////
template <typename OutputT>
static void
usage (
int argc,
char const * const* argv,
std::vector<cruft::cmdopt2::positional_t> const &positional,
std::vector<cruft::cmdopt2::keyword_t> const &keyword,
OutputT &output
) {
fmt::print (output, "Usage:");
void
parser::usage (int argc, char const * const* argv, FILE *fp) const
{
if (argc > 0)
fmt::print (output, " {}", argv[0]);
fmt::print (fp, " {}", argv[0]);
static char constexpr OPTIONAL[2] { '[', ']' };
static char constexpr REQUIRED[2] { '<', '>' };
for (auto const &arg: keyword) {
for (auto const &arg: m_keyword) {
auto const &delimiters = arg.required () ? REQUIRED : OPTIONAL;
if (arg.short_) {
fmt::print (output, FMT_STRING (" {}-{}"), delimiters[0], *arg.short_);
fmt::print (fp, FMT_STRING (" {}-{}"), delimiters[0], *arg.short_);
if (arg.acceptor1)
fmt::print (output, FMT_STRING (" <{}>"), arg.name);
fmt::print (output, FMT_STRING ("{}"), delimiters[1]);
fmt::print (fp, FMT_STRING (" <{}>"), arg.name);
fmt::print (fp, FMT_STRING ("{}"), delimiters[1]);
}
if (arg.long_) {
fmt::print (output, FMT_STRING (" {}--{}"), delimiters[0], *arg.long_);
fmt::print (fp, FMT_STRING (" {}--{}"), delimiters[0], *arg.long_);
if (arg.acceptor1)
fmt::print (output, FMT_STRING (" <{}>"), arg.name);
fmt::print (output, FMT_STRING ("{}"), delimiters[1]);
fmt::print (fp, FMT_STRING (" <{}>"), arg.name);
fmt::print (fp, FMT_STRING ("{}"), delimiters[1]);
}
}
for (auto const &arg: positional) {
for (auto const &arg: m_positional) {
auto const &delimiters = arg.required () ? REQUIRED : OPTIONAL;
fmt::print (output, FMT_STRING (" {}{}{}"), delimiters[0], arg.name, delimiters[1]);
fmt::print (fp, FMT_STRING (" {}{}{}"), delimiters[0], arg.name, delimiters[1]);
}
fmt::print (output, "\n");
}
//-----------------------------------------------------------------------------
void
parser::usage (int argc, char const * const* argv, std::ostream &os) const
{
::usage (argc, argv, m_positional, m_keyword, os);
}
//-----------------------------------------------------------------------------
void
parser::usage (int argc, char const * const* argv, FILE *fp) const
{
::usage (argc, argv, m_positional, m_keyword, fp);
fmt::print (fp, "\n");
}

View File

@ -23,7 +23,6 @@ namespace cruft::cmdopt2 {
keyword_t& add (keyword_t const&) &;
void usage (int argc, char const * const* argv, FILE*) const;
void usage (int argc, char const * const* argv, std::ostream&) const;
private:
int parse_named (int argc, char const* const* argv, int*) const;