cmdopt2/args: extract argument::create as a free function
This commit is contained in:
parent
a54ed01dcb
commit
179f016fa1
@ -1,50 +1,50 @@
|
||||
#include "./args.hpp"
|
||||
|
||||
using cruft::cmdopt2::positional;
|
||||
using cruft::cmdopt2::keyword;
|
||||
using cruft::cmdopt2::positional_t;
|
||||
using cruft::cmdopt2::keyword_t;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
positional
|
||||
positional::create (char const *name)
|
||||
positional_t
|
||||
cruft::cmdopt2::positional (char const *name)
|
||||
{
|
||||
return create (std::string (name));
|
||||
return positional (std::string (name));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
positional
|
||||
positional::create (std::string &&name)
|
||||
positional_t
|
||||
cruft::cmdopt2::positional (std::string &&name)
|
||||
{
|
||||
positional res {};
|
||||
positional_t res {};
|
||||
res.name = name;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
positional
|
||||
positional::ignore (void) const
|
||||
positional_t
|
||||
positional_t::ignore (void) const
|
||||
{
|
||||
positional res = *this;
|
||||
positional_t res = *this;
|
||||
res.acceptor1.reset ();
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
keyword
|
||||
keyword::create (char const *name)
|
||||
keyword_t
|
||||
cruft::cmdopt2::keyword (char const *name)
|
||||
{
|
||||
return create (std::string (name));
|
||||
return keyword (std::string (name));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::create (std::string &&name)
|
||||
keyword_t
|
||||
cruft::cmdopt2::keyword (std::string &&name)
|
||||
{
|
||||
keyword res {};
|
||||
keyword_t res {};
|
||||
res.name = name;
|
||||
res.long_ = name;
|
||||
return res;
|
||||
@ -52,10 +52,10 @@ keyword::create (std::string &&name)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::flag (void) const
|
||||
keyword_t
|
||||
keyword_t::flag (void) const
|
||||
{
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.long_.reset ();
|
||||
res.short_.reset ();
|
||||
return res;
|
||||
@ -63,66 +63,66 @@ keyword::flag (void) const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::flag (char val) const
|
||||
keyword_t
|
||||
keyword_t::flag (char val) const
|
||||
{
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.short_ = val;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::flag (std::string_view val) const
|
||||
keyword_t
|
||||
keyword_t::flag (std::string_view val) const
|
||||
{
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.long_ = val;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::count (int &val) const
|
||||
keyword_t
|
||||
keyword_t::count (int &val) const
|
||||
{
|
||||
CHECK (!acceptor1 and !acceptor0);
|
||||
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.acceptor0 = [&val] (void) { ++val; };
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::present (bool &val) const
|
||||
keyword_t
|
||||
keyword_t::present (bool &val) const
|
||||
{
|
||||
CHECK (!acceptor1 and !acceptor0);
|
||||
|
||||
val = false;
|
||||
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.acceptor0 = [&val] (void) { val = true; };
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::acceptor (acceptor0_t _acceptor)
|
||||
keyword_t
|
||||
keyword_t::acceptor (acceptor0_t _acceptor)
|
||||
{
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.acceptor0 = _acceptor;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
keyword
|
||||
keyword::ignore (void) const
|
||||
keyword_t
|
||||
keyword_t::ignore (void) const
|
||||
{
|
||||
keyword res = *this;
|
||||
keyword_t res = *this;
|
||||
res.acceptor0.reset ();
|
||||
res.acceptor1.reset ();
|
||||
return res;
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
|
||||
namespace cruft::cmdopt2 {
|
||||
struct argument {
|
||||
struct argument_t {
|
||||
std::string name;
|
||||
std::optional<std::string> description;
|
||||
bool required_ = false;
|
||||
@ -30,7 +30,7 @@ namespace cruft::cmdopt2 {
|
||||
|
||||
|
||||
template <typename BaseT>
|
||||
struct ops : argument {
|
||||
struct ops_t : argument_t {
|
||||
template <typename ValueT>
|
||||
BaseT bind (ValueT&&) = delete;
|
||||
|
||||
@ -90,39 +90,40 @@ namespace cruft::cmdopt2 {
|
||||
};
|
||||
|
||||
|
||||
struct positional : public ops<positional> {
|
||||
static positional create (char const *name);
|
||||
static positional create (std::string_view name);
|
||||
static positional create (std::string const &name);
|
||||
static positional create (std::string &&name);
|
||||
|
||||
positional ignore (void) const;
|
||||
struct positional_t : public ops_t<positional_t> {
|
||||
positional_t ignore (void) const;
|
||||
|
||||
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)>;
|
||||
std::optional<acceptor0_t> acceptor0;
|
||||
|
||||
static keyword create (char const *name);
|
||||
static keyword create (std::string_view name);
|
||||
static keyword create (std::string const &name);
|
||||
static keyword create (std::string &&name);
|
||||
using ops_t::acceptor;
|
||||
keyword_t acceptor (acceptor0_t);
|
||||
|
||||
using ops::acceptor;
|
||||
keyword acceptor (acceptor0_t);
|
||||
keyword_t ignore (void) const;
|
||||
|
||||
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 flag (std::string_view long_) const;
|
||||
keyword flag (char short_) const;
|
||||
|
||||
keyword count (int &) const;
|
||||
keyword present (bool &) const;
|
||||
keyword_t count (int &) const;
|
||||
keyword_t present (bool &) const;
|
||||
|
||||
std::optional<char> short_;
|
||||
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);
|
||||
}
|
||||
|
@ -10,9 +10,9 @@
|
||||
|
||||
|
||||
namespace cruft::cmdopt2 {
|
||||
struct argument;
|
||||
struct positional;
|
||||
struct keyword;
|
||||
struct argument_t;
|
||||
struct positional_t;
|
||||
struct keyword_t;
|
||||
|
||||
class parser;
|
||||
}
|
||||
|
@ -8,16 +8,16 @@ using cruft::cmdopt2::parser;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
cruft::cmdopt2::positional&
|
||||
parser::add (positional const &arg)&
|
||||
cruft::cmdopt2::positional_t&
|
||||
parser::add (positional_t const &arg)&
|
||||
{
|
||||
return m_positional.emplace_back (std::move (arg));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
cruft::cmdopt2::keyword&
|
||||
parser::add (keyword const &arg)&
|
||||
cruft::cmdopt2::keyword_t&
|
||||
parser::add (keyword_t const &arg)&
|
||||
{
|
||||
return m_keyword.emplace_back (std::move (arg));
|
||||
}
|
||||
@ -222,8 +222,8 @@ static void
|
||||
usage (
|
||||
int argc,
|
||||
char const * const* argv,
|
||||
std::vector<cruft::cmdopt2::positional> const &positional,
|
||||
std::vector<cruft::cmdopt2::keyword> const &keyword,
|
||||
std::vector<cruft::cmdopt2::positional_t> const &positional,
|
||||
std::vector<cruft::cmdopt2::keyword_t> const &keyword,
|
||||
OutputT &output
|
||||
) {
|
||||
fmt::print (output, "Usage:");
|
||||
|
@ -19,8 +19,8 @@ namespace cruft::cmdopt2 {
|
||||
public:
|
||||
int parse [[nodiscard]] (int argc, char const* const* argv);
|
||||
|
||||
positional& add (positional const&) &;
|
||||
keyword& add (keyword const&) &;
|
||||
positional_t& add (positional_t 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, std::ostream&) const;
|
||||
@ -30,7 +30,7 @@ namespace cruft::cmdopt2 {
|
||||
int parse_short (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<keyword> m_keyword;
|
||||
std::vector<positional_t> m_positional;
|
||||
std::vector<keyword_t> m_keyword;
|
||||
};
|
||||
}
|
||||
|
@ -82,10 +82,10 @@ test_combinations (cruft::TAP::logger &tap)
|
||||
using namespace cruft::cmdopt2;
|
||||
parser p;
|
||||
|
||||
p.add (keyword::create ("foo").flag ().flag ('f').bind (foo));
|
||||
p.add (keyword::create ("bar").bind (bar));
|
||||
p.add (positional::create ("qux").bind (qux));
|
||||
p.add (keyword::create ("verbose").flag ().flag ('v').present (verbose));
|
||||
p.add (keyword ("foo").flag ().flag ('f').bind (foo));
|
||||
p.add (keyword ("bar").bind (bar));
|
||||
p.add (positional ("qux").bind (qux));
|
||||
p.add (keyword ("verbose").flag ().flag ('v').present (verbose));
|
||||
|
||||
for (auto const &t: TESTS) {
|
||||
foo = decltype(foo) {};
|
||||
@ -167,8 +167,8 @@ static void test_presence (cruft::TAP::logger &tap)
|
||||
using namespace cruft::cmdopt2;
|
||||
|
||||
parser p;
|
||||
p.add (keyword::create ("verbose").flag ().flag ('v').count (count));
|
||||
p.add (keyword::create ("present").flag ().flag ('p').present (present));
|
||||
p.add (keyword ("verbose").flag ().flag ('v').count (count));
|
||||
p.add (keyword ("present").flag ().flag ('p').present (present));
|
||||
|
||||
for (auto const &t: TESTS) {
|
||||
count = 0;
|
||||
@ -202,8 +202,8 @@ test_required (cruft::TAP::logger &tap)
|
||||
using namespace cruft::cmdopt2;
|
||||
|
||||
parser p;
|
||||
p.add (keyword::create ("y").flag ('y').required (true ).ignore ());
|
||||
p.add (keyword::create ("n").flag ('n').required (false).ignore ());
|
||||
p.add (keyword ("y").flag ('y').required (true ).ignore ());
|
||||
p.add (keyword ("n").flag ('n').required (false).ignore ());
|
||||
|
||||
for (auto const &t: TESTS) {
|
||||
bool success;
|
||||
|
Loading…
Reference in New Issue
Block a user