aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTaylor C. Richberger <taywee@gmx.com>2017-12-12 13:55:52 -0700
committerGitHub <noreply@github.com>2017-12-12 13:55:52 -0700
commit4ac0840b657a8e4e74ea6742f8cab7e24acd771c (patch)
treec93c196a6d4eff377df9c64b5112f74cbea990db
parentMerge pull request #51 from pavel-belikov/fix-usage-wrapping (diff)
parentadd more GetChoicesString overrides (diff)
downloadargs.hxx-4ac0840b657a8e4e74ea6742f8cab7e24acd771c.tar.xz
Merge pull request #53 from pavel-belikov/map-flag-list-choices
Add more GetChoicesString overrides
-rw-r--r--args.hxx80
-rw-r--r--test.cxx14
2 files changed, 74 insertions, 20 deletions
diff --git a/args.hxx b/args.hxx
index a8818ab..549fd54 100644
--- a/args.hxx
+++ b/args.hxx
@@ -869,6 +869,13 @@ namespace args
defaultString = str;
}
+ /** Gets default value string that will be added to argument description.
+ */
+ std::string HelpDefault(const HelpParams &params) const
+ {
+ return GetDefaultString(params);
+ }
+
/** Sets choices string that will be added to argument description.
* Use empty string to disable it for this argument.
*/
@@ -878,6 +885,13 @@ namespace args
choicesString = str;
}
+ /** Gets choices string that will be added to argument description.
+ */
+ std::string HelpChoices(const HelpParams &params) const
+ {
+ return GetChoicesString(params);
+ }
+
virtual std::vector<std::tuple<std::string, std::string, unsigned>> GetDescription(const HelpParams &params, const unsigned indentLevel) const override
{
std::tuple<std::string, std::string, unsigned> description;
@@ -947,12 +961,39 @@ namespace args
s << value;
return s.str();
}
+
template <typename T>
typename std::enable_if<!IsConvertableToString<T>::value, std::string>::type
ToString(const T &)
{
return {};
}
+
+ template <typename T>
+ std::string MapKeysToString(const T &map)
+ {
+ std::string res;
+ using K = typename std::decay<decltype(std::begin(map)->first)>::type;
+ if (IsConvertableToString<K>::value)
+ {
+ std::vector<std::string> values;
+ for (const auto &p : map)
+ {
+ values.push_back(detail::ToString(p.first));
+ }
+
+ std::sort(values.begin(), values.end());
+ for (const auto &s : values)
+ {
+ if (!res.empty())
+ {
+ res += ", ";
+ }
+ res += s;
+ }
+ }
+ return res;
+ }
}
/** Base class for all flag options
@@ -3188,26 +3229,7 @@ namespace args
protected:
virtual std::string GetChoicesString(const HelpParams &) const override
{
- std::string res;
- if (detail::IsConvertableToString<K>::value)
- {
- std::vector<std::string> values;
- for (const auto &p : map)
- {
- values.push_back(detail::ToString(p.first));
- }
-
- std::sort(values.begin(), values.end());
- for (const auto &s : values)
- {
- if (!res.empty())
- {
- res += ", ";
- }
- res += s;
- }
- }
- return res;
+ return detail::MapKeysToString(map);
}
public:
@@ -3286,6 +3308,12 @@ namespace args
Container values;
Reader reader;
+ protected:
+ virtual std::string GetChoicesString(const HelpParams &) const override
+ {
+ return detail::MapKeysToString(map);
+ }
+
public:
typedef T value_type;
typedef typename Container::allocator_type allocator_type;
@@ -3557,6 +3585,12 @@ namespace args
T value;
Reader reader;
+ protected:
+ virtual std::string GetChoicesString(const HelpParams &) const override
+ {
+ return detail::MapKeysToString(map);
+ }
+
public:
MapPositional(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const T &defaultValue_ = T()): PositionalBase(name_, help_), map(map_), value(defaultValue_)
@@ -3626,6 +3660,12 @@ namespace args
Container values;
Reader reader;
+ protected:
+ virtual std::string GetChoicesString(const HelpParams &) const override
+ {
+ return detail::MapKeysToString(map);
+ }
+
public:
typedef T value_type;
typedef typename Container::allocator_type allocator_type;
diff --git a/test.cxx b/test.cxx
index 911779d..1ad728b 100644
--- a/test.cxx
+++ b/test.cxx
@@ -1227,6 +1227,20 @@ TEST_CASE("Default values work as expected", "[args]")
)");
}
+TEST_CASE("Choices description works as expected", "[args]")
+{
+ args::ArgumentParser p("parser");
+ args::MapFlag<int, int> map(p, "map", "map", {"map"}, {{1,1}, {2, 2}});
+ args::MapFlagList<char, int> maplist(p, "maplist", "maplist", {"maplist"}, {{'1',1}, {'2', 2}});
+ args::MapPositional<std::string, int, args::ValueReader, std::map> mappos(p, "mappos", "mappos", {{"1",1}, {"2", 2}});
+ args::MapPositionalList<char, int, std::vector, args::ValueReader, std::map> 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");
+}
+
#undef ARGS_HXX
#define ARGS_TESTNAMESPACE
#define ARGS_NOEXCEPT