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

TECA apps should raise error if an unexpected command-line argument is provided #794

Open
taobrienlbl opened this issue Jan 26, 2024 · 0 comments
Labels
2_medium_priority an issue that should be fixed, but is not critical to the next release feature good first issue Relatively simple issues that will help new contributors become familiar with the codebase

Comments

@taobrienlbl
Copy link
Collaborator

If a user makes a subtle typo in a command-line argument, it can lead to TECA apps behaving in a way different from what the user expects (e.g., see #791) and can be hard to track down. If possible, TECA should issue an error if an unexpected command line argument is given...unless we have some good reason for apps to tolerate unexpected arguments?

A MS Copilot question about this gives the following information:

Yes, there is a way to raise an error if unexpected command line arguments are given when using the Boost Program Options library in C++. You can remove the allow_unregistered() function from your parse command and use po::store(parse_command_line(argc, argv, desc), vm) instead. This will throw an exception on unknown options ¹.

If you want to raise an error if an option has no double hyphen, you can write an extra parser. Here is an example of such a parser:

std::pair<std::string, std::string> fix_option(const std::string& value) {
  std::string name = value;
  std::string val;
  std::string::size_type pos = name.find("=");
  if (pos != std::string::npos) {
    val = name.substr(pos + [1](https://stackoverflow.com/questions/17742259/boostprogram-options-passing-an-unknown-command-line-argument));
    name = name.substr(0, pos);
  }
  if (name.substr(0, 2) != "--") {
    throw std::logic_error(std::string("invalid command, no -- in command: ") + name);
  }
  return std::make_pair(name.substr(2), val);
}

This code example will throw an exception if an option has no double hyphen ¹.

I hope this helps!

Source: Conversation with Bing, 1/26/2024
(1) Boost::Program_options, passing an unknown command line argument. https://stackoverflow.com/questions/17742259/boostprogram-options-passing-an-unknown-command-line-argument.
(2) boost program options: argument being detected more than once. https://stackoverflow.com/questions/46078852/boost-program-options-argument-being-detected-more-than-once.
(3) Required and Optional Arguments Using Boost Library Program Options. https://stackoverflow.com/questions/5395503/required-and-optional-arguments-using-boost-library-program-options.
(4) linking with program_options has unresolved symbols on MSVC #58 - GitHub. boostorg/program_options#58.
(5) undefined. http://www.boost.org/doc/libs/[1](https://stackoverflow.com/questions/17742259/boostprogram-options-passing-an-unknown-command-line-argument)_54_0/doc/html/program_options/howto.html.

This will likely require making this change to all TECA apps, but it should be pretty straightforward to do.

@taobrienlbl taobrienlbl added feature 2_medium_priority an issue that should be fixed, but is not critical to the next release good first issue Relatively simple issues that will help new contributors become familiar with the codebase labels Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2_medium_priority an issue that should be fixed, but is not critical to the next release feature good first issue Relatively simple issues that will help new contributors become familiar with the codebase
Projects
None yet
Development

No branches or pull requests

1 participant