Skip to content

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

@taobrienlbl

Description

@taobrienlbl

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2_medium_priorityan issue that should be fixed, but is not critical to the next releasefeaturegood first issueRelatively simple issues that will help new contributors become familiar with the codebase

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions