cmdopt2/args: extract argument::create as a free function

This commit is contained in:
Danny Robson 2022-03-18 15:23:17 +10:00
parent a54ed01dcb
commit 179f016fa1
6 changed files with 83 additions and 82 deletions

View File

@ -1,50 +1,50 @@
#include "./args.hpp" #include "./args.hpp"
using cruft::cmdopt2::positional; using cruft::cmdopt2::positional_t;
using cruft::cmdopt2::keyword; using cruft::cmdopt2::keyword_t;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
positional positional_t
positional::create (char const *name) cruft::cmdopt2::positional (char const *name)
{ {
return create (std::string (name)); return positional (std::string (name));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
positional positional_t
positional::create (std::string &&name) cruft::cmdopt2::positional (std::string &&name)
{ {
positional res {}; positional_t res {};
res.name = name; res.name = name;
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
positional positional_t
positional::ignore (void) const positional_t::ignore (void) const
{ {
positional res = *this; positional_t res = *this;
res.acceptor1.reset (); res.acceptor1.reset ();
return res; return res;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
keyword keyword_t
keyword::create (char const *name) cruft::cmdopt2::keyword (char const *name)
{ {
return create (std::string (name)); return keyword (std::string (name));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::create (std::string &&name) cruft::cmdopt2::keyword (std::string &&name)
{ {
keyword res {}; keyword_t res {};
res.name = name; res.name = name;
res.long_ = name; res.long_ = name;
return res; return res;
@ -52,10 +52,10 @@ keyword::create (std::string &&name)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::flag (void) const keyword_t::flag (void) const
{ {
keyword res = *this; keyword_t res = *this;
res.long_.reset (); res.long_.reset ();
res.short_.reset (); res.short_.reset ();
return res; return res;
@ -63,66 +63,66 @@ keyword::flag (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::flag (char val) const keyword_t::flag (char val) const
{ {
keyword res = *this; keyword_t res = *this;
res.short_ = val; res.short_ = val;
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::flag (std::string_view val) const keyword_t::flag (std::string_view val) const
{ {
keyword res = *this; keyword_t res = *this;
res.long_ = val; res.long_ = val;
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::count (int &val) const keyword_t::count (int &val) const
{ {
CHECK (!acceptor1 and !acceptor0); CHECK (!acceptor1 and !acceptor0);
keyword res = *this; keyword_t res = *this;
res.acceptor0 = [&val] (void) { ++val; }; res.acceptor0 = [&val] (void) { ++val; };
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::present (bool &val) const keyword_t::present (bool &val) const
{ {
CHECK (!acceptor1 and !acceptor0); CHECK (!acceptor1 and !acceptor0);
val = false; val = false;
keyword res = *this; keyword_t res = *this;
res.acceptor0 = [&val] (void) { val = true; }; res.acceptor0 = [&val] (void) { val = true; };
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::acceptor (acceptor0_t _acceptor) keyword_t::acceptor (acceptor0_t _acceptor)
{ {
keyword res = *this; keyword_t res = *this;
res.acceptor0 = _acceptor; res.acceptor0 = _acceptor;
return res; return res;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
keyword keyword_t
keyword::ignore (void) const keyword_t::ignore (void) const
{ {
keyword res = *this; keyword_t res = *this;
res.acceptor0.reset (); res.acceptor0.reset ();
res.acceptor1.reset (); res.acceptor1.reset ();
return res; return res;

View File

@ -19,7 +19,7 @@
namespace cruft::cmdopt2 { namespace cruft::cmdopt2 {
struct argument { struct argument_t {
std::string name; std::string name;
std::optional<std::string> description; std::optional<std::string> description;
bool required_ = false; bool required_ = false;
@ -30,7 +30,7 @@ namespace cruft::cmdopt2 {
template <typename BaseT> template <typename BaseT>
struct ops : argument { struct ops_t : argument_t {
template <typename ValueT> template <typename ValueT>
BaseT bind (ValueT&&) = delete; BaseT bind (ValueT&&) = delete;
@ -90,39 +90,40 @@ namespace cruft::cmdopt2 {
}; };
struct positional : public ops<positional> { struct positional_t : public ops_t<positional_t> {
static positional create (char const *name); positional_t ignore (void) const;
static positional create (std::string_view name);
static positional create (std::string const &name);
static positional create (std::string &&name);
positional ignore (void) const;
int count = 1; int count = 1;
}; };
struct keyword : public ops<keyword> { positional_t positional (char const *name);
positional_t positional (std::string_view name);
positional_t positional (std::string const &name);
positional_t positional (std::string &&name);
struct keyword_t : public ops_t<keyword_t> {
using acceptor0_t = std::function<void(void)>; using acceptor0_t = std::function<void(void)>;
std::optional<acceptor0_t> acceptor0; std::optional<acceptor0_t> acceptor0;
static keyword create (char const *name); using ops_t::acceptor;
static keyword create (std::string_view name); keyword_t acceptor (acceptor0_t);
static keyword create (std::string const &name);
static keyword create (std::string &&name);
using ops::acceptor; keyword_t ignore (void) const;
keyword acceptor (acceptor0_t);
keyword ignore (void) const; keyword_t flag (void) const;
keyword_t flag (std::string_view long_) const;
keyword_t flag (char short_) const;
keyword flag (void) const; keyword_t count (int &) const;
keyword flag (std::string_view long_) const; keyword_t present (bool &) const;
keyword flag (char short_) const;
keyword count (int &) const;
keyword present (bool &) const;
std::optional<char> short_; std::optional<char> short_;
std::optional<std::string> long_; std::optional<std::string> long_;
}; };
keyword_t keyword (char const *name);
keyword_t keyword (std::string_view name);
keyword_t keyword (std::string const &name);
keyword_t keyword (std::string &&name);
} }

View File

@ -10,9 +10,9 @@
namespace cruft::cmdopt2 { namespace cruft::cmdopt2 {
struct argument; struct argument_t;
struct positional; struct positional_t;
struct keyword; struct keyword_t;
class parser; class parser;
} }

View File

@ -8,16 +8,16 @@ using cruft::cmdopt2::parser;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
cruft::cmdopt2::positional& cruft::cmdopt2::positional_t&
parser::add (positional const &arg)& parser::add (positional_t const &arg)&
{ {
return m_positional.emplace_back (std::move (arg)); return m_positional.emplace_back (std::move (arg));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
cruft::cmdopt2::keyword& cruft::cmdopt2::keyword_t&
parser::add (keyword const &arg)& parser::add (keyword_t const &arg)&
{ {
return m_keyword.emplace_back (std::move (arg)); return m_keyword.emplace_back (std::move (arg));
} }
@ -222,8 +222,8 @@ static void
usage ( usage (
int argc, int argc,
char const * const* argv, char const * const* argv,
std::vector<cruft::cmdopt2::positional> const &positional, std::vector<cruft::cmdopt2::positional_t> const &positional,
std::vector<cruft::cmdopt2::keyword> const &keyword, std::vector<cruft::cmdopt2::keyword_t> const &keyword,
OutputT &output OutputT &output
) { ) {
fmt::print (output, "Usage:"); fmt::print (output, "Usage:");

View File

@ -19,8 +19,8 @@ namespace cruft::cmdopt2 {
public: public:
int parse [[nodiscard]] (int argc, char const* const* argv); int parse [[nodiscard]] (int argc, char const* const* argv);
positional& add (positional const&) &; positional_t& add (positional_t const&) &;
keyword& add (keyword const&) &; keyword_t& add (keyword_t const&) &;
void usage (int argc, char const * const* argv, FILE*) const; void usage (int argc, char const * const* argv, FILE*) const;
void usage (int argc, char const * const* argv, std::ostream&) const; void usage (int argc, char const * const* argv, std::ostream&) const;
@ -30,7 +30,7 @@ namespace cruft::cmdopt2 {
int parse_short (int argc, char const* const* argv, int*) const; int parse_short (int argc, char const* const* argv, int*) const;
int parse_long (int argc, char const* const* argv, int*) const; int parse_long (int argc, char const* const* argv, int*) const;
std::vector<positional> m_positional; std::vector<positional_t> m_positional;
std::vector<keyword> m_keyword; std::vector<keyword_t> m_keyword;
}; };
} }

View File

@ -82,10 +82,10 @@ test_combinations (cruft::TAP::logger &tap)
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p;
p.add (keyword::create ("foo").flag ().flag ('f').bind (foo)); p.add (keyword ("foo").flag ().flag ('f').bind (foo));
p.add (keyword::create ("bar").bind (bar)); p.add (keyword ("bar").bind (bar));
p.add (positional::create ("qux").bind (qux)); p.add (positional ("qux").bind (qux));
p.add (keyword::create ("verbose").flag ().flag ('v').present (verbose)); p.add (keyword ("verbose").flag ().flag ('v').present (verbose));
for (auto const &t: TESTS) { for (auto const &t: TESTS) {
foo = decltype(foo) {}; foo = decltype(foo) {};
@ -167,8 +167,8 @@ static void test_presence (cruft::TAP::logger &tap)
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p;
p.add (keyword::create ("verbose").flag ().flag ('v').count (count)); p.add (keyword ("verbose").flag ().flag ('v').count (count));
p.add (keyword::create ("present").flag ().flag ('p').present (present)); p.add (keyword ("present").flag ().flag ('p').present (present));
for (auto const &t: TESTS) { for (auto const &t: TESTS) {
count = 0; count = 0;
@ -202,8 +202,8 @@ test_required (cruft::TAP::logger &tap)
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p;
p.add (keyword::create ("y").flag ('y').required (true ).ignore ()); p.add (keyword ("y").flag ('y').required (true ).ignore ());
p.add (keyword::create ("n").flag ('n').required (false).ignore ()); p.add (keyword ("n").flag ('n').required (false).ignore ());
for (auto const &t: TESTS) { for (auto const &t: TESTS) {
bool success; bool success;