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

Implicit values break value assignment without equals sign #441

Open
blackworm27 opened this issue Nov 6, 2024 · 2 comments
Open

Implicit values break value assignment without equals sign #441

blackworm27 opened this issue Nov 6, 2024 · 2 comments

Comments

@blackworm27
Copy link

blackworm27 commented Nov 6, 2024

I am trying to allow the following kind of command line uses:

  1. <option not specified> yields default value (e.g., {""})
  2. --option with no argument yields implicit value (e.g., {"all"})
  3. --option a yields {"a"}
  4. --option a --option b yields {"a", "b"}

Instead, #3 yields {"all"} and #4 yields {"all", "all"}. It seems using implicit_value forces the user to use the --option=a,b syntax. Is this intended?

Here is my code fragment. I am using v3.2.1.

    _currentOptions->add_options()(
        "option",
        "description",
        cxxopts::value<std::vector<std::string>>()->default_value("")->implicit_value("all"));

Thank you for any guidance.

@jarro2783
Copy link
Owner

This is a problem that often comes up, and I don't know if there are any better ways to solve this. The problem with implicit options is that the parser can't tell if the next option is supposed to be attached to the implicit option or is a standalone argument (positional argument). So it basically forces you to use =. In your example 4, it's the same as writing

--option --option a b

which gives you (all, all) for --option, and two positional arguments a and b.

@blackworm27
Copy link
Author

Thanks for your reply. I don't tend to use positional arguments. Perhaps a solution would be a mode where any positional arguments must follow a specific switch (typically --). In that mode, any arguments not beginning with - are parsed as appending to the vector associated to the previous switch. The preceding switch would have to be a vector or there would be a parse error.

Anyway, I understand this is a minor limitation and a new mode may not be feasible or desired. Thanks for your response and for sharing this useful tool!

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