-
Notifications
You must be signed in to change notification settings - Fork 242
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
add_argument should return the same name exist argument #224
Comments
Please correct me if I misunderstand. Are you reporting that the following code will compile, but running the compiled binary will almost always give a confusing error or other unexpected result? #include <argparse/argparse.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
argparse::ArgumentParser program("program_name");
program.add_argument("file");
program.add_argument("file");
program.parse_args(argc, argv);
std::cout << program.get("file") << std::endl;
return 0;
} $ program
terminate called after throwing an instance of 'std::runtime_error'
what(): file: 1 argument(s) expected. 0 provided.
Aborted
$ program first_file.txt
terminate called after throwing an instance of 'std::runtime_error'
what(): file: 1 argument(s) expected. 0 provided.
Aborted
$ program first_file.txt second_file.txt
second_file.txt |
A little bit different case
`
the risk is "--vel" may not exist in function 2,so a modified code like
is a more natural way. |
I think I understand. You want program.add_argument("--file");
program.add_argument("--file").default_value("badger-sett-001.txt"); to not create a new, second |
That's right. It's more natural. |
I've thought more about this and I'm not a fan of two meanings for a single function. Your proposal would have PR #245 does not directly address this, but could be used to make a stand-alone function to add or return an existing argparse::Argument &add_or_get(argparse::ArgumentParser parser, const std::string arg) {
argparse::Argument new_arg;
try {
new_arg = parser.at(arg);
} catch () {
new_arg = parser.add_argument(arg);
}
return &new_arg;
}
// use example
add_or_get(program, "--file");
add_or_get(program, "--file").default_value("badger-sett-001.txt"); |
I think it's not about two meanings. What's the point about adding dunplicate argument? if all the arguments are added in one function that's fine.But if arguments could be added by others , I guess that code add_argument means He/She/AnyGender will need that argument and just too lazy to add a test code. |
the add_or_get looks won't work here. |
add_argument now with
auto argument = m_optional_arguments.emplace(std::cend(m_optional_arguments), m_prefix_chars, array_of_sv{f_args...});
and [] will return the exist argumint in m_argument_map and throw exception when missing.
In most cases it should no allow the same name argument right? Why not return the exist argument in m_argument_map instead?
The text was updated successfully, but these errors were encountered: