cmdopt2/parser: add a description parameter

This commit is contained in:
Danny Robson 2022-03-21 10:50:39 +10:00
parent 787b91c6ef
commit bc5aaae818
3 changed files with 14 additions and 3 deletions

View File

@ -7,6 +7,12 @@
using cruft::cmdopt2::parser; using cruft::cmdopt2::parser;
///////////////////////////////////////////////////////////////////////////////
parser::parser (char const *_description)
: m_description (_description)
{ ; }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
cruft::cmdopt2::positional_t& cruft::cmdopt2::positional_t&
parser::add (positional_t const &arg)& parser::add (positional_t const &arg)&
@ -220,6 +226,7 @@ parser::parse (int const argc, const char *const *argv)
void void
parser::usage (int argc, char const * const* argv, FILE *fp) const parser::usage (int argc, char const * const* argv, FILE *fp) const
{ {
fmt::print (fp, FMT_STRING("{}\nUsage:"), m_description);
if (argc > 0) if (argc > 0)
fmt::print (fp, " {}", argv[0]); fmt::print (fp, " {}", argv[0]);

View File

@ -17,6 +17,8 @@
namespace cruft::cmdopt2 { namespace cruft::cmdopt2 {
class parser { class parser {
public: public:
explicit parser (char const *desc);
int parse [[nodiscard]] (int argc, char const* const* argv); int parse [[nodiscard]] (int argc, char const* const* argv);
positional_t& add (positional_t const&) &; positional_t& add (positional_t const&) &;
@ -31,5 +33,7 @@ namespace cruft::cmdopt2 {
std::vector<positional_t> m_positional; std::vector<positional_t> m_positional;
std::vector<keyword_t> m_keyword; std::vector<keyword_t> m_keyword;
char const *m_description;
}; };
} }

View File

@ -80,7 +80,7 @@ test_combinations (cruft::TAP::logger &tap)
bool verbose; bool verbose;
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p ("test suite");
p.add (keyword ("foo").flag ().flag ('f').bind (foo)); p.add (keyword ("foo").flag ().flag ('f').bind (foo));
p.add (keyword ("bar").bind (bar)); p.add (keyword ("bar").bind (bar));
@ -166,7 +166,7 @@ static void test_presence (cruft::TAP::logger &tap)
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p ("test suite");
p.add (keyword ("verbose").flag ().flag ('v').count (count)); p.add (keyword ("verbose").flag ().flag ('v').count (count));
p.add (keyword ("present").flag ().flag ('p').present (present)); p.add (keyword ("present").flag ().flag ('p').present (present));
@ -201,7 +201,7 @@ test_required (cruft::TAP::logger &tap)
using namespace cruft::cmdopt2; using namespace cruft::cmdopt2;
parser p; parser p ("test suite");
p.add (keyword ("y").flag ('y').required (true ).ignore ()); p.add (keyword ("y").flag ('y').required (true ).ignore ());
p.add (keyword ("n").flag ('n').required (false).ignore ()); p.add (keyword ("n").flag ('n').required (false).ignore ());