From debaca70bc9ac882a210c4efd3761edf3935f10d Mon Sep 17 00:00:00 2001 From: Pavel Belikov Date: Sat, 23 Dec 2017 11:27:03 +0300 Subject: change GetChoicesString() signature --- args.hxx | 137 +++++++++++++++++++++++++++++++++------------------------------ test.cxx | 10 ++--- 2 files changed, 77 insertions(+), 70 deletions(-) diff --git a/args.hxx b/args.hxx index 37e86b4..ba0a010 100644 --- a/args.hxx +++ b/args.hxx @@ -705,6 +705,40 @@ namespace args std::string defaultString = "\nDefault: "; }; + /** A number of arguments which can be consumed by an option. + * + * Represents a closed interval [min, max]. + */ + struct Nargs + { + const size_t min; + const size_t max; + + Nargs(size_t min_, size_t max_) : min(min_), max(max_) + { +#ifndef ARGS_NOEXCEPT + if (max < min) + { + throw UsageError("Nargs: max > min"); + } +#endif + } + + Nargs(size_t num_) : min(num_), max(num_) + { + } + + friend bool operator == (const Nargs &lhs, const Nargs &rhs) + { + return lhs.min == rhs.min && lhs.max == rhs.max; + } + + friend bool operator != (const Nargs &lhs, const Nargs &rhs) + { + return !(lhs == rhs); + } + }; + /** Base class for all match types */ class Base @@ -841,12 +875,12 @@ namespace args bool kickout = false; std::string defaultString; bool defaultStringManual = false; - std::string choicesString; + std::vector choicesStrings; bool choicesStringManual = false; virtual std::string GetDefaultString(const HelpParams&) const { return {}; } - virtual std::string GetChoicesString(const HelpParams&) const { return {}; } + virtual std::vector GetChoicesStrings(const HelpParams&) const { return {}; } virtual std::string GetNameString(const HelpParams&) const { return Name(); } @@ -884,20 +918,20 @@ namespace args return GetDefaultString(params); } - /** Sets choices string that will be added to argument description. - * Use empty string to disable it for this argument. + /** Sets choices strings that will be added to argument description. + * Use empty vector to disable it for this argument. */ - void HelpChoices(const std::string &str) + void HelpChoices(const std::vector &array) { choicesStringManual = true; - choicesString = str; + choicesStrings = array; } - /** Gets choices string that will be added to argument description. + /** Gets choices strings that will be added to argument description. */ - std::string HelpChoices(const HelpParams ¶ms) const + std::vector HelpChoices(const HelpParams ¶ms) const { - return GetChoicesString(params); + return GetChoicesStrings(params); } virtual std::vector> GetDescription(const HelpParams ¶ms, const unsigned indentLevel) const override @@ -907,7 +941,23 @@ namespace args std::get<1>(description) = help; std::get<2>(description) = indentLevel; - AddDescriptionPostfix(std::get<1>(description), choicesStringManual, choicesString, params.addChoices, GetChoicesString(params), params.choiceString); + auto join = [](const std::vector &array) -> std::string + { + std::string res; + + for (auto &str : array) + { + if (!res.empty()) + { + res += ", "; + } + res += str; + } + + return res; + }; + + AddDescriptionPostfix(std::get<1>(description), choicesStringManual, join(choicesStrings), params.addChoices, join(GetChoicesStrings(params)), params.choiceString); AddDescriptionPostfix(std::get<1>(description), defaultStringManual, defaultString, params.addDefault, GetDefaultString(params), params.defaultString); return { std::move(description) }; @@ -919,40 +969,6 @@ namespace args } }; - /** A number of arguments which can be consumed by an option. - * - * Represents a closed interval [min, max]. - */ - struct Nargs - { - const size_t min; - const size_t max; - - Nargs(size_t min_, size_t max_) : min(min_), max(max_) - { -#ifndef ARGS_NOEXCEPT - if (max < min) - { - throw UsageError("Nargs: max > min"); - } -#endif - } - - Nargs(size_t num_) : min(num_), max(num_) - { - } - - friend bool operator == (const Nargs &lhs, const Nargs &rhs) - { - return lhs.min == rhs.min && lhs.max == rhs.max; - } - - friend bool operator != (const Nargs &lhs, const Nargs &rhs) - { - return !(lhs == rhs); - } - }; - namespace detail { template @@ -978,27 +994,18 @@ namespace args } template - std::string MapKeysToString(const T &map) + std::vector MapKeysToStrings(const T &map) { - std::string res; + std::vector res; using K = typename std::decayfirst)>::type; if (IsConvertableToString::value) { - std::vector values; for (const auto &p : map) { - values.push_back(detail::ToString(p.first)); + res.push_back(detail::ToString(p.first)); } - std::sort(values.begin(), values.end()); - for (const auto &s : values) - { - if (!res.empty()) - { - res += ", "; - } - res += s; - } + std::sort(res.begin(), res.end()); } return res; } @@ -3241,9 +3248,9 @@ namespace args Reader reader; protected: - virtual std::string GetChoicesString(const HelpParams &) const override + virtual std::vector GetChoicesStrings(const HelpParams &) const override { - return detail::MapKeysToString(map); + return detail::MapKeysToStrings(map); } public: @@ -3323,9 +3330,9 @@ namespace args Reader reader; protected: - virtual std::string GetChoicesString(const HelpParams &) const override + virtual std::vector GetChoicesStrings(const HelpParams &) const override { - return detail::MapKeysToString(map); + return detail::MapKeysToStrings(map); } public: @@ -3600,9 +3607,9 @@ namespace args Reader reader; protected: - virtual std::string GetChoicesString(const HelpParams &) const override + virtual std::vector GetChoicesStrings(const HelpParams &) const override { - return detail::MapKeysToString(map); + return detail::MapKeysToStrings(map); } public: @@ -3675,9 +3682,9 @@ namespace args Reader reader; protected: - virtual std::string GetChoicesString(const HelpParams &) const override + virtual std::vector GetChoicesStrings(const HelpParams &) const override { - return detail::MapKeysToString(map); + return detail::MapKeysToStrings(map); } public: diff --git a/test.cxx b/test.cxx index 01decbe..5be6ff7 100644 --- a/test.cxx +++ b/test.cxx @@ -1203,7 +1203,7 @@ TEST_CASE("Default values work as expected", "[args]") )"); f.HelpDefault("123"); - b.HelpChoices("1, 2, 3"); + b.HelpChoices({"1", "2", "3"}); REQUIRE(p.Help() == R"( prog {OPTIONS} parser @@ -1239,10 +1239,10 @@ TEST_CASE("Choices description works as expected", "[args]") args::MapPositional mappos(p, "mappos", "mappos", {{"1",1}, {"2", 2}}); args::MapPositionalList mapposlist(p, "mapposlist", "mapposlist", {{'1',1}, {'2', 2}}); - REQUIRE(map.HelpChoices(p.helpParams) == "1, 2"); - REQUIRE(maplist.HelpChoices(p.helpParams) == "1, 2"); - REQUIRE(mappos.HelpChoices(p.helpParams) == "1, 2"); - REQUIRE(mapposlist.HelpChoices(p.helpParams) == "1, 2"); + REQUIRE(map.HelpChoices(p.helpParams) == std::vector{"1", "2"}); + REQUIRE(maplist.HelpChoices(p.helpParams) == std::vector{"1", "2"}); + REQUIRE(mappos.HelpChoices(p.helpParams) == std::vector{"1", "2"}); + REQUIRE(mapposlist.HelpChoices(p.helpParams) == std::vector{"1", "2"}); } #undef ARGS_HXX -- cgit v1.2.1