Skip to content
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

Open
darealshinji opened this issue Apr 19, 2024 · 4 comments
Open

Option to stop on an empty value #122

darealshinji opened this issue Apr 19, 2024 · 4 comments

Comments

@darealshinji
Copy link
Contributor

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 to Get() the value and check if it's empty.
It would be useful if I could set an option flag for that.

@Taywee
Copy link
Owner

Taywee commented Apr 19, 2024

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.

@darealshinji
Copy link
Contributor Author

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);
        }
    }

@Taywee
Copy link
Owner

Taywee commented Apr 22, 2024

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.

@darealshinji
Copy link
Contributor Author

Maybe the custom reader should be run inside ArgumentParser::ParseArgsValues(). That way it would receive the argument name.

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants