diff --git a/cmdopt.cpp b/cmdopt.cpp index 277808b8..b12b20ba 100644 --- a/cmdopt.cpp +++ b/cmdopt.cpp @@ -149,6 +149,15 @@ null::execute (const char *restrict) } +//----------------------------------------------------------------------------- +const std::string& +null::example (void) const +{ + static const std::string EXAMPLE; + return EXAMPLE; +} + + /////////////////////////////////////////////////////////////////////////////// present::present (std::string _name, std::string _description, bool &_data): base (std::move (_name), std::move (_description)), @@ -164,6 +173,15 @@ present::execute (void) } +//----------------------------------------------------------------------------- +const std::string& +present::example (void) const +{ + static const std::string EXAMPLE; + return EXAMPLE; +} + + //----------------------------------------------------------------------------- void present::finish (void) @@ -443,12 +461,28 @@ parser::print_help (const int argc, exit (0); // find the longest long form argument so we can set field alignment - auto largest = std::max_element (m_long.begin (), - m_long.end (), - [] (const auto &a, const auto &b) { - return std::get<0> (a).size () < std::get<0> (b).size (); + auto largestwidth = std::max_element ( + m_long.begin (), + m_long.end (), + [] (const auto &a, const auto &b) + { + return std::get (a).size () < std::get (b).size (); }); - int longwidth = std::get<0> (*largest).size (); + int longwidth = std::get (*largestwidth).size (); + + // find the longest example text + auto largestexample = std::max_element ( + m_options.cbegin (), + m_options.cend (), + [] (const auto &a, const auto &b) + { + const auto &example_a = std::get> (a)->example (); + const auto &example_b = std::get> (b)->example (); + + return example_a.size () > example_b.size (); + }); + + int longexample = std::get> (*largestexample)->example ().size (); // field width requires an alignment. we don't care about preserving // state as we're about to bail anyway @@ -459,8 +493,9 @@ parser::print_help (const int argc, for (auto &o: m_options) { std::cout << '\t' - << '-' << std::get<0> (o) << '\t' - << std::setw (longwidth) << std::get<1> (o) << '\t' + << '-' << std::get (o) << '\t' + << std::setw (longwidth) << std::get (o) << '\t' + << std::setw (longexample) << std::get> (o)->example () << '\t' << std::setw (0) << std::get<2> (o)->description () << '\n'; } diff --git a/cmdopt.hpp b/cmdopt.hpp index 9f5e587b..41313181 100644 --- a/cmdopt.hpp +++ b/cmdopt.hpp @@ -38,6 +38,8 @@ namespace util { namespace cmdopt { virtual void start (void); virtual void finish (void); + virtual const std::string& example (void) const = 0; + const std::string& name (void) const; const std::string& description (void) const; @@ -62,6 +64,8 @@ namespace util { namespace cmdopt { virtual void execute (void) override; virtual void execute (const char *restrict) override; + + virtual const std::string& example (void) const override; }; @@ -72,6 +76,8 @@ namespace util { namespace cmdopt { using base::execute; virtual void execute (void) override; + virtual const std::string& example (void) const override; + virtual void finish (void) override; private: @@ -87,6 +93,8 @@ namespace util { namespace cmdopt { using base::execute; void execute (const char *restrict) override; + const std::string& example (void) const override; + T data (void) const; T& data (void); T& data (T); diff --git a/cmdopt.ipp b/cmdopt.ipp index 0b2d37ee..52a5c9c0 100644 --- a/cmdopt.ipp +++ b/cmdopt.ipp @@ -21,6 +21,9 @@ #include +#include "./introspection.hpp" +#include "./iterator.hpp" + namespace util { namespace cmdopt { /////////////////////////////////////////////////////////////////////////// template @@ -53,6 +56,45 @@ namespace util { namespace cmdopt { } + //------------------------------------------------------------------------- + namespace detail { + template + std::enable_if_t::value, const std::string&> + value_example (void) + { + static const std::string EXAMPLE = + std::string {"<"} + + std::string {to_string ()} + + std::string {">"}; + + return EXAMPLE; + } + + template + std::enable_if_t::value, const std::string&> + value_example (void) + { + static const std::string EXAMPLE = [] (void) { + std::ostringstream os; + std::copy (std::cbegin (enum_values::values), + std::cend (enum_values::values), + infix_iterator (os, "|")); + return os.str (); + } (); + return EXAMPLE; + } + } + + + //------------------------------------------------------------------------- + template + const std::string& + option::value::example (void) const + { + return detail::value_example (); + } + + //----------------------------------------------------------------------------- template T