Argparse: implement value_name/alias/hide accessors
This commit is contained in:
1 parent
17a23bf88a
commit
5c5fe96f11
1 file changed
+50
-3
@@ -165,6 +165,46 @@ Parser& Parser::conflicts(std::string_view first, std::string_view second)
|
||||
return *this;
|
||||
}
|
||||
|
||||
Parser& Parser::value_name(std::string_view lng, std::string_view name)
|
||||
{
|
||||
for (auto& option : options_) {
|
||||
if (option.lng == lng) {
|
||||
option.value_name = std::string{name};
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Parser& Parser::alias(std::string_view lng, std::string_view alias)
|
||||
{
|
||||
for (auto& flag : flags_) {
|
||||
if (flag.lng == lng) {
|
||||
flag.alias = std::string{alias};
|
||||
}
|
||||
}
|
||||
for (auto& option : options_) {
|
||||
if (option.lng == lng) {
|
||||
option.alias = std::string{alias};
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Parser& Parser::hide(std::string_view lng)
|
||||
{
|
||||
for (auto& flag : flags_) {
|
||||
if (flag.lng == lng) {
|
||||
flag.hidden = true;
|
||||
}
|
||||
}
|
||||
for (auto& option : options_) {
|
||||
if (option.lng == lng) {
|
||||
option.hidden = true;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Parser& Parser::positional(std::string_view name,
|
||||
std::string_view desc,
|
||||
std::string& out)
|
||||
@@ -243,7 +283,8 @@ ParseResult Parser::parse_detailed(int argc, const char* const* argv) const
|
||||
bool& matched,
|
||||
std::string& err) -> bool {
|
||||
for (const auto& f : p->flags_) {
|
||||
if (arg == f.lng || (!f.sht.empty() && arg == f.sht)) {
|
||||
if (arg == f.lng || (!f.sht.empty() && arg == f.sht) ||
|
||||
(!f.alias.empty() && arg == f.alias)) {
|
||||
if (f.count_out) {
|
||||
++*f.count_out;
|
||||
} else {
|
||||
@@ -294,7 +335,8 @@ ParseResult Parser::parse_detailed(int argc, const char* const* argv) const
|
||||
value = arg.substr(opt.lng.size() + 1);
|
||||
found = true;
|
||||
} else if (arg == opt.lng ||
|
||||
(!opt.sht.empty() && arg == opt.sht)) {
|
||||
(!opt.sht.empty() && arg == opt.sht) ||
|
||||
(!opt.alias.empty() && arg == opt.alias)) {
|
||||
if (i + 1 >= argc) {
|
||||
err = "option '" + std::string{arg} + "' requires a value";
|
||||
return false;
|
||||
@@ -629,17 +671,22 @@ std::string Parser::help() const
|
||||
|
||||
std::vector<Row> flags;
|
||||
for (const auto& f : flags_) {
|
||||
if (f.hidden) continue;
|
||||
std::string left = f.lng;
|
||||
if (!f.sht.empty()) left += ", " + f.sht;
|
||||
if (!f.alias.empty()) left += ", " + f.alias;
|
||||
flags.emplace_back(std::move(left), f.desc);
|
||||
}
|
||||
section("Flags", std::move(flags));
|
||||
|
||||
std::vector<Row> options;
|
||||
for (const auto& o : options_) {
|
||||
if (o.hidden) continue;
|
||||
std::string left = o.lng;
|
||||
if (!o.sht.empty()) left += ", " + o.sht;
|
||||
left += " <value>";
|
||||
if (!o.alias.empty()) left += ", " + o.alias;
|
||||
left += " <" + (o.value_name.empty() ? std::string{"value"}
|
||||
: o.value_name) + ">";
|
||||
std::string desc = o.desc;
|
||||
if (o.required) desc += " [required]";
|
||||
if (!o.def.empty()) desc += " [default: " + o.def + "]";
|
||||
|
||||
Reference in new issue
Block a user