-
Notifications
You must be signed in to change notification settings - Fork 353
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
Support repeating multi-argument options #128
Comments
This was added in 1.5. See #92. You can now do: ...->set_custom_option("KEY VALUE", -2); If you really want to go all out and capture the structure too, something like this should work: #include<map>
std::map<std::string, std::string> options;
app.add_option("--entry",
[&options](CLI::results_t vals){ // results_t is just a vector of strings
for(size_t i=0; i<vals.size()/2; i++) // will always be a multiple of 2
options[vals.at(i*2)] = vals.at(i*2+1);
return true;
},
"key & value")
->set_custom_option("KEY VALUE", -2); Or you could make a vector of pairs or something else.
|
Thanks for the quick response! Sorry that I missed I tried the example you provided, but I'm having a couple issues. First, #include <iostream>
#include <map>
#include <CLI/CLI.hpp>
int main(int argc, char *argv[]) {
CLI::App app{"Repeating options that take multiple arguments"};
std::map<std::string, std::string> entry_map;
auto entry_option{
app.add_option("--entry",
[&entry_map](CLI::results_t vals) { // results_t is just a vector of strings
for (size_t i{0}; i < vals.size() / 2; ++i) // will always be a multiple of 2
entry_map[vals.at(i * 2)] = vals.at(i * 2 + 1);
return true;
},
"key & value")};
entry_option->set_custom_option("KEY VALUE", 2);
entry_option->expected(-1);
CLI11_PARSE(app, argc, argv);
for (const auto &entry : entry_map)
std::cout << "Key: " << entry.first << "; Value: " << entry.second << std::endl;
return 0;
} Compiling and running this yields the following:
So it seems like |
A) Can you make a PR? It should return |
How about renaming? The |
Sounds good to me 👍 I'm not sure how much time I'll have to work on it this week, but I'll start by setting up a development environment. Regarding N<-1, the README currently states:
I interpreted this as "the option takes a minimum of Is that an incorrect interpretation, or are you suggesting changing that behavior to require multiples of Thank you for your input on this! |
That is the correct interpretation. The type size is separate from the expected value. So, for example: ...->set_custom_option("A B", /* type size*/ -2)->expected(-2) Would mean you have to enter A B twice or more.
This is already how it works (except for the broken By the way, setting a negative type size automatically sets expected to -1. |
This is what we have now: ->set_custom_option("KEY VALUE", -2); // Using -> broken These are options: ->type_info("KEY VALUE", -2); Or ->type_name("KEY VALUE")->type_size(-2); Since you probably always want to set the type name if you set the type size, that's why I was thinking of using one method instead of two. Maybe the other way is clearer, though. |
Ok, I think I understand how it is supposed to work now. Thank you for the explanation! I think the single method |
Yes, for a release, at least for Lines 317 to 319 in cf6a99f
|
What's the ETA on this? Looking at a 1.6 release soon. |
That ought to fix it, let me know if it doesn't work! |
@henryiii Thank you! I'm sorry for not responding earlier —we have had some changes at work and I haven't had as much time to work on other projects (such as this). I will let you know how it goes when I try it! |
First of all, thank you for creating CLI11! It's much better than anything else out there. I've used Boost.Program_Options, which was ok, and QCommandLineParser, which was awful. CLI11 is a breath of fresh air and has saved me a lot of time, so, thank you!
I am looking to implement a command-line interface that allows a repeating option that takes 2 arguments. Here is an example Python program using argparse that implements it:
Example runs:
It's almost possible with a combination of
…->expected(2)->join()
, but what I'm really looking for is a result that shows accurate error messages and preserves the nested structure (a list of lists).If there is a path forward, I may also be able to help implement. Thank you!
The text was updated successfully, but these errors were encountered: