-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to stop on an empty value #122
Comments
That's beyond the scope of an option flag. That would be more in line with a custom reader, where you could make the decision to skip or do whatever else you like on an empty string. The argument will still be considered "matched", and will return true on a boolean test, but that isn't possible to bypass. |
It works, but I can't print the argument name in an error message. Right now this is how I do it with my helper function: template<class T=args::ValueFlag<std::string>>
void check_empty(const std::string &val, T &flag)
{
if (val.empty()) {
std::string arg = flag.GetMatcher().GetLongOrAny().str(/* "-", "--" */);
std::cerr << "error: Flag '" << arg << "' requires a non-empty argument" << std::endl;
std::exit(1);
}
} |
The custom reader does give you access to the argument name, but yes, not the flag. There's not a way to get the flag used in the current version of the library in the reader. An option is a possibility, and could be checked for reasonably pre-reader execution. I'm not planning on adding this feature, but I would accept a good pull request that added it. |
Maybe the custom reader should be run inside Here's how I patched that part to do what I needed it: --- a/src/args.hxx
+++ b/src/args.hxx
@@ -2360,6 +2360,10 @@
{
if (!canDiscardJoined || nargs.max != 0)
{
+ if (joinedArg.empty()) {
+ return "Flag '" + arg + "' received an empty argument";
+ }
+
values.push_back(joinedArg);
}
} else if (!allowSeparate)
@@ -2383,6 +2387,10 @@
return "";
}
+ if ((*valueIt).empty()) {
+ return "Flag '" + arg + "' received an empty argument";
+ }
+
values.push_back(*valueIt);
++it;
++valueIt; |
Right now when I specify something like
--name=''
the value for name will be read as an empty string. But for my use case I never want empty strings. Right now I use a small helper function toGet()
the value and check if it's empty.It would be useful if I could set an option flag for that.
The text was updated successfully, but these errors were encountered: