aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--args.hxx102
-rw-r--r--test.cxx22
2 files changed, 95 insertions, 29 deletions
diff --git a/args.hxx b/args.hxx
index b0f1182..67f53eb 100644
--- a/args.hxx
+++ b/args.hxx
@@ -514,17 +514,25 @@ namespace args
*/
Required = 0x02,
- /** Flag is excluded from help output.
+ /** Flag is excluded from usage line.
*/
- Hidden = 0x04,
+ HiddenFromUsage = 0x04,
+
+ /** Flag is excluded from options help.
+ */
+ HiddenFromDescription = 0x08,
/** Flag is global and can be used in any subcommand.
*/
- Global = 0x08,
+ Global = 0x10,
/** Flag stops a parser.
*/
- KickOut = 0x10,
+ KickOut = 0x20,
+
+ /** Flag is excluded from options help and usage line
+ */
+ Hidden = HiddenFromUsage | HiddenFromDescription,
};
inline Options operator | (Options lhs, Options rhs)
@@ -869,6 +877,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 +893,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 +969,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
@@ -1350,7 +1399,7 @@ namespace args
for (Base *child: Children())
{
- if ((child->GetOptions() & Options::Hidden) != Options::None)
+ if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
{
continue;
}
@@ -1371,7 +1420,7 @@ namespace args
std::vector <std::string> names;
for (Base *child: Children())
{
- if ((child->GetOptions() & Options::Hidden) != Options::None)
+ if ((child->GetOptions() & Options::HiddenFromUsage) != Options::None)
{
continue;
}
@@ -1900,7 +1949,7 @@ namespace args
for (Base *child: Children())
{
- if ((child->GetOptions() & Options::Hidden) != Options::None)
+ if ((child->GetOptions() & Options::HiddenFromDescription) != Options::None)
{
continue;
}
@@ -3193,26 +3242,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:
@@ -3291,6 +3321,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;
@@ -3562,6 +3598,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_)
@@ -3631,6 +3673,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..01decbe 100644
--- a/test.cxx
+++ b/test.cxx
@@ -593,8 +593,8 @@ TEST_CASE("Required flags work as expected", "[args]")
TEST_CASE("Hidden options are excluded from help", "[args]")
{
args::ArgumentParser parser1("");
- args::ValueFlag<int> foo(parser1, "foo", "foo", {'f', "foo"}, args::Options::Hidden);
- args::ValueFlag<int> bar(parser1, "bar", "bar", {'b'});
+ args::ValueFlag<int> foo(parser1, "foo", "foo", {'f', "foo"}, args::Options::HiddenFromDescription);
+ args::ValueFlag<int> bar(parser1, "bar", "bar", {'b'}, args::Options::HiddenFromUsage);
args::Group group(parser1, "group");
args::ValueFlag<int> foo1(group, "foo", "foo", {'f', "foo"}, args::Options::Hidden);
args::ValueFlag<int> bar2(group, "bar", "bar", {'b'});
@@ -604,6 +604,10 @@ TEST_CASE("Hidden options are excluded from help", "[args]")
REQUIRE(std::get<0>(desc[0]) == "-b[bar]");
REQUIRE(std::get<0>(desc[1]) == "group");
REQUIRE(std::get<0>(desc[2]) == "-b[bar]");
+
+ parser1.helpParams.proglineShowFlags = true;
+ parser1.helpParams.proglinePreferShortFlags = true;
+ REQUIRE((parser1.GetProgramLine(parser1.helpParams) == std::vector<std::string>{"[-f <foo>]", "[-b <bar>]"}));
}
TEST_CASE("Implicit values work as expected", "[args]")
@@ -1227,6 +1231,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