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

Throw exception in parse when there is no argument value for a positional option without default value #376

Open
mrbeardad opened this issue Oct 31, 2022 · 2 comments

Comments

@mrbeardad
Copy link

Current Behavior

  cxxopts::Options o{"", ""};
  o.add_options()("a", "", cxxopts::value<std::string>());
  o.parse_positional({"a"});
  std::vector<const char*> arg = {"a.out"};
  auto r = o.parse(arg.size(), arg.data());
  EXPECT_ANY_THROW(r["a"].as<std::string>());

Expected Behavior

  cxxopts::Options o{"", ""};
  o.add_options()("a", "", cxxopts::value<std::string>());
  o.parse_positional({"a"});
  std::vector<const char*> arg = {"a.out"};
  EXPECT_ANY_THROW(o.parse(arg.size(), arg.data()));
@jarro2783
Copy link
Owner

I think that would be unintuitive for library users to have to catch that when a user doesn't type enough options. It gives more flexibility if library users have to check for the inputs, and allows some to still be left out. This request would make all positional options mandatory, which might not always be the case.

@mrbeardad
Copy link
Author

mrbeardad commented Nov 4, 2022

If an option is optional, We can give it a default value.

  cxxopts::Options o{"", ""};
  o.add_options()("a", "", cxxopts::value<std::string>()->default_value("a"));
  o.parse_positional({"a"});
  std::vector<const char*> arg = {"a.out"};
  o.parse(arg.size(), arg.data());
  EXPECT_EQ(o["a"].as<std::string>(), "a");

To check whether a positional option is type by user, use o.count("a")

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